00001
00020 #ifndef Manager_h
00021 #define Manager_h
00022
00023 #include <rtm/RTC.h>
00024
00025 #include <iostream>
00026 #include <string>
00027 #include <vector>
00028
00029 #include <coil/Mutex.h>
00030 #include <coil/Guard.h>
00031 #include <coil/Task.h>
00032
00033 #include <rtm/Factory.h>
00034 #include <rtm/ECFactory.h>
00035 #include <rtm/ObjectManager.h>
00036 #include <rtm/SystemLogger.h>
00037
00038 namespace RTM
00039 {
00040 class ManagerServant;
00041 }
00042
00043 namespace coil
00044 {
00045 class Timer;
00046 };
00047
00048 namespace RTC
00049 {
00050
00051 class CorbaNaming;
00052 class ModuleManager;
00053 class NamingManager;
00054 class Manager;
00055 class RTObject_impl;
00056 typedef RTObject_impl RtcBase;
00057
00058 typedef void (*ModuleInitProc)(Manager* manager);
00059
00080 class Manager
00081 {
00082 typedef coil::Mutex Mutex;
00083 typedef coil::Guard<Mutex> Guard;
00084 protected:
00098 Manager();
00099
00117 Manager(const Manager& manager);
00118
00119
00120 public:
00173 static Manager* init(int argc, char** argv);
00174
00197 static Manager& instance();
00198
00199
00200
00201
00202
00216 void terminate();
00217
00233 void shutdown();
00234
00248 void join();
00249
00267 LogStreamBuf& getLogStreamBuf() {return m_logStreamBuf;}
00268 std::string& getLogLevel() {return m_config["logger.log_level"];}
00269
00287 coil::Properties& getConfig() { return m_config;}
00288
00313 void setModuleInitProc(ModuleInitProc proc);
00314
00347 bool activateManager();
00348
00379 void runManager(bool no_block = false);
00380
00381
00382
00383
00406 void load(const char* fname, const char* initfunc);
00407
00427 void unload(const char* fname);
00428
00444 void unloadAll();
00445
00463 std::vector<coil::Properties> getLoadedModules();
00464
00484 std::vector<coil::Properties> getLoadableModules();
00485
00486
00487
00488
00515 bool registerFactory(coil::Properties& profile,
00516 RtcNewFunc new_func,
00517 RtcDeleteFunc delete_func);
00518 std::vector<coil::Properties> getFactoryProfiles();
00545 bool registerECFactory(const char* name,
00546 ECNewFunc new_func,
00547 ECDeleteFunc delete_func);
00548
00566 std::vector<std::string> getModulesFactories();
00567
00568
00569
00570
00629 RTObject_impl* createComponent(const char* comp_args);
00630 ExecutionContextBase* createContext(const char* ec_args);
00631
00650 void cleanupComponent(RTObject_impl* comp);
00651
00675 bool registerComponent(RTObject_impl* comp);
00676
00698 bool unregisterComponent(RTObject_impl* comp);
00699
00700
00725 void deleteComponent(const char* instance_name);
00726
00727
00751 RTObject_impl* getComponent(const char* instance_name);
00752
00770 std::vector<RTObject_impl*> getComponents();
00771
00772
00773
00774
00792 CORBA::ORB_ptr getORB();
00793
00811 PortableServer::POA_ptr getPOA();
00812
00830 PortableServer::POAManager_ptr getPOAManager();
00831
00832
00833
00834
00835 protected:
00836
00837
00838
00839
00867 void initManager(int argc, char** argv);
00868
00884 void shutdownManager();
00885
00886
00887
00888
00910 bool initLogger();
00911
00929 void shutdownLogger();
00930
00931
00932
00933
00951 bool initORB();
00952
00972 std::string createORBOptions();
00973
00992 void shutdownORB();
00993
00994
00995
00996
01028 bool initNaming();
01029
01045 void shutdownNaming();
01046
01047
01048
01049
01065 void shutdownComponents();
01066
01067
01096 bool procComponentArgs(const char* comp_arg,
01097 coil::Properties& comp_id,
01098 coil::Properties& comp_conf);
01099 bool procContextArgs(const char* ec_args,
01100 std::string& ec_id,
01101 coil::Properties& ec_conf);
01102
01125 void configureComponent(RTObject_impl* comp, const coil::Properties& prop);
01126
01148 bool initExecContext();
01149 bool initComposite();
01150 bool initFactories();
01151
01171 bool initTimer();
01172
01173 bool initManagerServant();
01174 RTM::ManagerServant* m_mgrservant;
01175
01201 bool mergeProperty(coil::Properties& prop, const char* file_name);
01202
01251 std::string formatString(const char* naming_format,
01252 coil::Properties& prop);
01253
01254
01255
01256
01257
01258
01259
01260
01268 static Manager* manager;
01269
01277 static Mutex mutex;
01278
01279
01280
01281
01289 CORBA::ORB_var m_pORB;
01290
01298 PortableServer::POA_var m_pPOA;
01299
01307 PortableServer::POAManager_var m_pPOAManager;
01308
01309
01310
01311
01319 ModuleInitProc m_initProc;
01320
01328 coil::Properties m_config;
01329
01337 ModuleManager* m_module;
01338
01346 NamingManager* m_namingManager;
01347
01355 coil::Timer* m_timer;
01356
01357
01358
01359
01367 LogStreamBuf m_logStreamBuf;
01368
01376 Logger rtclog;
01377
01378 std::vector<std::ofstream*> m_logfiles;
01379
01380
01381
01382
01383
01384 struct InstanceName
01385 {
01386 InstanceName(RTObject_impl* comp);
01387 InstanceName(const char* name);
01388 InstanceName(const std::string name);
01389 bool operator()(RTObject_impl* comp);
01390 std::string m_name;
01391 };
01392
01393 typedef ObjectManager<std::string, RTObject_impl, InstanceName> ComponentManager;
01394
01402 ComponentManager m_compManager;
01403
01404
01405
01406
01407
01408 class FactoryPredicate
01409 {
01410 public:
01411 FactoryPredicate(const char* imple_id)
01412 : m_vendor(""), m_category(""), m_impleid(imple_id), m_version("")
01413 {
01414 }
01415 FactoryPredicate(const coil::Properties& prop)
01416 : m_vendor(prop["vendor"]),
01417 m_category(prop["category"]),
01418 m_impleid(prop["implementation_id"]),
01419 m_version(prop["version"])
01420 {
01421 }
01422 FactoryPredicate(FactoryBase* factory)
01423 : m_vendor(factory->profile()["vendor"]),
01424 m_category(factory->profile()["category"]),
01425 m_impleid(factory->profile()["implementation_id"]),
01426 m_version(factory->profile()["version"])
01427 {
01428 }
01429 bool operator()(FactoryBase* factory)
01430 {
01431
01432 if (m_impleid.empty()) return false;
01433
01434 const coil::Properties& prop(factory->profile());
01435
01436 if (m_impleid != prop["implementation_id"])
01437 return false;
01438 if (!m_vendor.empty() && m_vendor != prop["vendor"])
01439 return false;
01440 if (!m_category.empty() && m_category != prop["category"])
01441 return false;
01442 if (!m_version.empty() && m_version != prop["version"])
01443 return false;
01444
01445 return true;
01446 }
01447 private:
01448 std::string m_vendor;
01449 std::string m_category;
01450 std::string m_impleid;
01451 std::string m_version;
01452 };
01453
01461 typedef ObjectManager<const coil::Properties, FactoryBase,
01462 FactoryPredicate> FactoryManager;
01463
01471 FactoryManager m_factory;
01472
01473
01474
01475
01476
01477 struct ECFactoryPredicate
01478 {
01479 ECFactoryPredicate(const char* name) : m_name(name){};
01480 ECFactoryPredicate(ECFactoryBase* factory)
01481 : m_name(factory->name()) {};
01482 bool operator()(ECFactoryBase* factory)
01483 {
01484 return m_name == factory->name();
01485 }
01486 std::string m_name;
01487 };
01488 typedef ObjectManager<const char*,
01489 ECFactoryBase,
01490 ECFactoryPredicate> ECFactoryManager;
01491
01499 ECFactoryManager m_ecfactory;
01500
01508 std::vector<ExecutionContextBase*> m_ecs;
01509
01510
01511 struct ModuleFactories
01512 {
01513 void operator()(FactoryBase* f)
01514 {
01515 modlist.push_back(f->profile().getProperty("implementation_id"));
01516 }
01517 std::vector<std::string> modlist;
01518 };
01519
01520
01521
01522
01542 class OrbRunner
01543 : public coil::Task
01544 {
01545 public:
01559 OrbRunner(CORBA::ORB_ptr orb) : m_pORB(orb)
01560 {
01561 open(0);
01562 };
01563
01585 virtual int open(void *args)
01586 {
01587 activate();
01588 return 0;
01589 }
01590
01608 virtual int svc(void)
01609 {
01610 m_pORB->run();
01611
01612 return 0;
01613 }
01614
01636 virtual int close(unsigned long flags)
01637 {
01638 return 0;
01639 }
01640 private:
01641 CORBA::ORB_ptr m_pORB;
01642 };
01643 OrbRunner* m_runner;
01644
01645
01646
01647
01667 class Terminator
01668 : public coil::Task
01669 {
01670 public:
01688 Terminator(Manager* manager) : m_manager(manager) {};
01689
01703 void terminate()
01704 {
01705 open(0);
01706 }
01707
01729 virtual int open(void *args)
01730 {
01731 activate();
01732 return 0;
01733 }
01734
01752 virtual int svc(void)
01753 {
01754 Manager::instance().shutdown();
01755 return 0;
01756 }
01757 Manager* m_manager;
01758 };
01759
01767 Terminator* m_terminator;
01768
01769 struct Term
01770 {
01771 int waiting;
01772 Mutex mutex;
01773 };
01774 Term m_terminate;
01775 };
01776 };
01777
01778 #endif // Manager_h