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 /* DcAdaptersBank.h: interface for the CdcAdaptersBank class. */ 00021 00022 #ifndef __CDC_ADAPTERS_BANK 00023 #define __CDC_ADAPTERS_BANK 00024 00025 00026 #include "DcGlobals.h" 00027 #include "DcParameters.h" 00028 #include "Interfaces/DcAdapter.h" 00029 00030 #include _GET_INCLUDE_PATH (DcMutex.h) 00031 #include _GET_INCLUDE_PATH (DcDynamicLibrary.h) 00032 00033 /** 00034 * This is a static class to load and store the adapters from Dynamic Libraries. 00035 * It loads all the adapters that list at the "Adapters" part of the 00036 * configuration files, and saves them in a map. Then the user can 00037 * ask for a pointer to a specific adapter interface. If the adapter is marked 00038 * as non multi-use, the class will give a pointer to its interface just to one 00039 * user. it will wait until the user release the adapter before giving it to 00040 * another user. 00041 * 00042 * <p> 00043 * This class is thread safe. 00044 * 00045 * <p> 00046 * @author Erez Bibi 00047 * @version 1.0 00048 */ 00049 00050 class CdcAdaptersBank 00051 { 00052 private: 00053 00054 /** Class to store one adapter in the map. */ 00055 class CdcAdapterNode 00056 { 00057 public: 00058 CdcString sDllName; 00059 bool bAvailable; 00060 CdcDynamicLibrary oLib; 00061 00062 /* Load regular adapter. */ 00063 CdcAdapterNode (const CdcString& dll_name) 00064 : oLib (dll_name), sDllName (dll_name), bAvailable (true) 00065 {} 00066 00067 /* Default constructor. */ 00068 CdcAdapterNode () 00069 : bAvailable (false), oLib ("None") {} 00070 00071 /* Destructor. */ 00072 ~CdcAdapterNode () 00073 {} 00074 }; 00075 00076 typedef map <CdcString, CdcAdapterNode*> t_adapter_map; 00077 00078 /** The adapters map. */ 00079 static t_adapter_map mAdapters; 00080 00081 /** Mutex for the AdaptersBank class. */ 00082 static CdcMutex oMutex; 00083 00084 /** No Constructor. */ 00085 CdcAdaptersBank() {} 00086 /** No Destructor. */ 00087 ~CdcAdaptersBank() {} 00088 00089 00090 /** 00091 * This function loads the dynamic library and get pointer to the adapter. 00092 * It mark the adapter as not available, saves the handle to the 00093 * library and copy of the pointer to the adapter in the 00094 * CdcAdapterNode. If the adapter is not available in the first 00095 * place, the function returns NULL. 00096 * <p> 00097 * @param adapter Pointer to the CdcAdapterNode of the adapter 00098 * to return. 00099 * @param name The name of the adapter (I need it just to transfer 00100 * to the DLL GetAdapter function). 00101 * @return (IdcAdapter*) Pointer to the adapter, or NULL if adapter 00102 * is in use by other user, or if the library cannot be loaded. 00103 */ 00104 static IdcAdapter* GetAdapterPointer (CdcAdapterNode* adapter, 00105 const CdcString& name); 00106 00107 /** 00108 * This function releases an adapter and marks it as available. 00109 * <p> 00110 * @param adapter Pointer to the CdcAdapterNode of the adapter 00111 * to release. 00112 */ 00113 static void ReleaseAdapterPointer (CdcAdapterNode* adapter); 00114 00115 public: 00116 00117 /** 00118 * Loads the adapters from the dynamic library, according to the list in the 00119 * Parameters file. 00120 */ 00121 static void Init (); 00122 00123 /** 00124 * Deletes all adapters and releases libraries. <br> 00125 * Note - The function does not check if an adapter is in use 00126 * before releasing it. 00127 */ 00128 static void Term (); 00129 00130 /** 00131 * Checks if a specific adapter is in use. 00132 * <p> 00133 * @param name The name of the adapter. 00134 * @return (bool) True if the adapter is available. The function 00135 * will return False if the adapter is in use or if it is not 00136 * exist. 00137 */ 00138 static bool IsAdapterAvailable (const CdcString& name); 00139 00140 /** 00141 * This function will return a pointer to a specific adapter 00142 * interface, or NULL if the adapter is not available or it doesn't exist. 00143 * <p> 00144 * @param name The name of the adapter. 00145 * @return (IdcAdapter**) A pointer to the adapter pointer interface. 00146 */ 00147 static uint GetAdapterPointer (const CdcString& name, 00148 IdcAdapter** padapter); 00149 00150 /** 00151 * The user has to call this function when it done using the adapter. 00152 * <p> 00153 * @param name The name of the adapter. 00154 */ 00155 static void ReleaseAdapter (const CdcString& name); 00156 }; 00157 00158 #endif /* __CDC_ADAPTERS_BANK */ 00159