OpenRTM  1.0.0
Manager.h
Go to the documentation of this file.
1 // -*- C++ -*-
20 #ifndef RTC_MANAGER_H
21 #define RTC_MANAGER_H
22 
23 #include <rtm/RTC.h>
24 
25 #include <iostream>
26 #include <string>
27 #include <vector>
28 
29 #include <coil/Mutex.h>
30 #include <coil/Guard.h>
31 #include <coil/Task.h>
32 
33 #include <rtm/Factory.h>
34 #include <rtm/ECFactory.h>
35 #include <rtm/ObjectManager.h>
36 #include <rtm/SystemLogger.h>
37 
38 namespace RTM
39 {
40  class ManagerServant;
41 }
42 
43 namespace coil
44 {
45  class Timer;
46 };
47 
48 namespace RTC
49 {
50 // class Properties;
51  class CorbaNaming;
52  class ModuleManager;
53  class NamingManager;
54  class Manager;
56  typedef RTObject_impl RtcBase;
57 
58  typedef void (*ModuleInitProc)(Manager* manager);
59 
80  class Manager
81  {
82  typedef coil::Mutex Mutex;
83  typedef coil::Guard<Mutex> Guard;
84  protected:
98  Manager();
99 
117  Manager(const Manager& manager);
118  // Manager& operator=(const Manager& manager){return manager;};
119 
120  public:
173  static Manager* init(int argc, char** argv);
174 
197  static Manager& instance();
198 
199  //============================================================
200  // Manager
201  //============================================================
202 
216  void terminate();
217 
233  void shutdown();
234 
248  void join();
249 
267  LogStreamBuf& getLogStreamBuf() {return m_logStreamBuf;}
268 
286  std::string& getLogLevel() {return m_config["logger.log_level"];}
287 
305  coil::Properties& getConfig() { return m_config;}
306 
331  void setModuleInitProc(ModuleInitProc proc);
332 
365  bool activateManager();
366 
397  void runManager(bool no_block = false);
398 
399  //============================================================
400  // Module management
401  //============================================================
424  void load(const char* fname, const char* initfunc);
425 
445  void unload(const char* fname);
446 
462  void unloadAll();
463 
481  std::vector<coil::Properties> getLoadedModules();
482 
502  std::vector<coil::Properties> getLoadableModules();
503 
504  //============================================================
505  // Component Factory Management
506  //============================================================
533  bool registerFactory(coil::Properties& profile,
534  RtcNewFunc new_func,
535  RtcDeleteFunc delete_func);
536 
554  std::vector<coil::Properties> getFactoryProfiles();
555 
582  bool registerECFactory(const char* name,
583  ECNewFunc new_func,
584  ECDeleteFunc delete_func);
585 
603  std::vector<std::string> getModulesFactories();
604 
605  //============================================================
606  // Component management
607  //============================================================
666  RTObject_impl* createComponent(const char* comp_args);
680  ExecutionContextBase* createContext(const char* ec_args);
681 
700  void cleanupComponent(RTObject_impl* comp);
701 
715  void cleanupComponents();
716 
735  void notifyFinalized(RTObject_impl* comp);
736 
760  bool registerComponent(RTObject_impl* comp);
761 
783  bool unregisterComponent(RTObject_impl* comp);
784 
785 
807  void deleteComponent(RTObject_impl* comp);
808 
831  void deleteComponent(const char* instance_name);
832 
833 
857  RTObject_impl* getComponent(const char* instance_name);
858 
876  std::vector<RTObject_impl*> getComponents();
877 
878  //============================================================
879  // CORBA 関連
880  //============================================================
898  CORBA::ORB_ptr getORB();
899 
917  PortableServer::POA_ptr getPOA();
918 
936  PortableServer::POAManager_ptr getPOAManager();
937 
938  //============================================================
939  // Protected functions
940  //============================================================
941  protected:
942 
943  //============================================================
944  // Manager initialize and finalization
945  //============================================================
973  void initManager(int argc, char** argv);
974 
990  void shutdownManager();
991 
1008  void shutdownOnNoRtcs();
1009 
1010  //============================================================
1011  // Logger initialize and terminator
1012  //============================================================
1034  bool initLogger();
1035 
1053  void shutdownLogger();
1054 
1055  //============================================================
1056  // ORB initialization and finalization
1057  //============================================================
1075  bool initORB();
1076 
1096  std::string createORBOptions();
1097 
1115  void createORBEndpoints(coil::vstring& endpoints);
1116 
1130  void createORBEndpointOption(std::string& opt, coil::vstring& endpoint);
1131 
1150  void shutdownORB();
1151 
1152  //============================================================
1153  // NamingService initialization and finalization
1154  //============================================================
1186  bool initNaming();
1187 
1203  void shutdownNaming();
1204 
1205  //============================================================
1206  // Component management
1207  //============================================================
1223  void shutdownComponents();
1224 
1225 
1273  bool procComponentArgs(const char* comp_arg,
1274  coil::Properties& comp_id,
1275  coil::Properties& comp_conf);
1316  bool procContextArgs(const char* ec_args,
1317  std::string& ec_id,
1318  coil::Properties& ec_conf);
1319 
1342  void configureComponent(RTObject_impl* comp, const coil::Properties& prop);
1343 
1365  bool initExecContext();
1366 
1382  bool initComposite();
1383 
1405  bool initFactories();
1406 
1426  bool initTimer();
1427 
1441  bool initManagerServant();
1442 
1451 
1477  bool mergeProperty(coil::Properties& prop, const char* file_name);
1478 
1527  std::string formatString(const char* naming_format,
1528  coil::Properties& prop);
1529 
1530 
1531 
1532  //============================================================
1533  // protected 変数
1534  //============================================================
1535 
1536  //------------------------------------------------------------
1537  // static var
1538  //------------------------------------------------------------
1546  static Manager* manager;
1547 
1555  static Mutex mutex;
1556 
1557  //------------------------------------------------------------
1558  // CORBA var
1559  //------------------------------------------------------------
1567  CORBA::ORB_var m_pORB;
1568 
1576  PortableServer::POA_var m_pPOA;
1577 
1585  PortableServer::POAManager_var m_pPOAManager;
1586 
1587  //------------------------------------------------------------
1588  // Manager's variable
1589  //------------------------------------------------------------
1598 
1607 
1616 
1625 
1634 
1635  //------------------------------------------------------------
1636  // Logger
1637  //------------------------------------------------------------
1646 
1655 
1663  std::vector<std::filebuf*> m_logfiles;
1664 
1665  //============================================================
1666  // コンポーネントマネージャ
1667  //============================================================
1668  // ObjectManager へ渡す述語クラス
1670  {
1671  InstanceName(RTObject_impl* comp);
1672  InstanceName(const char* name);
1673  InstanceName(const std::string name);
1674  bool operator()(RTObject_impl* comp);
1675  std::string m_name;
1676  };
1677 
1679 
1687  ComponentManager m_compManager;
1688 
1689  //============================================================
1690  // コンポーネントファクトリ
1691  //============================================================
1692  // コンポーネントファクトリへ渡す述語クラス
1694  {
1695  public:
1696  FactoryPredicate(const char* imple_id)
1697  : m_vendor(""), m_category(""), m_impleid(imple_id), m_version("")
1698  {
1699  }
1701  : m_vendor(prop["vendor"]),
1702  m_category(prop["category"]),
1703  m_impleid(prop["implementation_id"]),
1704  m_version(prop["version"])
1705  {
1706  }
1708  : m_vendor(factory->profile()["vendor"]),
1709  m_category(factory->profile()["category"]),
1710  m_impleid(factory->profile()["implementation_id"]),
1711  m_version(factory->profile()["version"])
1712  {
1713  }
1714  bool operator()(FactoryBase* factory)
1715  {
1716  // implementation_id must not be empty
1717  if (m_impleid.empty()) return false;
1718 
1719  const coil::Properties& prop(factory->profile());
1720 
1721  if (m_impleid != prop["implementation_id"])
1722  return false;
1723  if (!m_vendor.empty() && m_vendor != prop["vendor"])
1724  return false;
1725  if (!m_category.empty() && m_category != prop["category"])
1726  return false;
1727  if (!m_version.empty() && m_version != prop["version"])
1728  return false;
1729 
1730  return true;
1731  }
1732  private:
1733  std::string m_vendor;
1734  std::string m_category;
1735  std::string m_impleid;
1736  std::string m_version;
1737  };
1738 
1740  {
1741  coil::Properties& m_prop;
1742  public:
1744  : m_prop(prop)
1745  {
1746  }
1748  {
1749  if (m_prop["implementation_id"] != prop["implementation_id"])
1750  {
1751  return false;
1752  }
1753  if (!m_prop["vendor"].empty() &&
1754  m_prop["vendor"] != prop["vendor"]) { return false; }
1755  if (!m_prop["category"].empty() &&
1756  m_prop["category"] != prop["category"]) { return false; }
1757  if (!m_prop["version"].empty() &&
1758  m_prop["version"] != prop["version"]) { return false; }
1759  return true;
1760  }
1761  };
1762 
1772 
1780  FactoryManager m_factory;
1781 
1782  //============================================================
1783  // ExecutionContextファクトリ
1784  //============================================================
1785  // ECファクトリへ渡す述語クラス
1787  {
1788  ECFactoryPredicate(const char* name) : m_name(name){};
1790  : m_name(factory->name()) {};
1791  bool operator()(ECFactoryBase* factory)
1792  {
1793  return m_name == factory->name();
1794  }
1795  std::string m_name;
1796  };
1797  typedef ObjectManager<const char*,
1798  ECFactoryBase,
1800 
1808  ECFactoryManager m_ecfactory;
1809 
1817  std::vector<ExecutionContextBase*> m_ecs;
1818 
1819  // ファクトリ名をリストアップするためのファンクタ
1821  {
1822  void operator()(FactoryBase* f)
1823  {
1824  modlist.push_back(f->profile().getProperty("implementation_id"));
1825  }
1826  std::vector<std::string> modlist;
1827  };
1828 
1829  //------------------------------------------------------------
1830  // ORB runner
1831  //------------------------------------------------------------
1852  : public coil::Task
1853  {
1854  public:
1868  OrbRunner(CORBA::ORB_ptr orb) : m_pORB(orb)
1869  {
1870  open(0);
1871  };
1872 
1894  virtual int open(void *args)
1895  {
1896  activate();
1897  return 0;
1898  }
1899 
1917  virtual int svc(void)
1918  {
1919  m_pORB->run();
1920 // Manager::instance().shutdown();
1921  return 0;
1922  }
1923 
1945  virtual int close(unsigned long flags)
1946  {
1947  return 0;
1948  }
1949  private:
1950  CORBA::ORB_ptr m_pORB;
1951  };
1960 
1961  //------------------------------------------------------------
1962  // Manager Terminator
1963  //------------------------------------------------------------
1984  : public coil::Task
1985  {
1986  public:
2004  Terminator(Manager* manager) : m_manager(manager) {};
2005 
2019  void terminate()
2020  {
2021  open(0);
2022  }
2023 
2045  virtual int open(void *args)
2046  {
2047  activate();
2048  return 0;
2049  }
2050 
2068  virtual int svc(void)
2069  {
2070  Manager::instance().shutdown();
2071  return 0;
2072  }
2074  };
2075 
2084 
2085  struct Term
2086  {
2087  int waiting;
2088  Mutex mutex;
2089  };
2104 
2105  struct Finalized
2106  {
2107  Mutex mutex;
2108  std::vector<RTObject_impl*> comps;
2109  };
2111 
2112 
2113  }; // class Manager
2114 }; // namespace RTC
2115 
2116 #endif // RTC_MANAGER_H
RTObject_impl *(* RtcNewFunc)(Manager *manager)
Definition: rtm/Factory.h:33
static Mutex mutex
The mutex of the pointer to the Manager.
Definition: Manager.h:1555
LogStreamBuf & getLogStreamBuf()
Get the log buffer.
Definition: Manager.h:267
void(* ModuleInitProc)(Manager *manager)
Definition: Manager.h:58
Logger rtclog
Logger stream.
Definition: Manager.h:1654
std::vector< std::string > modlist
Definition: Manager.h:1826
RT-Component.
virtual coil::Properties & profile()
Get the component profile.
virtual int open(void *args)
ORB activation processing.
Definition: Manager.h:1894
Term m_terminate
Synchronous flag for manager termination.
Definition: Manager.h:2103
Definition: Manager.h:1786
log_streambuf template class
Definition: Logger.h:54
Manager * m_manager
Definition: Manager.h:2073
FactoryManager m_factory
ComponentManager.
Definition: Manager.h:1780
Mutex class.
Definition: Mutex.h:40
RT component logger class.
void terminate()
Termination processing.
Definition: Manager.h:2019
Definition: Manager.h:1820
RTM::ManagerServant * m_mgrservant
The pointer to the ManagerServant.
Definition: Manager.h:1450
Manager CORBA class.
Definition: ManagerServant.h:63
FactoryPredicate(const char *imple_id)
Definition: Manager.h:1696
A base class for ExecutionContext.
Definition: ExecutionContextBase.h:51
std::string & getLogLevel()
Get the log level of the configuration.
Definition: Manager.h:286
RT-Component class.
Definition: RTObject.h:89
Definition: Manager.h:1669
Manager class.
Definition: Manager.h:80
int waiting
Definition: Manager.h:2087
ECFactoryPredicate(const char *name)
Definition: Manager.h:1788
coil::Timer * m_timer
Timer Object.
Definition: Manager.h:1633
NamingManager * m_namingManager
The pointer to the NamingManager.
Definition: Manager.h:1624
std::string m_name
Definition: Manager.h:1675
OrbRunner(CORBA::ORB_ptr orb)
Constructor.
Definition: Manager.h:1868
virtual int svc(void)
ORB start processing.
Definition: Manager.h:1917
std::vector< std::string > vstring
Definition: stringutil.h:37
coil::Properties & getConfig()
Get the manager configuration.
Definition: Manager.h:305
static Manager * manager
The pointer to the Manager.
Definition: Manager.h:1546
ECFactoryBase abstract class.
Definition: ECFactory.h:115
std::vector< std::filebuf * > m_logfiles
Files for log output.
Definition: Manager.h:1663
const std::string & getProperty(const std::string &key) const
Search for the property with the specified key in this property.
Definition: Manager.h:2085
coil::Properties Properties
Definition: RTC.h:83
std::vector< ExecutionContextBase * > m_ecs
ExecutionContext list.
Definition: Manager.h:1817
std::string m_name
Definition: Manager.h:1795
Definition: Manager.h:2105
LogStreamBuf m_logStreamBuf
Logger buffer.
Definition: Manager.h:1645
Finalized m_finalized
Definition: Manager.h:2110
ObjectManager< const coil::Properties, FactoryBase, FactoryPredicate > FactoryManager
ComponentFactory.
Definition: Manager.h:1771
Guard template class.
OrbRunner class.
Definition: Manager.h:1851
coil::Properties m_config
Managaer&#39;s configuration Properties.
Definition: Manager.h:1606
CORBA::ORB_var m_pORB
The pointer to the ORB.
Definition: Manager.h:1567
ObjectManager< const char *, ECFactoryBase, ECFactoryPredicate > ECFactoryManager
Definition: Manager.h:1799
Logger class.
Definition: SystemLogger.h:99
FactoryPredicate(const coil::Properties &prop)
Definition: Manager.h:1700
ModuleManager class.
Definition: ModuleManager.h:74
ECFactoryPredicate(ECFactoryBase *factory)
Definition: Manager.h:1789
ModuleManager * m_module
The pointer to the ModuleManager.
Definition: Manager.h:1615
virtual int close(unsigned long flags)
ORB close processing.
Definition: Manager.h:1945
virtual int open(void *args)
Termination processing activation.
Definition: Manager.h:2045
Definition: Manager.h:1739
Terminator * m_terminator
The pointer to ORB termination helper class.
Definition: Manager.h:2083
Timer class.
Definition: Timer.h:53
Class represents a set of properties.
Definition: Properties.h:101
ExecutionContext Factory class.
ExecutionContextBase *(* ECNewFunc)()
Definition: ECFactory.h:29
Object management class.
std::vector< RTObject_impl * > comps
Definition: Manager.h:2108
FactoryBase base class.
Definition: rtm/Factory.h:118
Task class.
Definition: Task.h:39
FactoryPredicate(FactoryBase *factory)
Definition: Manager.h:1707
ObjectManager< std::string, RTObject_impl, InstanceName > ComponentManager
Definition: Manager.h:1678
void(* RtcDeleteFunc)(RTObject_impl *rtc)
Definition: rtm/Factory.h:34
ComponentManager m_compManager
ComponentManager.
Definition: Manager.h:1687
RTComponent header.
Mutex mutex
Definition: Manager.h:2107
bool operator()(FactoryBase *factory)
Definition: Manager.h:1714
Mutex mutex
Definition: Manager.h:2088
bool operator()(coil::Properties &prop)
Definition: Manager.h:1747
PortableServer::POA_var m_pPOA
The pointer to the POA.
Definition: Manager.h:1576
Guard template class.
Definition: Guard.h:41
Definition: Manager.h:1693
Terminator(Manager *manager)
Constructor.
Definition: Manager.h:2004
virtual const char * name()=0
Pure virtual function to get names of creation target ExecutionContext.
ECFactoryManager m_ecfactory
ExecutionContext Manager.
Definition: Manager.h:1808
NamingServer management class.
Definition: NamingManager.h:353
void(* ECDeleteFunc)(ExecutionContextBase *ec)
Definition: ECFactory.h:30
OrbRunner * m_runner
The pointer to ORB helper class.
Definition: Manager.h:1959
bool operator()(ECFactoryBase *factory)
Definition: Manager.h:1791
RT-Middleware.
Definition: Manager.h:38
ModulePredicate(coil::Properties &prop)
Definition: Manager.h:1743
PortableServer::POAManager_var m_pPOAManager
The pointer to the POAManager.
Definition: Manager.h:1585
Terminator class.
Definition: Manager.h:1983
ModuleInitProc m_initProc
User&#39;s initialization function&#39;s pointer.
Definition: Manager.h:1597
void operator()(FactoryBase *f)
Definition: Manager.h:1822
virtual int svc(void)
ORB and manager&#39;s termination processing.
Definition: Manager.h:2068
RTObject_impl RtcBase
Definition: Manager.h:55
Common Object Interface Layer.
Definition: Allocator.h:27