VRUT::ModuleHandle< T > Class Template Reference

Data container and management of dynamic library with module instances. More...

#include <modulehandle.h>

List of all members.

Public Member Functions

 ModuleHandle (MODULE_HANDLE_ID _hid, SERVER_ID srvID, const wxFileName &fname, MODULE_INSTANCE_ID instOffset=0)
 Class constructor.
 ~ModuleHandle ()
 Class destructor.
MODULE_HANDLE_ID GetID () const
 Get module handle ID.
bool Load ()
 Load associated dynamic library.
MODULE_INSTANCE_ID GetInstanceID (const T *module) const
 Get module's MODULE_INSTANCE_ID or MODULE_INSTANCE_ID_NONE if not found.
void Release (MODULE_INSTANCE_ID id=MODULE_INSTANCE_ID_NONE)
bool IsLoaded () const
 Is dynamic library loaded?
const wxFileName & GetFileName () const
 Get file information.
const wxString & GetName () const
 Get handled module name, return empty string if no module instantiated yet.
MODULE_INSTANCE_ID InstantiateModule ()
 Create new module instance and get its id.
T * GetModule (MODULE_INSTANCE_ID id) const
 Get module instance, must be instantiated first.
void GetModules (ModuleList *moduleList) const
 Get all module instances.

Static Public Member Functions

static bool StopModuleThread (Module *module, bool wait=true)
static void StartModuleThread (Module *module)
 Create and run thread with module.

Protected Attributes

MODULE_HANDLE_ID hid
 Module handle ID.
SERVER_ID serverID
 Server owner ID.
wxFileName fileName
 File information of dynamic library.
wxString moduleName
 Handled module name.
wxDynamicLibrary dlib
 Dynamic library.
std::vector< T * > moduleInstances
 Module instances list.
unsigned numActModuleInstances
 Number of active module instances.


Detailed Description

template<typename T>
class VRUT::ModuleHandle< T >

Data container and management of dynamic library with module instances.

Definition at line 39 of file modulehandle.h.


Constructor & Destructor Documentation

template<typename T>
VRUT::ModuleHandle< T >::ModuleHandle ( MODULE_HANDLE_ID  _hid,
SERVER_ID  srvID,
const wxFileName &  fname,
MODULE_INSTANCE_ID  instOffset = 0 
) [inline]

Class constructor.

Definition at line 59 of file modulehandle.h.

00060                             : hid(_hid), serverID(srvID), fileName(fname)
00061               {
00062                      if (instOffset)
00063                             moduleInstances.resize(instOffset);
00064                      numActModuleInstances = 0;
00065               }

template<typename T>
VRUT::ModuleHandle< T >::~ModuleHandle (  )  [inline]

Class destructor.

Definition at line 67 of file modulehandle.h.

00068               {
00069                      Release();
00070                      wxASSERT_MSG(numActModuleInstances == 0, wxT("<ModuleHandle>Not all module instances released"));
00071               }


Member Function Documentation

template<typename T>
MODULE_HANDLE_ID VRUT::ModuleHandle< T >::GetID (  )  const [inline]

Get module handle ID.

Definition at line 74 of file modulehandle.h.

00075               {
00076                      return hid;
00077               }

template<typename T>
bool VRUT::ModuleHandle< T >::Load (  )  [inline]

Load associated dynamic library.

Definition at line 80 of file modulehandle.h.

00081               {
00082                      if (dlib.IsLoaded())
00083                             return true;
00084 
00085                      bool ret = dlib.Load(fileName.GetFullPath());
00086                      if (ret)
00087                             LOG(wxT("<ModuleHandle>Loaded '") + fileName.GetFullPath() + wxT("'"));
00088                      return ret;
00089               }

template<typename T>
MODULE_INSTANCE_ID VRUT::ModuleHandle< T >::GetInstanceID ( const T *  module  )  const [inline]

Get module's MODULE_INSTANCE_ID or MODULE_INSTANCE_ID_NONE if not found.

Definition at line 92 of file modulehandle.h.

