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 
268 
286  std::string& getLogLevel() {return m_config["logger.log_level"];}
287 
306 
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 
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 
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 
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 
1809 
1817  std::vector<ExecutionContextBase*> m_ecs;
1818 
1819  // ファクトリ名をリストアップするためのファンクタ
1821  {
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  {
2071  return 0;
2072  }
2074  };
2075 
2084 
2085  struct Term
2086  {
2087  int waiting;
2089  };
2104 
2105  struct Finalized
2106  {
2108  std::vector<RTObject_impl*> comps;
2109  };
2111 
2112 
2113  }; // class Manager
2114 }; // namespace RTC
2115 
2116 #endif // RTC_MANAGER_H