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 /* DcAdaptersSet.h: interface for the CdcAdaptersSet class. */ 00021 00022 #ifndef __CDC_ADAPTERS_LIST 00023 #define __CDC_ADAPTERS_LIST 00024 00025 00026 #include "DcGlobals.h" 00027 #include "DcHttpTime.h" 00028 #include "DcAdaptersBank.h" 00029 #include "Interfaces/DcAdapter.h" 00030 #include "Interfaces/DcAdaptersSet.h" 00031 #include _GET_INCLUDE_PATH (DcMutex.h) 00032 #include _GET_INCLUDE_PATH (DcThread.h) 00033 00034 /** 00035 * This class implements the IdcAdaptersSet interface, it stores all the 00036 * adapters that in use by one user. It works closely with the CdcAdaptersBank 00037 * that allocates these adapters. However this class is the one that destroys 00038 * the adapters. When an engine needs to work with an adapter, it gets it's ID 00039 * from this class, and call Read and/or Write with this ID. When the engine is 00040 * done it either releases the adapters or calls Done on the adapters IDs. 00041 * 00042 * <p> 00043 * As Adapter Set works just with the scripting engines, it does not use 00044 * CdcString. Scripting engines does not know CdcString. It uses STL string. 00045 * 00046 * <p> 00047 * This class is thread safe, as user can have more then one thread going in 00048 * the same time. An adapter however, can be in use of just one thread at a time. 00049 * 00050 * <p> 00051 * @author Erez Bibi 00052 * @version 2.0 00053 */ 00054 00055 00056 class CdcAdaptersSet : public IdcAdaptersSet 00057 { 00058 private: 00059 00060 /** 00061 * Class for one node in the adapters list. 00062 */ 00063 class CdcAdapterNode 00064 { 00065 public: 00066 IdcAdapter* pAdapter; /** Pointer to the adapter itself. */ 00067 CdcHttpTime tStartIdle; /** Time of saving an adapter (idle start time) */ 00068 int nValidSeconds; /** Number of seconds to keep the adapter idle. */ 00069 bool isFree; /** Is adapter free flag */ 00070 bool isMultiEngine; /** True if this is a multi-use adapter. */ 00071 bool isStorable; /** True if need to save this adapter. */ 00072 CdcString sName; /** The adapter name. */ 00073 uint nAID; /** The adapter ID. */ 00074 }; 00075 00076 typedef map <uint, CdcAdapterNode*> t_adapters_map; 00077 00078 /** Map of adapters and IDs. */ 00079 t_adapters_map mAdapters; 00080 00081 /** Mutex for this object. */ 00082 CdcMutex oMutex; 00083 00084 /** True when this set is in a logged in state. */ 00085 bool isLoggedIn; 00086 00087 /** The "auto release" thread object. */ 00088 CdcThread oReleaseThread; 00089 00090 /** Flag to signal to the Release thread to exit 00091 (dynamically allocated by the class and delete by the limit thread). 00092 It works this way so the thread will still have something to look at 00093 even when the class object is deallocated. */ 00094 volatile bool* isReleseThreadGo; 00095 00096 /** Dynamically allocated Mutex to prevent Release Thread clash with other 00097 threads. */ 00098 CdcMutex* poLimitMutex; 00099 00100 public: 00101 00102 /** 00103 * Constructor - Does nothing. 00104 */ 00105 CdcAdaptersSet () 00106 :isReleseThreadGo (NULL), poLimitMutex (NULL), isLoggedIn (false){} 00107 00108 /** 00109 * Destructor - Does nothing. 00110 */ 00111 virtual ~CdcAdaptersSet() {} 00112 00113 /** 00114 * Start the Release thread. 00115 */ 00116 void Login (); 00117 00118 /** 00119 * release all unused adapters, and close the Release thread. 00120 */ 00121 void Logout (); 00122 00123 /** 00124 * This function tries to find an (ID of) adapter by the name 'name'. 00125 * It looks first in the class adapters map. If it finds an adapter with 00126 * the same name that is free, it returns it's ID, and marks it as not free. 00127 * This adapter will always be a non multi-engine adapter, otherwise it 00128 * will not be saved as free adapter. If it finds a taken adapter in the map 00129 * and the adapter is non multi-engine, it returns 0. If it doesn't find an 00130 * adapter in the map, or it finds a taken one but it is a multi-engine 00131 * adapter, it tries to get the adapter from the Adapters Bank. The adapters 00132 * bank might return a new adapter or not. 00133 * <p> 00134 * @param name The desired adapter name. 00135 * @return (uint) The adapter ID or 0 if cannot get this adapter. 00136 */ 00137 virtual uint GetAdapter (BufferHolder name); 00138 00139 /** This function releases (delete) an adapter. The function notify the 00140 * Adapters Bank that it deletes this adapter. This notification is relevant 00141 * for the Bank, just if it marked this adapter as taken. 00142 * <p> 00143 * @param AID The adapter ID. 00144 * @param check_free Is a flag to indicate to this function not to 00145 * release the adapter if it is taken by another thread. 00146 */ 00147 virtual void ReleaseAdapter (uint AID, bool check_free=false); 00148 00149 /** 00150 * When an engine exits (done with a script) it calls this function on every 00151 * adapter it has (got with GetAdapter and didn't release). If the 00152 * adapter is not "Storable", the class will release (delete) it. If the 00153 * adapter is "Storable", the class will mark it as free, set the tStartIdle 00154 * time (for the release idles thread), but keep the adapter in its 00155 * adapters map. 00156 * <p> 00157 * @param AID The ID of the adapter an engine is done with. 00158 */ 00159 void AdapterDone (uint AID); 00160 00161 /** 00162 * This is the function that calls the adapter Read function. This function 00163 * does not lock the class Mutex. 00164 * <p> 00165 * @param AID The adapter ID. 00166 * @param address The address (or name) to read from. 00167 * @throw (BufferHolder) Just pass an exception from the adapter. 00168 * @return (BufferHolder) Holds the result string (or an empty string). 00169 */ 00170 virtual BufferHolder Read (unsigned int AID, BufferHolder address) 00171 throw (BufferHolder); 00172 00173 /** 00174 * This is the function that calls the adapter Write function. This function 00175 * does not lock the class Mutex. 00176 * <p> 00177 * @param AID The adapter ID. 00178 * @param address The address (or name) to write to. 00179 * @param value The string to write to this address (Optional). 00180 * @throw (BufferHolder) Just pass an exception from the adapter. 00181 * @return (bool) True on success. 00182 */ 00183 virtual bool Write (uint AID, BufferHolder address, BufferHolder value) 00184 throw (BufferHolder); 00185 00186 00187 /** 00188 * This is a global friend function. This function runs as a 00189 * separate thread, has access to all the private members of the 00190 * class and in charge of releasing adapters that have not been 00191 * used for sometime. The time is defined in the parameters file 00192 * for each adapter. Adapter does not has to have a release time. 00193 */ 00194 friend int DcRelease (void* pParam); 00195 }; 00196 00197 #endif /* __CDC_ADAPTERS_LIST */