00093               {
00094                      typename std::vector<T *>::const_iterator it;
00095                      MODULE_INSTANCE_ID id = 0;
00096                      for (it = moduleInstances.begin(); it != moduleInstances.end(); it++, id++)
00097                             if (*it == module)
00098                                    return id;
00099                      return MODULE_INSTANCE_ID_NONE;
00100               }

template<typename T>
void VRUT::ModuleHandle< T >::Release ( MODULE_INSTANCE_ID  id = MODULE_INSTANCE_ID_NONE  )  [inline]

Release module instance and unload library if no instances left

Parameters:
[in] id Module instance to release, release all if not defined (default)

Be sure there is no memory allocated on module memory heap here

Definition at line 104 of file modulehandle.h.

00105               {
00106                      if (dlib.IsLoaded())
00107                      {
00108                             RELEASE_INSTANCE_FUNC_PROTO release = RELEASE_INSTANCE_FUNC_PROTO(dlib.GetSymbol(wxT("RELEASE_INSTANCE_FUNC")));
00109                             if (release == (RELEASE_INSTANCE_FUNC_PROTO)NULL)
00110                             {
00111                                    wxString errStr = wxT("<ModuleHandle>Could not release module instance,");
00112                                    errStr << wxString::Format(wxT(" library does not provide release function, module '%s' is corrupt"), fileName.GetFullName().c_str());
00113                                    LOGERROR(errStr);
00114                                    return;
00115                             }
00116 
00117                             if (id != MODULE_INSTANCE_ID_NONE)
00118                             {
00119                                    if (id < moduleInstances.size() && moduleInstances[id])
00120                                    {
00121                                           if (!StopModuleThread(moduleInstances[id]))
00122                                                  LOGWARNING(wxT("<ModuleHandle>Module has not finished in time, forcing release"));
00123                                           Parameter::ParameterIdentificator pi(moduleInstances[id]->GetName(), wxT("*"), id);
00124                                           wxCommandEvent unregEvt = Event::GET_EVT_PARAM_UNREGISTER(pi);
00125                                           KERNEL->GetMessageSink()->PostEvent(unregEvt);
00126                                           wxCommandEvent gunregEvt = Event::GET_EVT_PARAM_GUIUNREGISTER(pi);
00127                                           KERNEL->GetMessageSink()->PostEvent(gunregEvt);
00128                                           release(moduleInstances[id]);
00129                                           moduleInstances[id] = (T *)NULL;
00130                                           numActModuleInstances--;
00131                                    }
00132                             }
00133                             else
00134                             {
00135                                    wxString modName;
00136                                    for (typename std::vector<T *>::iterator it = moduleInstances.begin(); it != moduleInstances.end(); it++)
00137                                    {
00138                                           if (*it)
00139                                           {
00140                                                  modName = CloneWxString((*it)->GetName());
00141                                                  if (!StopModuleThread(*it))
00142                                                         LOGWARNING(wxT("<ModuleHandle>Module has not finished in time, forcing release"));
00143                                                  release(*it);
00144                                                  *it = (T *)NULL;
00145                                           }
00146                                    }
00147                                    numActModuleInstances = 0;
00148                                    Parameter::ParameterIdentificator pi(modName);
00149                                    wxCommandEvent unregEvt = Event::GET_EVT_PARAM_UNREGISTER(pi);
00150                                    KERNEL->GetMessageSink()->PostEvent(unregEvt);
00151                                    wxCommandEvent gunregEvt = Event::GET_EVT_PARAM_GUIUNREGISTER(pi);
00152                                    KERNEL->GetMessageSink()->PostEvent(gunregEvt);
00153                             }
00154 
00155                             if (numActModuleInstances == 0)
00156                             {
00158                                    KERNEL->GetMessageSink()->ProcessEventsFrom(hid);
00159                                    dlib.Unload();
00160                                    LOG(wxT("<ModuleHandle>Unloaded '") + fileName.GetFullPath() + wxT("'"));
00161                             }
00162                      }
00163               }

template<typename T>
bool VRUT::ModuleHandle< T >::IsLoaded (  )  const [inline]

Is dynamic library loaded?

Definition at line 166 of file modulehandle.h.

