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

Beesnest/DcAdaptersSet.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  * DcAdaptersSet.cpp: implementation of the CdcAdaptersSet class.
00022  * for documentation see the header file.
00023  */
00024 
00025 #include "DcAdaptersSet.h"
00026 
00027 void CdcAdaptersSet::Login ()
00028 {
00029     /* Start the release thread. */
00030     isReleseThreadGo = new volatile bool (true);    /* Signal to thread to Go. */
00031     poLimitMutex  = new CdcMutex ();
00032     oReleaseThread.CloseThread ();  /* That because I start this thread in every login. */
00033     oReleaseThread.MakeThread (DcRelease, (void*) this);
00034     oReleaseThread.SetPriority (DC_TP_LOW);
00035     isLoggedIn = true;
00036 }
00037 
00038 
00039 void CdcAdaptersSet::Logout ()
00040 {
00041     isLoggedIn = false;
00042     if (isReleseThreadGo) *isReleseThreadGo = false;    /* Signal to thread to exit. */
00043     if (poLimitMutex) poLimitMutex->Lock ();        /* So limit thread doesn't run. */
00044     for (CdcAdaptersSet::t_adapters_map::iterator pos = mAdapters.begin ();
00045          pos != mAdapters.end (); ++pos)
00046         ReleaseAdapter ((*pos).first, true);    /* Release just if adapter is free. */
00047     if (poLimitMutex) poLimitMutex->Unlock ();
00048 }
00049 
00050 
00051 uint CdcAdaptersSet::GetAdapter (BufferHolder name)
00052 {
00053     if (!isLoggedIn) return 0;
00054     uint AID = 0;
00055     CdcString s_name (name);
00056     oMutex.Lock ();
00057     /* Try to find in the Adapters List. */
00058     for (CdcAdaptersSet::t_adapters_map::iterator pos =
00059         mAdapters.begin (); pos != mAdapters.end (); ++pos)
00060         if (pos->second != NULL && pos->second->sName == s_name)
00061         {   /* Found un adapter. */
00062             if (pos->second->isFree)
00063             {   /* Mark as taken and return its AID. */
00064                 pos->second->isFree = false;
00065                 oMutex.Unlock ();
00066                 return pos->second->nAID;
00067             }
00068             else if (pos->second->isMultiEngine == false)
00069             {   /* This adapter is taken and not multi-engine - return 0. */
00070                 oMutex.Unlock ();
00071                 return 0;
00072             }
00073         }
00074     /* Adapter is not in map, or it is multi-engine - get from bank. */
00075     IdcAdapter* padapter;
00076     AID = CdcAdaptersBank::GetAdapterPointer (s_name, &padapter);
00077     if (AID > 0 && padapter != NULL)
00078     {   /* Inser new adapter to map. */
00079         CdcAdapterNode* pnode = new CdcAdapterNode;
00080         if (pnode != NULL)
00081         {
00082             pnode->pAdapter = padapter;
00083             pnode->nValidSeconds = CdcParameters::ToInt (CdcParameters::
00084                 GetParam ("Adapters->" + s_name + "->Sec-To-Logout"));
00085             pnode->isMultiEngine = pnode->pAdapter->IsMultiUse () &&
00086                 CdcParameters::GetParam ("Adapters->" + s_name + "->Multi-Engine")
00087                 == "yes";
00088             pnode->isStorable = !pnode->isMultiEngine && CdcParameters::GetParam
00089                 ("Adapters->" + s_name + "->Storable") == "yes";
00090             pnode->isFree = false;
00091             pnode->sName = s_name;
00092             pnode->nAID = AID;
00093             mAdapters [AID] = pnode;
00094         }
00095     }
00096     oMutex.Unlock ();
00097     return AID;
00098 }
00099 
00100 
00101 void CdcAdaptersSet::ReleaseAdapter (uint AID, bool check_free)
00102 {
00103     oMutex.Lock ();
00104     t_adapters_map::iterator itr = mAdapters.find (AID);
00105     if (itr != mAdapters.end () && itr->second != NULL &&
00106         (!check_free || itr->second->isFree))
00107     {
00108         CdcAdaptersBank::ReleaseAdapter (itr->second->sName);   /* Mark adapter
00109                                             as free in Bank, if relevant. */
00110         mAdapters.erase (AID);
00111         /* Here I delete the adapter that was allocated by Adapter Bank. */
00112         delete itr->second->pAdapter;
00113         delete itr->second;
00114     }
00115     oMutex.Unlock ();
00116 }
00117 
00118 void CdcAdaptersSet::AdapterDone (uint AID)
00119 {
00120     oMutex.Lock ();
00121     t_adapters_map::iterator itr = mAdapters.find (AID);
00122     if (itr != mAdapters.end () && itr->second != NULL)
00123     {
00124         itr->second->isFree = true;
00125         if (itr->second->isStorable)
00126             itr->second->tStartIdle = CdcHttpTime::GetCurrentTime ();
00127         else
00128         {
00129             oMutex.Unlock ();
00130             ReleaseAdapter (AID);
00131             return;
00132         }
00133     }
00134     oMutex.Unlock ();
00135 }
00136 
00137 
00138 BufferHolder CdcAdaptersSet::Read (unsigned int AID, BufferHolder address)
00139     throw (BufferHolder)
00140 {
00141     t_adapters_map::iterator itr = mAdapters.find (AID);
00142     if (itr != mAdapters.end () && itr->second != NULL)
00143         return itr->second->pAdapter->Read (address);
00144     return BufferHolder (EMPTY_STR.c_str ());
00145 }
00146 
00147 bool CdcAdaptersSet::Write (uint AID, BufferHolder address, BufferHolder value)
00148      throw (BufferHolder)
00149 {
00150     t_adapters_map::iterator itr = mAdapters.find (AID);
00151     if (itr != mAdapters.end () && itr->second != NULL)
00152         return itr->second->pAdapter->Write (address, value);
00153     return false;
00154 }
00155 
00156 
00157 int DcRelease (void* pParam)
00158 {
00159     CdcAdaptersSet* pThis = (CdcAdaptersSet*)pParam;
00160     if (pThis == NULL) return 1;   /* if pParam is not valid. */
00161     volatile bool* pRun = pThis->isReleseThreadGo;
00162     CdcMutex* pMutex = pThis->poLimitMutex;
00163     while (*pRun)
00164     {
00165         Sleep (10000);
00166         pMutex->Lock ();
00167         if (*pRun == false) break;  /* That will release the mutex as well. */
00168         CdcHttpTime now = CdcHttpTime::GetCurrentTime ();
00169         for (CdcAdaptersSet::t_adapters_map::iterator pos =
00170             pThis->mAdapters.begin (); pos != pThis->mAdapters.end (); ++pos)
00171             if (pos->second != NULL &&
00172                 pos->second->isFree &&
00173                 pos->second->nValidSeconds > 0 &&
00174                 now - pos->second->tStartIdle > pos->second->nValidSeconds)
00175             {   /* Release adapter (if it free). */
00176                 pThis->ReleaseAdapter (pos->first, true);
00177                 break;  /* I release just one in each loop. */
00178             }
00179         pMutex->Unlock ();
00180     }
00181     delete pRun;    /* That was allocated by the class. */
00182     delete pMutex;  /* That was allocated by the class. */
00183     return 0;
00184 }

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