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 /* DcEnginesSet.h: interface for the CdcEnginesSet class. */ 00021 00022 #ifndef __CDC_ENGINES_LIST 00023 #define __CDC_ENGINES_LIST 00024 00025 00026 #include "DcGlobals.h" 00027 #include "DcHttpTime.h" 00028 #include "DcParameters.h" 00029 #include "Interfaces/DcEngine.h" 00030 #include _GET_INCLUDE_PATH (DcMutex.h) 00031 #include _GET_INCLUDE_PATH (DcThread.h) 00032 00033 /** 00034 * This class works very similar to <b>AdaptersSet</b>, just a little simpler. 00035 * In this case the <b>EnginesBank</b> is the client and this class is the server. 00036 * EnginesBank can put an engine here for storage, and can take an engine out. 00037 * When the EnginesBank takes an engine out, the class delete the place for this 00038 * engine, but the engine of course is still alive. When EnginesBank puts an 00039 * engine in, the class allocates a space for it, and set the idle Start Time to 00040 * Now. The only cases when this class delete an engine itself, is if it was 00041 * stored over the time limit, or if it was stored when the user logged out 00042 * (in the Destructor). Otherwise the Engines Bank is the one that deletes the 00043 * engines. It simply take them out and doesn't put them back in. 00044 * 00045 * <p> 00046 * This class is thread safe, as user can have more then one thread going in 00047 * the same time. An engine however, can be in use of just one thread at a time. 00048 * 00049 * <p> 00050 * @author Erez Bibi 00051 * @version 1.0 00052 */ 00053 00054 00055 class CdcEnginesSet 00056 { 00057 private: 00058 00059 /** 00060 * Class of one node in the engines list. 00061 */ 00062 class CdcEngineNode 00063 { 00064 public: 00065 IdcEngine* pEngine; /** Pointer to the engine itself. */ 00066 CdcHttpTime tStartIdle; /** Time of saving an engine (idle) */ 00067 00068 /** Constructor - Save an engine pointer and init the time to now. */ 00069 CdcEngineNode (IdcEngine* peng) 00070 :pEngine (peng) {} 00071 }; 00072 00073 typedef map <CdcString, CdcEngineNode*> t_engines_map; 00074 00075 /** Map of engines and names. */ 00076 t_engines_map mEngines; 00077 00078 /** Mutex for this object. */ 00079 CdcMutex oMutex; 00080 00081 /** True when this set is in a logged in state. */ 00082 bool isLoggedIn; 00083 00084 /** The "auto release" thread object. */ 00085 CdcThread oReleaseThread; 00086 00087 /** Flag for signaling the ReLease thread to exit 00088 (dynamically allocated by the class and delete by the Release thread). 00089 It works this way so the thread will still have something to look at 00090 even when the class object is deallocated. */ 00091 volatile bool* isReleseThreadGo; 00092 00093 /** Dynamically allocated Mutex to prevent Release Thread clash with other 00094 threads. */ 00095 CdcMutex* poLimitMutex; 00096 00097 /** The time limit for all engines. */ 00098 int nEngineTimeLimit; 00099 00100 public: 00101 00102 /** 00103 * Constructor - Does nothing. 00104 */ 00105 CdcEnginesSet () 00106 :isReleseThreadGo (NULL), poLimitMutex (NULL) 00107 { nEngineTimeLimit = 00108 CdcParameters::ToInt (CdcParameters::GetParam ("Server->Engine-Time-Limit")); } 00109 00110 /** 00111 * The Destructor - Does nothing. 00112 */ 00113 virtual ~CdcEnginesSet() {} 00114 00115 /** 00116 * start the release thread. 00117 */ 00118 void Login (); 00119 00120 /** 00121 * close all engines, and close the release thread. 00122 */ 00123 void Logout (); 00124 00125 /** 00126 * If this object has an engine of the name 'name' it returns it and removes 00127 * it from its map. 00128 * <p> 00129 * @param name The desired engine name. 00130 * @return (IdcEngine*) The engine pointer or NULL. 00131 */ 00132 IdcEngine* GetEngine (const CdcString& name); 00133 00134 /** 00135 * This function adds an engine to the set. If an engine with this name is 00136 * already in the set, it will not replace it and return false. 00137 * <p> 00138 * @param name The engine name. 00139 * @param peng A pointer to this engine. 00140 * @return (bool) True on success, or False if an engine with this name 00141 * is already in the set. 00142 */ 00143 bool PutEngine (const CdcString& name, IdcEngine* peng); 00144 00145 /** 00146 * This is a global friend function. This function runs in a separate thread, 00147 * has access to all private members of the class and in charge of releasing 00148 * engines that have not been used for sometime. The time is defined in the 00149 * parameters file for all the engines. This time limit can be zero (so 00150 * engines will not be released). The release thread get created just if the 00151 * time limit for the engines exists and its more then 0. 00152 */ 00153 friend int DcReleaseEngine (void* pParam); 00154 }; 00155 00156 #endif /* __CDC_ENGINES_LIST */