00167               {
00168                      return dlib.IsLoaded();
00169               }

template<typename T>
const wxFileName& VRUT::ModuleHandle< T >::GetFileName (  )  const [inline]

Get file information.

Definition at line 172 of file modulehandle.h.

00173               {
00174                      return fileName;
00175               }

template<typename T>
const wxString& VRUT::ModuleHandle< T >::GetName (  )  const [inline]

Get handled module name, return empty string if no module instantiated yet.

Definition at line 178 of file modulehandle.h.

00179               {
00180                      return moduleName;
00181               }

template<typename T>
MODULE_INSTANCE_ID VRUT::ModuleHandle< T >::InstantiateModule (  )  [inline]

Create new module instance and get its id.

Definition at line 184 of file modulehandle.h.

00185               {
00186                      if (!Load())
00187                             return MODULE_INSTANCE_ID_NONE;
00188 
00189                      CREATE_INSTANCE_FUNC_PROTO instantiate = CREATE_INSTANCE_FUNC_PROTO(dlib.GetSymbol(wxT("CREATE_INSTANCE_FUNC")));
00190                      if (instantiate == (CREATE_INSTANCE_FUNC_PROTO)NULL)
00191                      {
00192                             wxString errStr = wxT("<ModuleHandle>Could not instantiate module,");
00193                             errStr << wxString::Format(wxT(" library does not provide create function, module '%s' is corrupt"), fileName.GetFullName().c_str());
00194                             LOGERROR(errStr);
00195                             return MODULE_INSTANCE_ID_NONE;
00196                      }
00197 
00198                      int ver = MODULES_VER_SUPP;
00199                      T * module = (T *)NULL;
00200                      MODULE_ID modID(serverID, hid, MODULE_INSTANCE_ID(moduleInstances.size()));
00201                      instantiate(&ver, (void **)&module, KERNEL->GetMessageSink(), modID);
00202                      if (!module)
00203                      {
00204                             if (ver != MODULES_VER_SUPP)
00205                                    LOGERROR(wxT("<ModuleHandle>Module version incompatible, update needed"));
00206                             else
00207                                    LOGERROR(wxT("<ModuleHandle>Module instance creation failed"));
00208                             return MODULE_INSTANCE_ID_NONE;
00209                      }
00210                      else
00211                      {
00212                             wxLog * logInst = FlexiLog::GetInstance();
00213                             if (logInst)
00214                             {
00215                                    wxCommandEvent ev = Event::GET_EVT_LOG_SET(logInst);
00216                                    module->PostEvent(ev, true);
00217                                    wxCommandEvent ev2 = Event::GET_EVT_LOG_LEVEL_SET(logInst->GetLogLevel());
00218                                    module->PostEvent(ev2, true);
00219                             }
00220 
00221                             if (moduleInstances.empty())
00222                                    LOGVERBOSE(wxString(wxT("<ModuleHandle>Module description: ")) + module->GetDesc());
00223                             LOGVERBOSE(wxString::Format(wxT("<ModuleHandle>Module '%s' instantiated: %s"), module->GetName().c_str(), modID.ToString().c_str()));
00224                             moduleInstances.push_back(module);
00225                             moduleName = module->GetName();
00226                             KERNEL->environment.ApplyPendingParams(module, fileName.GetName());
00227                             numActModuleInstances++;
00228                             return modID.GetInstanceID();
00229                      }
00230               }

template<typename T>
T* VRUT::ModuleHandle< T >::GetModule ( MODULE_INSTANCE_ID  id  )  const [inline]

Get module instance, must be instantiated first.

Definition at line 233 of file modulehandle.h.

00234               {
00235                      if (id >= moduleInstances.size())
00236                             return (T *)NULL;
00237 
00238                      return moduleInstances[id];
00239               }

template<typename T>
void VRUT::ModuleHandle< T >::GetModules ( ModuleList moduleList  )  const [inline]

Get all module instances.

Definition at line 242 of file modulehandle.h.

