Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "DcAdaptersBank.h"
00026
00027 #define FUNC_NAME "GetAdapter"
00028
00029 typedef bool (__stdcall *cfunc)(IdcAdapter** pp_adapter,
00030 const char* name,
00031 bool (*pGetParam)(const char*, char*, int));
00032
00033
00034
00035 CdcAdaptersBank::t_adapter_map CdcAdaptersBank::mAdapters;
00036
00037 CdcMutex CdcAdaptersBank::oMutex;
00038
00039
00040 void CdcAdaptersBank::Init ()
00041 {
00042 srand (time (NULL));
00043
00044 CdcParameters::CdcNode* pnode = CdcParameters::GetNodePointer
00045 ("Adapters");
00046 if (pnode == NULL) return;
00047
00048 oMutex.Lock ();
00049 CdcParameters::CdcNode::t_node_list::iterator itr;
00050 for (itr = pnode->oNodes.begin (); itr != pnode->oNodes.end (); ++itr)
00051 {
00052 CdcString ad_name = ((CdcParameters::CdcNode*)*itr)->name;
00053 CdcString dll_name = CdcParameters::GetParam ("Adapters->" +
00054 ad_name + "->File-Name");
00055 mAdapters[ad_name] = new CdcAdapterNode (dll_name);
00056 }
00057 oMutex.Unlock ();
00058 }
00059
00060
00061 void CdcAdaptersBank::Term ()
00062 {
00063 oMutex.Lock ();
00064 for (CdcAdaptersBank::t_adapter_map::iterator pos = mAdapters.begin ();
00065 pos != mAdapters.end (); ++pos)
00066 delete pos->second;
00067 mAdapters.clear ();
00068 oMutex.Unlock ();
00069 }
00070
00071
00072 bool CdcAdaptersBank::IsAdapterAvailable (const CdcString& name)
00073 {
00074 oMutex.Lock ();
00075 CdcAdaptersBank::t_adapter_map::iterator pos = mAdapters.find (name);
00076 oMutex.Unlock ();
00077 if (pos == mAdapters.end () || pos->second == NULL) return false;
00078 return pos->second->bAvailable;
00079 }
00080
00081
00082 uint CdcAdaptersBank::GetAdapterPointer (const CdcString& name,
00083 IdcAdapter** padapter)
00084 {
00085 oMutex.Lock ();
00086 CdcAdaptersBank::t_adapter_map::iterator pos = mAdapters.find (name);
00087 if (pos == mAdapters.end () || pos->second == NULL) return 0;
00088 *padapter = GetAdapterPointer (pos->second, name);
00089 uint AID = (uint)(*padapter) * rand ();
00090 oMutex.Unlock ();
00091 return AID;
00092 }
00093
00094
00095 void CdcAdaptersBank::ReleaseAdapter (const CdcString& name)
00096 {
00097 oMutex.Lock ();
00098 CdcAdaptersBank::t_adapter_map::iterator pos = mAdapters.find (name);
00099 if (pos != mAdapters.end () && pos->second != NULL)
00100 pos->second->bAvailable = true;
00101 oMutex.Unlock ();
00102 }
00103
00104
00105 IdcAdapter* CdcAdaptersBank::GetAdapterPointer (
00106 CdcAdaptersBank::CdcAdapterNode* adapter,
00107 const CdcString& name)
00108 {
00109 if (adapter == NULL) return NULL;
00110 if (adapter->bAvailable == false) return NULL;
00111
00112 cfunc dllGetAdapter = (cfunc)adapter->oLib.GetFunctionByName (FUNC_NAME);
00113 if (dllGetAdapter == NULL)
00114 {
00115 PrintString (adapter->oLib.GetLastError (), PS_GEN);
00116 return NULL;
00117 }
00118
00119 IdcAdapter* padapter = NULL;
00120 dllGetAdapter (&padapter, name.GetBuffer (), &ExtGetParam);
00121 if (padapter == NULL)
00122 {
00123 PrintString ("Unable to get the adapter from " + adapter->sDllName, PS_GEN);
00124 return NULL;
00125 }
00126
00127 adapter->bAvailable = padapter->IsMultiUse () &&
00128 CdcParameters::GetParam ("Adapters->" + name + "->Multi-User") == "yes";
00129 return padapter;
00130 }
00131