00001 // -*- C++ -*- 00020 #ifndef RTC_MODULEMANAGER_H 00021 #define RTC_MODULEMANAGER_H 00022 00023 // STL includes 00024 #include <string> 00025 #include <vector> 00026 #include <map> 00027 00028 // ACE includes 00029 #include <coil/DynamicLib.h> 00030 00031 // RTC includes 00032 #include <rtm/Manager.h> 00033 #include <coil/Properties.h> 00034 #include <rtm/ObjectManager.h> 00035 00036 00037 #define CONFIG_EXT "manager.modules.config_ext" 00038 #define CONFIG_PATH "manager.modules.config_path" 00039 #define DETECT_MOD "manager.modules.detect_loadable" 00040 #define MOD_LOADPTH "manager.modules.load_path" 00041 #define INITFUNC_SFX "manager.modules.init_func_suffix" 00042 #define INITFUNC_PFX "manager.modules.init_func_prefix" 00043 #define ALLOW_ABSPATH "manager.modules.abs_path_allowed" 00044 #define ALLOW_URL "manager.modules.download_allowed" 00045 #define MOD_DWNDIR "manager.modules.download_dir" 00046 #define MOD_DELMOD "manager.modules.download_cleanup" 00047 #define MOD_PRELOAD "manager.modules.preload" 00048 00049 #ifdef WIN32 00050 #pragma warning( disable : 4290 ) 00051 #endif 00052 00053 namespace RTC 00054 { 00074 class ModuleManager 00075 { 00076 public: 00098 ModuleManager(coil::Properties& prop); 00099 00111 ~ModuleManager(void); 00112 00120 struct Error 00121 { 00122 Error(const std::string& _reason) 00123 : reason(_reason) {} 00124 std::string reason; 00125 }; 00126 00135 struct NotFound 00136 { 00137 NotFound(const std::string& _name) 00138 : name(_name) {} 00139 std::string name; 00140 }; 00141 00150 struct FileNotFound 00151 : public NotFound 00152 { 00153 FileNotFound(const std::string& _name) 00154 : NotFound(_name) {} 00155 }; 00156 00165 struct ModuleNotFound 00166 : public NotFound 00167 { 00168 ModuleNotFound(const std::string& _name) 00169 : NotFound(_name) {} 00170 }; 00171 00180 struct SymbolNotFound 00181 : public NotFound 00182 { 00183 SymbolNotFound(const std::string& _name) 00184 : NotFound(_name) {} 00185 }; 00186 00195 struct NotAllowedOperation 00196 : public Error 00197 { 00198 NotAllowedOperation(const std::string& _reason) 00199 : Error(_reason) {} 00200 }; 00201 00210 struct InvalidArguments 00211 : public Error 00212 { 00213 InvalidArguments(const std::string& _reason) 00214 : Error(_reason) {} 00215 }; 00216 00225 struct InvalidOperation 00226 : public Error 00227 { 00228 InvalidOperation(const std::string& _reason) 00229 : Error(_reason) {} 00230 }; 00231 typedef void (*ModuleInitFunc)(Manager*); 00232 00278 std::string load(const std::string& file_name); 00279 00307 std::string load(const std::string& file_name, const std::string& init_func); 00308 00326 void unload(const std::string& file_name); 00327 00341 void unloadAll(); 00342 00350 void* symbol(const std::string& file_name, const std::string& func_name) 00351 throw (ModuleNotFound, SymbolNotFound); 00352 00370 void setLoadpath(const std::vector<std::string>& load_path); 00371 00389 inline std::vector<std::string> getLoadPath() 00390 { 00391 return m_loadPath; 00392 } 00393 00411 void addLoadpath(const std::vector<std::string>& load_path); 00412 00430 std::vector<coil::Properties> getLoadedModules(); 00431 00450 std::vector<coil::Properties> getLoadableModules(); 00451 00465 inline void allowAbsolutePath() 00466 { 00467 m_absoluteAllowed = true; 00468 } 00469 00483 inline void disallowAbsolutePath() 00484 { 00485 m_absoluteAllowed = false; 00486 } 00487 00505 inline void allowModuleDownload() 00506 { 00507 m_downloadAllowed = true; 00508 } 00509 00523 inline void disallowModuleDownload() 00524 { 00525 m_downloadAllowed = false; 00526 } 00527 00551 std::string findFile(const std::string& fname, 00552 const std::vector<std::string>& load_path); 00553 00575 bool fileExist(const std::string& filename); 00576 00598 std::string getInitFuncName(const std::string& file_path); 00599 00600 protected: 00608 Logger rtclog; 00609 00617 struct DLLEntity 00618 { 00619 coil::Properties properties; 00620 coil::DynamicLib dll; 00621 }; 00622 00623 typedef std::vector<std::string> StringVector; 00624 typedef StringVector::iterator StringVectorItr; 00625 typedef StringVector::const_iterator StringVectorConstItr; 00626 00627 typedef std::vector<DLLEntity> DllMap; 00628 typedef DllMap::iterator DllMapItr; 00629 typedef DllMap::const_iterator DllMapConstItr; 00630 00638 coil::Properties& m_properties; 00639 00647 class DllPred 00648 { 00649 std::string m_filepath; 00650 public: 00651 DllPred(const char* filepath) : m_filepath(filepath) {} 00652 DllPred(const DLLEntity* dll) : m_filepath(dll->properties["file_path"]) {} 00653 bool operator()(DLLEntity* dllentity) 00654 { 00655 return m_filepath == dllentity->properties.getProperty("file_path"); 00656 } 00657 }; 00665 // DllMap m_modules; 00666 ObjectManager<const char*, DLLEntity, DllPred> m_modules; 00667 00675 StringVector m_loadPath; 00683 StringVector m_configPath; 00691 bool m_downloadAllowed; 00699 bool m_absoluteAllowed; 00700 00708 std::string m_initFuncSuffix; 00716 std::string m_initFuncPrefix; 00717 00718 class UnloadPred 00719 { 00720 public: 00721 UnloadPred(){} 00722 void operator()(DLLEntity* dll) 00723 { 00724 dll->dll.close(); 00725 delete dll; 00726 } 00727 }; 00728 00729 }; // class ModuleManager 00730 }; // namespace RTC 00731 00732 #ifdef WIN32 00733 #pragma warning( default : 4290 ) 00734 #endif 00735 00736 #endif // RTC_MODULEMANAGER_H