00243               {
00244                      for (size_t it = 0; it < moduleInstances.size(); it++)
00245                             if (moduleInstances[it])
00246                                    moduleList->push_back(ModulePair(moduleInstances[it], MODULE_INSTANCE_ID(it)));
00247               }

template<typename T>
static bool VRUT::ModuleHandle< T >::StopModuleThread ( Module module,
bool  wait = true 
) [inline, static]

Exit module thread

Parameters:
[in] module Module to be stopped
[in] wait Wait for module thread to finish
Returns:
false if module thread has not finished in THREAD_WAIT_TIMEOUT milliseconds

Definition at line 253 of file modulehandle.h.

00254               {
00255                      wxCommandEvent ev = Event::GET_EVT_EXIT();
00256                      module->PostEvent(ev, true);
00257                      if (wait)
00258                      {
00259                             unsigned waiting = 0;
00260                             unsigned sec = 0;
00261                             while (module->IsActive())
00262                             {
00263                                    wxThread::Sleep(THREAD_WAIT_INTERVAL);
00264                                    waiting += THREAD_WAIT_INTERVAL;
00265                                    sec += THREAD_WAIT_INTERVAL;
00266                                    if (waiting >= THREAD_WAIT_TIMEOUT)
00267                                           return false;
00268 
00269                                    if (sec >= 1000)
00270                                    {
00271                                           LOG(wxString::Format(wxT("<ModuleHandle>Waiting %i seconds for '%s (%s)' module's detached thread to finish"), (THREAD_WAIT_TIMEOUT - waiting)/1000, module->GetName().c_str(), module->GetID().ToString().c_str()));
00272                                           sec = 0;
00273                                    }
00274                             }
00275                             wxThread::Sleep(THREAD_WAIT_INTERVAL);
00276                             LOG(wxString::Format(wxT("<ModuleHandle>Module thread finished '%s (%s)'"), module->GetName().c_str(), module->GetID().ToString().c_str()));
00277                      }
00278                      return true;
00279               }

template<typename T>
static void VRUT::ModuleHandle< T >::StartModuleThread ( Module module  )  [inline, static]

Create and run thread with module.

Definition at line 282 of file modulehandle.h.

00283               {
00284                      if (module)
00285                      {
00286                             wxThread * thread = new ModuleThread(module);
00287                             wxThreadError err = thread->Create();
00288                             wxASSERT_MSG(err == wxTHREAD_NO_ERROR, wxT("<ModuleHandle>Failed to create thread"));
00289                             err = thread->Run();
00290                             wxASSERT_MSG(err == wxTHREAD_NO_ERROR, wxT("<ModuleHandle>Failed to start thread"));
00291                             LOG(wxString::Format(wxT("<ModuleHandle>Starting module thread '%s (%s)'"), module->GetName().c_str(), module->GetID().ToString().c_str()));
00292                      }
00293               }


Member Data Documentation

template<typename T>
MODULE_HANDLE_ID VRUT::ModuleHandle< T >::hid [protected]

Module handle ID.

Definition at line 43 of file modulehandle.h.

template<typename T>
SERVER_ID VRUT::ModuleHandle< T >::serverID [protected]

Server owner ID.

Definition at line 45 of file modulehandle.h.

template<typename T>
wxFileName VRUT::ModuleHandle< T >::fileName [protected]

File information of dynamic library.

Definition at line 47 of file modulehandle.h.

template<typename T>
wxString VRUT::ModuleHandle< T >::moduleName [protected]

Handled module name.

Definition at line 49 of file modulehandle.h.

template<typename T>
wxDynamicLibrary VRUT::ModuleHandle< T >::dlib [protected]

Dynamic library.

Definition at line 51 of file modulehandle.h.

template<typename T>
std::vector<T *> VRUT::ModuleHandle< T >::moduleInstances [protected]

Module instances list.

Definition at line 53 of file modulehandle.h.

template<typename T>
unsigned VRUT::ModuleHandle< T >::numActModuleInstances [protected]

Number of active module instances.

Definition at line 55 of file modulehandle.h.


The documentation for this class was generated from the following file:

Generated on Tue Mar 10 14:41:44 2009 for VRUT by  doxygen 1.5.5