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

Beesnest/DcEnginesSet.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  * DcEnginesSet.cpp: implementation of the CdcEnginesSet class.
00022  * for documentation see the header file.
00023  */
00024 #include "DcEnginesSet.h"
00025 
00026 void CdcEnginesSet::Login ()
00027 {
00028     if (nEngineTimeLimit > 0)
00029     {   /* Start the release thread. */
00030         isReleseThreadGo = new volatile bool (true);    /* Signal to thread to Go. */
00031         poLimitMutex  = new CdcMutex ();
00032         /* That is because I start this thread in every login. */
00033         oReleaseThread.CloseThread ();
00034         oReleaseThread.MakeThread (DcReleaseEngine, (void*) this);
00035         oReleaseThread.SetPriority (DC_TP_LOW);
00036     }
00037 }
00038 
00039 
00040 void CdcEnginesSet::Logout ()
00041 {
00042     if (nEngineTimeLimit > 0 && isReleseThreadGo)
00043         *isReleseThreadGo = false;  /* Signal to thread to exit. */
00044     if (poLimitMutex) poLimitMutex->Lock ();
00045     for (CdcEnginesSet::t_engines_map::iterator pos = mEngines.begin ();
00046          pos != mEngines.end (); ++pos)
00047         if (pos->second)
00048         {
00049             mEngines.erase (pos);
00050             if (pos->second->pEngine)
00051                 delete (pos->second->pEngine);
00052             delete pos->second;
00053         }
00054     if (poLimitMutex) poLimitMutex->Unlock ();
00055 }
00056 
00057 
00058 IdcEngine* CdcEnginesSet::GetEngine (const CdcString& name)
00059 {
00060     IdcEngine* peng = NULL;
00061     oMutex.Lock ();
00062     /* Try to find in the Engines map. */
00063     CdcEnginesSet::t_engines_map::iterator itr =
00064         mEngines.find (name);
00065     if (itr != mEngines.end () && itr->second != NULL)
00066     {   /* Found an engine. */
00067         peng = itr->second->pEngine;
00068         delete (itr->second);   /* The engine is still alive!!! */
00069         mEngines.erase (itr);
00070     }
00071     oMutex.Unlock ();
00072     return peng;
00073 }
00074 
00075 bool CdcEnginesSet::PutEngine (const CdcString& name, IdcEngine* peng)
00076 {
00077     oMutex.Lock ();
00078     bool ret = false;
00079     /* Try to find in the Engines map. */
00080     CdcEnginesSet::t_engines_map::iterator itr =
00081         mEngines.find (name);
00082     if (itr == mEngines.end () && peng)
00083     {   /* Engine doesn't exist yet. */
00084         peng->Stored ();    /* Signal "Stored" to engine. */
00085         mEngines[name] = new CdcEngineNode (peng);
00086         ret = true;
00087     }
00088     oMutex.Unlock ();
00089     return ret;
00090 }
00091 
00092 int DcReleaseEngine (void* pParam)
00093 {
00094     CdcEnginesSet* pThis = (CdcEnginesSet*)pParam;
00095     if (pThis == NULL) return 1;   /* if pParam is not valid. */
00096     volatile bool* pRun = pThis->isReleseThreadGo;
00097     CdcMutex* pMutex = pThis->poLimitMutex;
00098     int time_limit = pThis->nEngineTimeLimit;
00099     while (*pRun)
00100     {   /* Wait for 10 sec between cycles. */
00101         Sleep (10000);
00102         pMutex->Lock ();
00103         if (*pRun == false) break;
00104         CdcHttpTime now = CdcHttpTime::GetCurrentTime ();
00105         for (CdcEnginesSet::t_engines_map::iterator pos =
00106             pThis->mEngines.begin (); pos != pThis->mEngines.end (); ++pos)
00107             if (pos->second != NULL &&
00108                 now - pos->second->tStartIdle > time_limit)
00109             {   /* Release engine . */
00110                 pThis->oMutex.Lock ();
00111                 if (pos->second->pEngine)
00112                     delete (pos->second->pEngine);
00113                 delete (pos->second);
00114                 pThis->mEngines.erase (pos);
00115                 pThis->oMutex.Unlock ();
00116                 break;  /* I release just one in each loop. */
00117             }
00118         pMutex->Unlock ();
00119     }
00120     delete pRun;    /* That was allocated by the class. */
00121     delete pMutex;
00122     return 0;
00123 }

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