freControllerCentral.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   F.R.E.E. - flexible registration evaluation engine
00004   Version:   v.1.0.0
00005   Date:      $Date: 2006/09/01 12:00:00 $
00006   Module:    $RCSfile: freControllerCentral.cxx,v $
00007   Language:  C++
00008 
00009 
00010 
00011   Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics,
00012   Institute for Medical Biometry and Informatics, University of Heidelberg,
00013   Germany). All rights reserved.
00014   See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm
00015   for details.
00016 
00017      This software is distributed WITHOUT ANY WARRANTY; without even 
00018      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00019      PURPOSE.  See the above copyright notices for more information.
00020 
00021 =========================================================================*/
00022 #if defined(_MSC_VER)
00023 #pragma warning ( disable : 4786 )
00024 #endif
00025 
00026 #include "freControllerCentral.h"
00027 #include "freExceptions.h"
00028 #include "freFileDispatch.h"
00029 
00030 #include "itkDirectory.h"
00031 #include "itkVersion.h"
00032 #include "itksys/SystemTools.hxx"
00033 
00034 namespace FREE
00035 {
00036 
00037 class CleanUpControllerCentral
00038 {
00039 public:
00040   inline void Use() 
00041   {
00042   }
00043   ~CleanUpControllerCentral()
00044   {
00045     ControllerCentral::UnRegisterCallbackEventList();
00046     ControllerCentral::UnRegisterProgressEventList();
00047     ControllerCentral::UnRegisterAllControllers();
00048   }  
00049 };
00050 static CleanUpControllerCentral CleanUpControllerCentralGlobal;
00051 
00052 ControllerInfo::ControllerInfo(GenericComponentController* pController,
00053                                const LibraryHandleType& libraryHandle, const std::string& sLibraryFile)
00054 {
00055   if (!pController) throwStaticExceptionMacro("Error. Cannot create a controller info by passing a null pointer as controller.");
00056   m_ControllerID = pController->ControllerID();
00057   m_pController = pController;
00058   m_LibraryHandle = libraryHandle;
00059   m_LibraryFile = sLibraryFile;
00060   m_bOwnedByLibrary = true;
00061 };
00062 
00063 ControllerInfo::ControllerInfo(GenericComponentController* pController)
00064 {
00065   if (!pController) throwStaticExceptionMacro("Error. Cannot create a controller info by passing a null pointer as controller.");
00066   m_ControllerID = pController->ControllerID();
00067   m_pController = pController;
00068   m_bOwnedByLibrary = false;
00069   m_LibraryFile = "";
00070 };
00071 
00075 ControllerCentral::ControllerListType* ControllerCentral::m_RegisteredControllers = NULL;
00076 
00080 ControllerCentral::LibraryHandleListType* ControllerCentral::m_LibraryHandles = NULL;
00081 
00085 ControllerCentral::CtrlCallbackListType* ControllerCentral::m_CtrlCallbacks = NULL;
00086 
00090 ControllerCentral::CtrlProgressListType* ControllerCentral::m_CtrlProgress = NULL;
00091 
00095 ProgressEventBase::Pointer ControllerCentral::m_fnOnControl = NULL;
00096 ProgressEventBase::Pointer ControllerCentral::m_fnOnBuild = NULL;
00097 
00101 bool ControllerCentral::m_bControllersAreExternal = false;
00102 
00103 itk::MutexLock::Pointer ControllerCentral::m_CallbackMutex = itk::MutexLock::New();
00104 itk::MutexLock::Pointer ControllerCentral::m_ProgressMutex = itk::MutexLock::New();
00105 
00106 itk::MutexLock* ControllerCentral::m_GlobalCallbackMutex = 0;
00107 itk::MutexLock* ControllerCentral::m_GlobalProgressMutex = 0;
00108 
00112 void
00113 ControllerCentral::
00114 Initialize()
00115 {
00116   try
00117   {
00118     if (!m_RegisteredControllers)
00119     {
00120       m_CallbackMutex->Lock();
00121       if (!m_CtrlCallbacks) m_CtrlCallbacks = new CtrlCallbackListType;
00122       m_CallbackMutex->Unlock();
00123      
00124       m_ProgressMutex->Lock();
00125       if (!m_CtrlProgress) m_CtrlProgress = new CtrlProgressListType;
00126       m_ProgressMutex->Unlock();
00127 
00128       m_RegisteredControllers = new ControllerListType;
00129       m_LibraryHandles = new LibraryHandleListType;
00130 
00131       ControllerCentral::RegisterDefaults();
00132       ControllerCentral::LoadDynamicControllers();
00133     }
00134   }
00135   catchAllNPassStaticMacro("Unknown Exception, while initializing free::ControllerCentral.");
00136 };
00137 
00138 void
00139 ControllerCentral::
00140 RegisterDefaults()
00141 {
00142 }
00143 
00144 void
00145 ControllerCentral::
00146 LinkControllerCentralAsDedicated(ControllerListType* pDedicatedControllerList,
00147                                  LibraryHandleListType* pDedicatedLibraryHandles,
00148                                  ProgressEventBase* pfnOnControl,
00149                                  ProgressEventBase* pfnOnBuild,
00150                                  CtrlCallbackListType* pCtrlCallbackList,
00151                                  CtrlProgressListType* pCtrlProgressList,
00152                                  itk::MutexLock* pGlobalCallbackMutex,
00153                                  itk::MutexLock* pGlobalProgressMutex)
00154 {
00155   if (m_RegisteredControllers!=pDedicatedControllerList)
00156   {
00157     UnRegisterAllControllers();
00158     m_LibraryHandles = pDedicatedLibraryHandles;
00159     m_RegisteredControllers = pDedicatedControllerList;
00160     m_bControllersAreExternal = true;
00161   }
00162 
00163   m_CallbackMutex->Lock();
00164   if (m_CtrlCallbacks!=pCtrlCallbackList)
00165   {
00166     ControllerCentral::UnRegisterCallbackEventList();
00167     m_CtrlCallbacks = pCtrlCallbackList;
00168     m_GlobalCallbackMutex = pGlobalCallbackMutex;
00169   }
00170   m_CallbackMutex->Unlock();
00171 
00172   m_ProgressMutex->Lock();
00173   if (m_CtrlProgress!=pCtrlProgressList)
00174   {
00175     ControllerCentral::UnRegisterProgressEventList();
00176     m_CtrlProgress = pCtrlProgressList;
00177     m_GlobalProgressMutex = pGlobalProgressMutex;
00178   }
00179   m_ProgressMutex->Unlock();
00180 
00181   m_fnOnControl = pfnOnControl;
00182   m_fnOnBuild = pfnOnBuild;
00183 };
00184 
00188 void
00189 ControllerCentral::
00190 LoadDynamicControllers()
00191 {
00195 #ifdef _WIN32
00196   char cPathSeparator = ';';
00197 #else
00198   char cPathSeparator = ':';
00199 #endif
00200   
00201   std::string sLoadPath = GetGeneralFREEPath();
00202   std::string sAutoLoadPath;
00203   
00204 #ifndef _DEBUG
00205   //Auto load path should not be added in the debug version of f.r.e.e. to avoid
00206   //conflicts with any installations of release versions
00207         if (itksys::SystemTools::GetEnv(csFREEAutoLoadPath,sAutoLoadPath)) sLoadPath += cPathSeparator+sAutoLoadPath;
00208 #endif
00209 
00210   std::string sCurrentDirectory = itksys::SystemTools::GetCurrentWorkingDirectory();
00211 
00212   if (!sCurrentDirectory.empty())
00213         {
00214                 sLoadPath += cPathSeparator+sCurrentDirectory;
00215         }
00216 
00217   if(sLoadPath.size() == 0)
00218   {
00219     return;
00220   }
00221   
00222         std::string::size_type endSeparatorPosition = 0;
00223   std::string::size_type startSeparatorPosition = 0;
00224 
00225   while ( startSeparatorPosition != std::string::npos )
00226   {
00227     startSeparatorPosition = endSeparatorPosition;
00231     endSeparatorPosition = sLoadPath.find(cPathSeparator, startSeparatorPosition);
00232 
00233     if(endSeparatorPosition == std::string::npos)
00234     {
00235       endSeparatorPosition = sLoadPath.size();
00236     }
00237 
00238     std::string CurrentPath = sLoadPath.substr(startSeparatorPosition, endSeparatorPosition-startSeparatorPosition);
00239     ControllerCentral::LoadLibrariesInPath(CurrentPath.c_str());
00240     
00244     if(endSeparatorPosition == sLoadPath.size())
00245     {
00246       startSeparatorPosition = std::string::npos;
00247     }
00248     else
00249     {
00250       endSeparatorPosition++;
00251     }
00252   }
00253 }
00254 
00255 
00262 inline bool
00263 NameIsSharedLibrary(const char* name)
00264 {
00265   std::string sname = name;
00266   if ( sname.find(itk::DynamicLoader::LibExtension()) != std::string::npos )
00267     {
00268     return true;
00269     }
00270   return false;
00271 }
00272 
00273 
00274 void
00275 ControllerCentral::
00276 LoadLibrariesInPath(const char* path)
00277 {
00278         itk::Directory::Pointer dir = itk::Directory::New();
00279         if ( !dir->Load(path) )
00280         {
00281                 return;
00282         }
00283 
00287         for ( unsigned int i = 0; i < dir->GetNumberOfFiles(); i++ )
00288         {
00289                 const char* file = dir->GetFile(i);
00294                 if ( NameIsSharedLibrary(file) )
00295                 {
00296       std::string fullpath = FileDispatch::CreateFullPath(path, file);
00297                         itk::LibHandle lib = itk::DynamicLoader::OpenLibrary(fullpath.c_str());
00298 
00299                         if ( lib )
00300                         {
00302                                 FREE_GET_CONTROLLER_LIST_FUNCTION pListFunction = (FREE_GET_CONTROLLER_LIST_FUNCTION)itk::DynamicLoader::GetSymbolAddress(lib, "FREEGetControllerList");
00303                                 FREE_CREATE_CONTROLLER_FUNCTION pLoadFunction = (FREE_CREATE_CONTROLLER_FUNCTION)itk::DynamicLoader::GetSymbolAddress(lib, "FREECreateController");
00304         FREE_LINK_CENTRAL_FUNCTION pLinkFunction = (FREE_LINK_CENTRAL_FUNCTION)itk::DynamicLoader::GetSymbolAddress(lib, "FREELinkCentralAsDedicated");
00305 
00306         unsigned int iSymbolCount = 0;
00307         if (pListFunction!=NULL) iSymbolCount++;
00308         if (pLoadFunction!=NULL) iSymbolCount++;
00309         if (pLinkFunction!=NULL) iSymbolCount++;
00310 
00311         if (iSymbolCount == 3)
00312                                 { 
00313           int iCtrlCount = 0;
00314           char** pControllerIDs = 0;
00315                                         
00316           TriggerControlEvent(2,"load components ("+fullpath+")...");
00317 
00318           (*pListFunction)(iCtrlCount,pControllerIDs);
00319                                         
00320           for (int index=0; index<iCtrlCount; index++)
00321                                         {
00322             char* pControllerID = pControllerIDs[index];
00323                                                 if (!GetController(std::string(pControllerID)))
00324                                                 {
00325                                                   GenericComponentController* pNewController = (*pLoadFunction)(pControllerID);
00326                                                   if (pNewController)
00327                                                   {
00329                                                           try
00330                                                           {
00331                                                                   ControllerCentral::RegisterController(pNewController,lib,fullpath);
00332                                                                   m_LibraryHandles->push_back(lib);
00333                                                           }
00334                                                           catch (ControllerException& e)
00335                                                           {
00336                   logException.AddEntry("External DLL error: "+std::string(e.what()));
00337                                                           };
00338                                                   }
00339               else logException.AddEntry("External DLL error: Controller missing. LoadController returns no controller for ControllerID:"+std::string(pControllerID));
00340                                           }
00341             else
00342             {
00343               logException.AddEntry("WARNING: Controller "+std::string(pControllerID)+" is already registered. Skip last one.");
00344               TriggerControlEvent(2,"WARNING: Controller "+std::string(pControllerID)+" is already registered. Skip last one.");
00345             }
00346           };
00347 
00348                                         if (pControllerIDs) delete [] pControllerIDs;
00349 
00350           if (!m_bControllersAreExternal)
00351           {
00352             (*pLinkFunction)(m_RegisteredControllers,
00353                              m_LibraryHandles,
00354                              m_fnOnControl,
00355                              m_fnOnBuild,
00356                              m_CtrlCallbacks,
00357                              m_CtrlProgress,
00358                              m_CallbackMutex,
00359                              m_ProgressMutex);
00360           }
00361           else
00362           {
00363             (*pLinkFunction)(m_RegisteredControllers,
00364                              m_LibraryHandles,
00365                              m_fnOnControl,
00366                              m_fnOnBuild,
00367                              m_CtrlCallbacks,
00368                              m_CtrlProgress,
00369                              m_GlobalCallbackMutex,
00370                              m_GlobalProgressMutex);
00371           }
00372                                 }
00373         else if (iSymbolCount>0)
00374         {
00375           //dll seems some how corrupted, because some symbols have been found, but not all
00376           //needed to load controllers properly.
00377           if ( !pListFunction)
00378           {
00379             logException.AddEntry("External DLL error: DLL seems to be invalid because FREEGetControllerList symbol is missing.");
00380             TriggerControlEvent(2,"!!! WARNING: Invalid DLL ("+fullpath+"); FREEGetControllerList symbol is missing.");          
00381           }
00382           
00383           if ( !pLoadFunction)
00384           {
00385             logException.AddEntry("External DLL error: DLL seems to be invalid because FREECreateController symbol is missing.");
00386             TriggerControlEvent(2,"!!! WARNING: Invalid DLL ("+fullpath+"); FREECreateController symbol is missing.");          
00387           }
00388           
00389           if ( !pLinkFunction)
00390           {
00391             logException.AddEntry("External DLL error: DLL seems to be invalid because FREELinkCentralAsDedicated symbol is missing.");
00392             TriggerControlEvent(2,"!!! WARNING: Invalid DLL ("+fullpath+"); FREELinkCentralAsDedicated symbol is missing.");          
00393           }
00394         };
00395                         };
00396                 };
00397         };
00398 };
00399 
00400 
00404 void
00405 ControllerCentral::
00406 ReHash()
00407 {
00408   //After rehashing the central isn't dedicated any more, so
00409   //remove the pointer to the external event lists.
00410   m_ProgressMutex->Lock();
00411   if (m_bControllersAreExternal)
00412   {
00413     m_GlobalProgressMutex = 0;
00414     m_CtrlProgress = 0;
00415   }
00416   m_ProgressMutex->Unlock();
00417 
00418   m_CallbackMutex->Lock();
00419   if (m_bControllersAreExternal)
00420   {
00421     m_GlobalCallbackMutex = 0;
00422     m_CtrlCallbacks = 0;
00423   }
00424   m_CallbackMutex->Unlock();
00425 
00426 
00427   ControllerCentral::UnRegisterAllControllers();
00428   ControllerCentral::Initialize();
00429 }
00430 
00431 
00435 ControllerCentral::
00436 ControllerCentral()
00437 {
00438   ControllerCentral::Initialize();
00439 }
00440 
00441 
00445 ControllerCentral::
00446 ~ControllerCentral()
00447 {
00448 }
00449 
00450 void
00451 ControllerCentral::
00452 RegisterController(GenericComponentController* pController, 
00453                    const LibraryHandleType& handle,
00454                    const std::string& sLibraryFile)
00455 {
00456   if (!pController) return;
00457 
00458   if (!m_RegisteredControllers) Initialize();
00459   if (!GetController(pController->ControllerID()))
00460   {
00461     ControllerInfo* pNewInfo = new ControllerInfo(pController,handle,sLibraryFile);
00462           m_RegisteredControllers->push_back(pNewInfo);
00463                 TriggerControlEvent(1, pController->ControllerID() + " added", pController);
00464   }
00465         else
00466   {
00467     logException.AddEntry("WARNING: Try to register a controller that is already registered. ControllerID: "+std::string(pController->ControllerID()));
00468     TriggerControlEvent(2,"WARNING: Try to register a controller that is already registered. ControllerID: "+std::string(pController->ControllerID()));
00469   }
00470 }
00471 
00472 void
00473 ControllerCentral::
00474 RegisterController(GenericComponentController* pController)
00475 {
00476   if (!pController) return;
00477 
00478   if (!m_RegisteredControllers) Initialize();
00479   if (!GetController(pController->ControllerID()))
00480   {
00481     ControllerInfo* pNewInfo = new ControllerInfo(pController);
00482           m_RegisteredControllers->push_back(pNewInfo);
00483                 TriggerControlEvent(1, pController->ControllerID() + " added", pController);
00484   }
00485         else
00486   {
00487     logException.AddEntry("WARNING: Try to register a controller that is already registered. ControllerID: "+std::string(pController->ControllerID()));
00488     TriggerControlEvent(2,"WARNING: Try to register a controller that is already registered. ControllerID: "+std::string(pController->ControllerID()));
00489   }
00490 }
00491 
00492 void
00493 ControllerCentral::
00494 UnRegisterController(GenericComponentController* pController)
00495 { 
00496   try
00497   {
00498     if (!m_bControllersAreExternal)
00499     {
00500       for (ControllerListType::iterator pos = m_RegisteredControllers->begin(); pos != m_RegisteredControllers->end(); pos++)
00501       {
00502         if ((*pos)->GetController()==pController)
00503         {
00504           if ((*pos)->IsOwnedByLibrary())
00505           {//Library must delete controller, because DLL may have been built with other runtime
00506             FREE_DELETE_CONTROLLER_FUNCTION pDelFunction = (FREE_DELETE_CONTROLLER_FUNCTION)itk::DynamicLoader::GetSymbolAddress((*pos)->GetLibraryHandle(), "FREEDeleteController");
00507 
00508             if ( !pDelFunction)
00509             {
00510               logException.AddEntry("External DLL error: DLL seems to be invalid because FREEDeleteController symbol is missing.");
00511               TriggerControlEvent(2,"!!! WARNING: Invalid DLL; FREEDeleteController symbol is missing. Cannot delete controller \""+(*pos)->GetControllerID()+"\".");          
00512             }
00513 
00514             (*pDelFunction)((*pos)->GetController());
00515           }
00516           else
00517           {//controller was build in this DLL/programm so delete directly
00518             delete pController;
00519           }
00520 
00521           //delete controller info
00522           delete (*pos);
00523 
00524           //remove entry from list
00525           m_RegisteredControllers->erase(pos);
00526         }
00527       }
00528     }
00529   }
00530   catchAllNPassStaticMacro("Unknown exception, while unregistering controller. ControllerID: " << pController->ControllerID());
00531 }
00532   
00533 void
00534 ControllerCentral::
00535 UnRegisterAllControllers()
00536 {
00537   std::string  sControllerName;
00538 
00539   if (!m_bControllersAreExternal)
00540   {
00541 
00542     try
00543     {
00544                   if (m_RegisteredControllers)
00545       {
00546         while ( m_RegisteredControllers->size()>0 )
00547         {
00548           sControllerName = "Unknown";
00549 
00550           ControllerInfo* pInfo = (*m_RegisteredControllers)[0];
00551 
00552           sControllerName = pInfo->GetControllerID();
00553           
00554           m_RegisteredControllers->erase(m_RegisteredControllers->begin());
00555 
00556           if (pInfo->IsOwnedByLibrary())
00557           {//Library must delete controller, because DLL may have been built with other runtime
00558             FREE_DELETE_CONTROLLER_FUNCTION pDelFunction = (FREE_DELETE_CONTROLLER_FUNCTION)itk::DynamicLoader::GetSymbolAddress(pInfo->GetLibraryHandle(), "FREEDeleteController");
00559 
00560             if ( !pDelFunction)
00561             {
00562               logException.AddEntry("External DLL error: DLL seems to be invalid because FREEDeleteController symbol is missing.");
00563               TriggerControlEvent(2,"!!! WARNING: Invalid DLL; FREEDeleteController symbol is missing. Cannot delete controller \""+pInfo->GetControllerID()+"\".");          
00564             }
00565 
00566             (*pDelFunction)(pInfo->GetController());
00567           }
00568           else
00569           {//controller was build in this DLL/programm so delete directly
00570             delete pInfo->GetController();
00571           }
00572 
00573           //delete controller info
00574           delete pInfo;
00575         }
00576 
00577         delete m_RegisteredControllers;
00578       }
00579     }
00580     catchAllNPassStaticMacro("Unknown exception, while unregistering all controllers." << sControllerName);
00581 
00582     try
00583     {
00584       if (m_LibraryHandles)
00585       {
00586         while (m_LibraryHandles->size() > 0)
00587         {
00588           itk::DynamicLoader::CloseLibrary((*m_LibraryHandles)[0]);
00589           m_LibraryHandles->erase(m_LibraryHandles->begin());
00590         }
00591 
00592         delete m_LibraryHandles;
00593       }
00594     }
00595     catchAllNPassStaticMacro("Unknown exception while closing all all libraries.");
00596   }
00597 
00598   m_bControllersAreExternal = false;
00599   m_RegisteredControllers = NULL;
00600   m_LibraryHandles = NULL;
00601 };
00602 
00603 void
00604 ControllerCentral::
00605 UnRegisterCallbackEventList()
00606 {
00607   if(m_CallbackMutex.IsNotNull()) m_CallbackMutex->Lock();
00608     if (!m_bControllersAreExternal)
00609     {
00610       try
00611       {
00612         if (m_CtrlCallbacks)
00613         {
00614           delete m_CtrlCallbacks;
00615         }
00616       }
00617       catchAllNPassStaticMacro("Unknown exception while deleting callback list.");
00618     }
00619 
00620     m_CtrlCallbacks = NULL;
00621   if(m_CallbackMutex.IsNotNull())   m_CallbackMutex->Unlock();
00622 };
00623 
00624 void
00625 ControllerCentral::
00626 UnRegisterProgressEventList()
00627 {
00628   if(m_ProgressMutex.IsNotNull()) m_ProgressMutex->Lock();
00629 
00630     if (!m_bControllersAreExternal)
00631     {
00632       try
00633       {
00634         if (m_CtrlProgress)
00635         {
00636           delete m_CtrlProgress;
00637         }
00638       }
00639       catchAllNPassStaticMacro("Unknown exception while deleting progress event list.");
00640     }
00641 
00642     m_CtrlProgress = NULL;
00643 
00644   if(m_ProgressMutex.IsNotNull()) m_ProgressMutex->Unlock();
00645 };
00646 
00647 ControllerCentral::ControllerListType
00648 ControllerCentral::
00649 GetControllers()
00650 {
00651   if (!m_RegisteredControllers) Initialize();
00652 
00653   return *m_RegisteredControllers;
00654 };
00655 
00656 GenericComponentController*
00657 ControllerCentral::
00658 GetController(const std::string&  sControllerName)
00659 {
00660   ControllerInfo* pInfo = GetControllerInfo(sControllerName);
00661 
00662   if (pInfo) return pInfo->GetController();
00663 
00664   return NULL;
00665 };
00666 
00667 ControllerInfo*
00668 ControllerCentral::
00669 GetControllerInfo(const std::string&  sControllerName)
00670 {
00671   if (!m_RegisteredControllers) Initialize();
00672 
00673   for( ControllerListType::iterator pos = m_RegisteredControllers->begin(); pos != m_RegisteredControllers->end(); pos++)
00674   {
00675     if ((*pos)->GetControllerID()==sControllerName)
00676     {
00677       return (*pos);
00678     }
00679   }
00680   return NULL;
00681 };
00682 
00683 GenericComponentController*
00684 ControllerCentral::
00685 GetResponsibleController(const GenericComponentType* pComponent)
00686 {
00687   if (!pComponent) return 0;
00688   if (!m_RegisteredControllers) Initialize();
00689 
00690   for( ControllerListType::iterator pos = m_RegisteredControllers->begin(); pos != m_RegisteredControllers->end(); pos++)
00691   {
00692     if ((*pos)->GetController()->ControllerIsResponsible(pComponent)) return (*pos)->GetController();
00693   }
00694   
00695   return 0;
00696 };
00697 
00698 void
00699 ControllerCentral::
00700 SetOnControlEvent(ProgressEventBase* pOnControl)
00701 {
00702         m_fnOnControl = pOnControl;
00703 };
00704 
00705 void
00706 ControllerCentral::
00707 SetOnBuildEvent(ProgressEventBase* pOnBuild)
00708 {
00709         m_fnOnBuild = pOnBuild;
00710 };
00711 
00712 
00713 void
00714 ControllerCentral::
00715 AddOnProgressEvent(ProgressCtrlEventBase* pOnProgress)
00716 {
00717   m_ProgressMutex->Lock();
00718     if (m_GlobalProgressMutex) m_GlobalProgressMutex->Lock();
00719       if ((!m_bControllersAreExternal)&&(!m_CtrlProgress)) m_CtrlProgress = new CtrlProgressListType;
00720 
00721       if (m_CtrlProgress)
00722       {
00723         m_CtrlProgress->push_back(ProgressCtrlEventBase::Pointer(pOnProgress));
00724       }
00725     if (m_GlobalProgressMutex) m_GlobalProgressMutex->Unlock();
00726   m_ProgressMutex->Unlock();
00727 };
00728 
00729 void
00730 ControllerCentral::
00731 RemoveOnProgressEvent(ProgressCtrlEventBase* pOnProgress)
00732 {
00733   m_ProgressMutex->Lock();
00734 
00735   if (m_CtrlProgress)
00736   {
00737     if (m_GlobalProgressMutex) m_GlobalProgressMutex->Lock();
00738 
00739     CtrlProgressListType::iterator pos = m_CtrlProgress->begin();
00740     while (pos != m_CtrlProgress->end())
00741     { //roam through the whole list and remove all entries of the observer
00742       if ((*pos).GetPointer()==pOnProgress)
00743       {
00744         pos = m_CtrlProgress->erase(pos);
00745       }
00746       else
00747       {
00748         ++pos;
00749       }
00750     }
00751 
00752     if (m_GlobalProgressMutex) m_GlobalProgressMutex->Unlock();
00753   }
00754   m_ProgressMutex->Unlock();
00755 };
00756 
00757 void
00758 ControllerCentral::
00759 ClearProgressEventList()
00760 {
00761   m_ProgressMutex->Lock();
00762 
00763   if (m_CtrlProgress)
00764   {
00765     if (m_GlobalProgressMutex) m_GlobalProgressMutex->Lock();
00766       m_CtrlProgress->clear();
00767     if (m_GlobalProgressMutex) m_GlobalProgressMutex->Unlock();
00768   }
00769 
00770   m_ProgressMutex->Unlock();
00771 };
00772 
00773 void
00774 ControllerCentral::
00775 AddOnCallbackEvent(CallbackCtrlEventBase* pOnCallback)
00776 {
00777   m_CallbackMutex->Lock();
00778     if (m_GlobalCallbackMutex) m_GlobalCallbackMutex->Lock();
00779 
00780       if ((!m_bControllersAreExternal)&&(!m_CtrlCallbacks))
00781       {
00782         m_CtrlCallbacks = new CtrlCallbackListType;
00783       }
00784 
00785       if (m_CtrlCallbacks)
00786       {
00787         m_CtrlCallbacks->push_back(CallbackCtrlEventBase::Pointer(pOnCallback));
00788       }
00789 
00790     if (m_GlobalCallbackMutex) m_GlobalCallbackMutex->Unlock();
00791 
00792   m_CallbackMutex->Unlock();
00793 };
00794 
00795 void
00796 ControllerCentral::
00797 RemoveOnCallbackEvent(CallbackCtrlEventBase* pOnCallback)
00798 {
00799   m_CallbackMutex->Lock();
00800 
00801   if (m_CtrlCallbacks)
00802   {
00803     if (m_GlobalCallbackMutex) m_GlobalCallbackMutex->Lock();
00804 
00805     CtrlCallbackListType::iterator pos = m_CtrlCallbacks->begin();
00806     while (pos != m_CtrlCallbacks->end())
00807     { //roam through the whole list and remove all entries of the observer
00808       if ((*pos).GetPointer()==pOnCallback)
00809       {
00810         pos = m_CtrlCallbacks->erase(pos);
00811       }
00812       else
00813       {
00814         ++pos;
00815       }
00816     }
00817 
00818     if (m_GlobalCallbackMutex) m_GlobalCallbackMutex->Unlock();
00819   }
00820 
00821   m_CallbackMutex->Unlock();
00822 };
00823 
00824 void
00825 ControllerCentral::
00826 ClearCallbackList()
00827 {
00828   m_CallbackMutex->Lock();
00829 
00830   if (m_CtrlCallbacks)
00831   {
00832     if (m_GlobalCallbackMutex) m_GlobalCallbackMutex->Lock();
00833       m_CtrlCallbacks->clear();
00834     if (m_GlobalCallbackMutex) m_GlobalCallbackMutex->Unlock();
00835   }
00836 
00837   m_CallbackMutex->Unlock();
00838 };
00839 
00840 bool
00841 ControllerCentral::
00842 TriggerControlEvent(const int iStatusID, const std::string& sComment, void* pSender, long threadID)
00843 {
00844   if (m_fnOnControl.IsNotNull())
00845   {
00846     m_fnOnControl->Execute(iStatusID, sComment, pSender, threadID);
00847     return true;
00848   }
00849   return false;
00850 };
00851 
00852 bool
00853 ControllerCentral::
00854 TriggerBuildEvent(const int iStatusID, const std::string& sComment, void* pSender, long threadID)
00855 {
00856   if (m_fnOnBuild.IsNotNull())
00857   {
00858     m_fnOnBuild->Execute(iStatusID, sComment, pSender, threadID);
00859     return true;
00860   }
00861   return false;
00862 };
00863 
00864 void
00865 ControllerCentral::
00866 TriggerControllerProgressEvent(const IDPath& senderID, const ProgressCtrlEventBase::StatusID status,
00867                                              const std::string& sComment,
00868                                              SessionComponentCache* pSender,
00869                                              long threadID)
00870 {
00871   m_ProgressMutex->Lock();
00872 
00873   if (m_CtrlProgress)
00874   {
00875     if (m_GlobalProgressMutex) m_GlobalProgressMutex->Lock();
00876 
00877     //check all limited events
00878     for (CtrlProgressListType::iterator pos = m_CtrlProgress->begin(); pos != m_CtrlProgress->end(); pos++)
00879     { //
00880       if ((*pos)->GetACR())
00881       {
00882         (*pos)->Execute(senderID,status,sComment,pSender,threadID);
00883       }
00884     }
00885     //now all non limited events
00886     for (CtrlProgressListType::iterator pos = m_CtrlProgress->begin(); pos != m_CtrlProgress->end(); pos++)
00887     { //
00888       if (!(*pos)->GetACR())
00889       {
00890         (*pos)->Execute(senderID,status,sComment,pSender,threadID);
00891       }
00892     }
00893 
00894     if (m_GlobalProgressMutex) m_GlobalProgressMutex->Unlock();
00895   }
00896 
00897   m_ProgressMutex->Unlock();
00898 };
00899 
00900 bool
00901 ControllerCentral::
00902 TriggerControllerCallback(const IDPath& senderID, CallbackCtrlEventBase::CallbackType callbackID,
00903                                         void* pData, SessionComponentCache* pSender, long threadID)
00904 {
00905   bool result = false;
00906 
00907   m_CallbackMutex->Lock();
00908 
00909   if (m_CtrlCallbacks)
00910   {
00911     if (m_GlobalProgressMutex) m_GlobalProgressMutex->Lock();
00912 
00913       //check all limited events
00914       for (CtrlCallbackListType::iterator pos = m_CtrlCallbacks->begin(); pos != m_CtrlCallbacks->end(); pos++)
00915       { //
00916         if ((*pos)->GetACR())
00917         {
00918           if ((*pos)->Execute(senderID,callbackID,pData,pSender,threadID))
00919           {
00920             result = true;
00921             break;
00922           }
00923         }
00924       }
00925       //now all non limited events
00926       for (CtrlCallbackListType::iterator pos = m_CtrlCallbacks->begin(); pos != m_CtrlCallbacks->end(); pos++)
00927       { //
00928         if (!(*pos)->GetACR())
00929         {
00930           if ((*pos)->Execute(senderID,callbackID,pData,pSender,threadID))
00931           {
00932             result = true;
00933             break;
00934           }
00935         }
00936       }
00937 
00938     if (m_GlobalProgressMutex) m_GlobalProgressMutex->Unlock();
00939   }
00940 
00941   m_CallbackMutex->Unlock();
00942 
00943   return result;
00944 };
00945 
00946 } //end of namspace free

Generated at Sat Oct 13 15:39:55 2007 for f.r.e.e. - Flexible Registration and Evaluation Engine by doxygen 1.5.3 written by Dimitri van Heesch, © 1997-2000