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

Beesnest/DcAdaptersBank.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  * DcAdaptersBank.cpp: implementation of the CdcAdaptersBank class.
00022  * for documentation see the header file.
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));    /* For Adapters IDs. */
00043     /* Get adapters list. */
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     {   /* Build the adapter and DLL names. */
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 (); /* if padapter = NULL, AID = 0. */
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     /* Get the DLL "GetAdapter" function. */
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     /* Get the adapter (just here I send 'name' to the dll). */
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     /* Return the pointer to the adapter. */
00127     adapter->bAvailable = padapter->IsMultiUse () &&
00128         CdcParameters::GetParam ("Adapters->" + name + "->Multi-User") == "yes";
00129     return padapter;
00130 }
00131 

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