• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/DcEnginesBank.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net)
00003 This file is part of Beesnest.
00004 
00005 Beesnest is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 Beesnest is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with Beesnest; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 /**
00021  * DcEnginesBank.cpp: implementation of the CdcEnginesBank class.
00022  * for documentation see the header file.
00023  */
00024 
00025 #include "DcEnginesBank.h"
00026 
00027 #define FUNC_NAME       "GetEngine"
00028 #define INIT_FUNC_NAME  "Init"
00029 #define TERM_FUNC_NAME  "Term"
00030 
00031 /* The GetEngine LIB proto function */
00032 typedef bool (__stdcall *cfunc)(IdcEngine** pp_engine,
00033                                 const char* name,
00034                                 bool (*pGetParam)(const char*, char*, int));
00035 /* The Init/Term function proto */
00036 typedef void (__stdcall *cfunc_void)();
00037 
00038 CdcEnginesBank::t_engines_lib_map CdcEnginesBank::mLibs;
00039 CdcMutex CdcEnginesBank::oMutex;
00040 CdcString CdcEnginesBank::sDefaultEngine;
00041 
00042 CdcEnginesBank::CdcEnginesBank (IdcRequest* preq, IdcResponse* pres,
00043     CdcUserStatus* pstatus)
00044 : pRequest (preq), pResponse (pres), pAdaptersSet (NULL), pEnginesSet (NULL)
00045 {
00046     if (pstatus)
00047     {
00048         pAdaptersSet = pstatus->GetAdaptersSet ();
00049         pEnginesSet = pstatus->GetEnginesSet ();
00050     }
00051 }
00052 
00053 CdcEnginesBank::~CdcEnginesBank ()
00054 {   /* Either store each engines in the Engines Set, or delete it. */
00055     for (CdcEnginesBank::t_engines_map::iterator pos = mEngines.begin ();
00056         pos != mEngines.end (); ++pos)
00057         if (pos->second)
00058         {
00059             if (pEnginesSet && pos->second->IsStorable () &&
00060                 CdcParameters::GetParam ("Engines->" + pos->first + "->Storable")
00061                 == "yes")
00062             {   /* Need to store the engine in the user EnginesSet. */
00063                 if (pEnginesSet->PutEngine (pos->first, pos->second) == false)
00064                     delete pos->second;     /* Engine didn't get stored. */
00065             }
00066             else    /* Need to delete this engine. */
00067                 delete pos->second;
00068         }
00069 }
00070 
00071 
00072 /* Note: I insert engine/lib to map also if I cannot load the library, so I
00073     won't need to try to load them again. */
00074 IdcEngine* CdcEnginesBank::GetEngine (const CdcString& name)
00075 {
00076     CdcString eng_name = name;
00077     if (eng_name.IsEmpty () == true)
00078         eng_name = sDefaultEngine;
00079     if (eng_name.IsEmpty () == true)
00080         return NULL;
00081     /* Try to find the engine in the map (if processing scripts on the same page). */
00082     CdcEnginesBank::t_engines_map::iterator itr;
00083     itr = mEngines.find (eng_name);
00084     if (itr != mEngines.end () && itr->second)
00085         return itr->second;
00086     /* Try to find the engine in the users Engine Set - user is logged in .*/
00087     IdcEngine* peng = NULL;
00088     if (pEnginesSet)
00089     {
00090         peng = pEnginesSet->GetEngine (eng_name);
00091         if (peng)
00092         {
00093             mEngines [eng_name] = peng;
00094             peng->Prepare (pRequest, pResponse, pAdaptersSet);
00095             return peng;
00096         }
00097     }
00098     /* Need to load the engine from a dynamic library. */
00099     CdcDynamicLibrary* plib = NULL;
00100     CdcString lib_name = CdcParameters::GetParam ("Engines->" + eng_name +
00101         "->File-Name");
00102     if (lib_name.IsEmpty () == false)
00103     {   /* Found library. */
00104         CdcEnginesBank::t_engines_lib_map::iterator ditr = mLibs.find (lib_name);
00105         if (ditr != mLibs.end ())
00106             plib = ditr->second;        /* Found Lib in the libs map. */
00107         else
00108             plib = LoadLib (lib_name);  /* Load Lib for the first time. */
00109     }
00110     if (plib != NULL)
00111         peng = LoadEngine (*plib, eng_name);    /* plib may point to an un-opend library! */
00112     mEngines [eng_name] = peng;
00113     return peng;                        /* peng may be NULL! */
00114 }
00115 
00116 void CdcEnginesBank::DeleteEngine (const CdcString& name)
00117 {
00118     CdcString eng_name = name;
00119     if (eng_name.IsEmpty () == true)
00120         eng_name = sDefaultEngine;
00121     if (eng_name.IsEmpty () == true)
00122         return;
00123 
00124     CdcEnginesBank::t_engines_map::iterator itr;
00125     itr = mEngines.find (eng_name);
00126     if (itr != mEngines.end () && itr->second)
00127     {
00128         delete (itr->second);
00129         mEngines.erase (itr);
00130     }
00131 }
00132 
00133 IdcEngine* CdcEnginesBank::LoadEngine (CdcDynamicLibrary& lib,
00134                                         const CdcString& name)
00135 {
00136     /* Get the LIB "GetEngine" function. */
00137     cfunc libGetEngine = (cfunc)lib.GetFunctionByName (FUNC_NAME);
00138     if (libGetEngine == NULL)
00139     {
00140         PrintString (lib.GetLastError (), PS_GEN);
00141         return NULL;
00142     }
00143     /* Get the engine (just here I send 'name' to the lib). */
00144     IdcEngine* pengine = NULL;
00145     libGetEngine (&pengine, name.GetBuffer (), &ExtGetParam);
00146     if (pengine == NULL)
00147     {
00148         PrintString ("Unable to get the engine - " + name, PS_GEN);
00149         return NULL;
00150     }
00151     /* Init the engine */
00152     pengine->Prepare (pRequest, pResponse, pAdaptersSet);
00153     return pengine;
00154 }
00155 
00156 CdcDynamicLibrary* CdcEnginesBank::LoadLib (const CdcString& name)
00157 {
00158     oMutex.Lock ();
00159         CdcDynamicLibrary* plib = NULL;
00160         /* just check again to be sure. */
00161         CdcEnginesBank::t_engines_lib_map::iterator itr;
00162         itr = mLibs.find (name);
00163         if (itr == mLibs.end ())
00164         {
00165             plib = new CdcDynamicLibrary (name);
00166             if (plib != NULL)
00167             {
00168                 CdcEnginesBank::ExecInit (*plib);
00169                 mLibs [name] = plib;
00170             }
00171         }
00172         else
00173             plib = itr->second;
00174     oMutex.Unlock ();
00175     return plib;
00176 }
00177 
00178 void CdcEnginesBank::Init ()
00179 {
00180     sDefaultEngine = CdcParameters::GetParam ("Server->Default-Engine");
00181 }
00182 
00183 void CdcEnginesBank::Term ()
00184 {
00185     /* Release all Libs. */
00186     oMutex.Lock ();
00187     for (CdcEnginesBank::t_engines_lib_map::iterator pos = mLibs.begin ();
00188         pos != mLibs.end (); ++pos)
00189         if (pos->second)
00190         {
00191             CdcEnginesBank::ExecTerm (*(pos->second));
00192             delete pos->second;     /* Here the dynamic library get freed. */
00193         }
00194     oMutex.Unlock ();
00195 }
00196 
00197 void CdcEnginesBank::ExecInit (CdcDynamicLibrary& lib)
00198 {
00199     /* Get the library "Init" function. */
00200     cfunc_void libInit = (cfunc_void)lib.GetFunctionByName (INIT_FUNC_NAME);
00201     if (libInit != NULL)
00202         libInit ();
00203 }
00204 
00205 void CdcEnginesBank::ExecTerm (CdcDynamicLibrary& lib)
00206 {
00207     /* Get the LIB "Term" function. */
00208     cfunc_void libTerm = NULL;
00209     libTerm = (cfunc_void)lib.GetFunctionByName (TERM_FUNC_NAME);
00210     if (libTerm != NULL)
00211         libTerm ();
00212 }
00213 

Generated on Mon Oct 11 2010 16:23:23 for Beesnest by  doxygen 1.7.2