OpenRTM-aist  2.0.0
Manager.h
[詳解]
1 
20 #ifndef RTC_MANAGER_H
21 #define RTC_MANAGER_H
22 
23 #include <rtm/RTC.h>
24 
25 #ifdef ORB_IS_TAO
26 #include <tao/Strategies/advanced_resource.h>
27 #include <tao/IORTable/IORTable.h>
28 #endif
29 
30 #include <rtm/Factory.h>
31 #include <rtm/ECFactory.h>
32 #include <rtm/ObjectManager.h>
33 #include <rtm/SystemLogger.h>
35 
36 #ifdef ORB_IS_ORBEXPRESS
37 #include <RTPortableServer.h>
38 #endif
39 
40 #include <coil/Timer.h>
41 #include <coil/Signal.h>
42 
43 #include <atomic>
44 #include <functional>
45 #include <mutex>
46 #include <list>
47 #include <thread>
48 #include <vector>
49 
50 namespace RTM
51 {
52  class ManagerServant;
53 } // namespace RTM
54 
55 namespace coil
56 {
57 } // namespace coil
58 
59 namespace RTC
60 {
61  class CorbaNaming;
62  class ModuleManager;
63  class NamingManager;
64  class Manager;
65  class RTObject_impl;
67 
68  using ModuleInitProc = void (*)(Manager*);
69 
90  class Manager
91  {
92  protected:
106  Manager();
107 
108  Manager(const Manager&) = delete;
109  Manager& operator=(const Manager&) = delete;
110 
111  public:
164  static Manager* init(int argc, char** argv);
165 
188  static Manager& instance();
189 
190  //============================================================
191  // Manager
192  //============================================================
193 
207  static void terminate();
208 
222  void join();
223 
241  LogStreamBuf& getLogStreamBuf() {return m_logStreamBuf;}
242 
260  std::string& getLogLevel() {return m_config["logger.log_level"];}
261 
279  coil::Properties& getConfig() { return m_config;}
280 
305  void setModuleInitProc(ModuleInitProc proc);
306 
339  bool activateManager();
340 
372  void runManager(bool no_block = false);
373 
374  using TaskId = coil::Timer<coil::PeriodicFunction>::TaskId;
375 
404  TaskId addTask(std::function<void(void)> fn,
405  std::chrono::nanoseconds period);
406 
426  static void removeTask(TaskId id) { id->stop(); }
427 
451  void invoke(std::function<void(void)> fn,
452  std::chrono::nanoseconds delay
453  = std::chrono::seconds::zero());
454 
455  //============================================================
456  // Module management
457  //============================================================
490  ReturnCode_t load(const std::string& fname, const std::string& initfunc);
491 
511  void unload(const char* fname);
512 
528  void unloadAll();
529 
547  std::vector<coil::Properties> getLoadedModules();
548 
568  std::vector<coil::Properties> getLoadableModules();
569 
570  //============================================================
571  // Component Factory Management
572  //============================================================
599  bool registerFactory(coil::Properties& profile,
600  RtcNewFunc new_func,
601  RtcDeleteFunc delete_func);
602 
620  std::vector<coil::Properties> getFactoryProfiles();
621 
648  bool registerECFactory(const char* name,
649  ECNewFunc new_func,
650  ECDeleteFunc delete_func);
651 
669  std::vector<std::string> getModulesFactories();
670 
671  //============================================================
672  // Component management
673  //============================================================
732  RTObject_impl* createComponent(const char* comp_args);
746  ExecutionContextBase* createContext(const char* ec_args);
747 
766  void cleanupComponent(RTObject_impl* comp);
767 
781  void cleanupComponents();
782 
801  void notifyFinalized(RTObject_impl* comp);
802 
826  bool registerComponent(RTObject_impl* comp);
827 
849  bool unregisterComponent(RTObject_impl* comp);
850 
851 
873  void deleteComponent(RTObject_impl* comp);
874 
897  void deleteComponent(const char* instance_name);
898 
899 
923  RTObject_impl* getComponent(const char* instance_name);
924 
942  std::vector<RTObject_impl*> getComponents();
943 
944 
945  void
946  addManagerActionListener(RTM::ManagerActionListener* listener,
947  bool autoclean = true);
948  void
949  removeManagerActionListener(RTM::ManagerActionListener* listener);
950 
951  void
952  addModuleActionListener(RTM::ModuleActionListener* listener,
953  bool autoclean = true);
954  void
955  removeModuleActionListener(RTM::ModuleActionListener* listener);
956 
957  void
958  addRtcLifecycleActionListener(RTM::RtcLifecycleActionListener* listener,
959  bool autoclean = true);
960  void
961  removeRtcLifecycleActionListener(RTM::RtcLifecycleActionListener* listener);
962 
963  void
964  addNamingActionListener(RTM::NamingActionListener* listener,
965  bool autoclean = true);
966  void
967  removeNamingActionListener(RTM::NamingActionListener* listener);
968 
969  void
970  addLocalServiceActionListener(RTM::LocalServiceActionListener* listener,
971  bool autoclean = true);
972  void
973  removeLocalServiceActionListener(RTM::LocalServiceActionListener* listener);
974 
975 
976  //============================================================
977  // CORBA 関連
978  //============================================================
1005  CORBA::ORB_ptr theORB();
1006 
1043  CORBA::ORB_ptr getORB();
1044 
1072  PortableServer::POA_ptr thePOA();
1073 #ifdef ORB_IS_OMNIORB
1074  PortableServer::POA_ptr theShortCutPOA();
1075 #endif
1076 
1112  PortableServer::POA_ptr getPOA();
1113 #ifdef ORB_IS_OMNIORB
1114  PortableServer::POA_ptr getShortCutPOA();
1115 #endif
1116 
1144  PortableServer::POAManager_ptr thePOAManager();
1184  PortableServer::POAManager_ptr getPOAManager();
1185 
1196  RTM::ManagerServant& getManagerServant();
1197 
1211  bool initLocalService();
1226  NamingManager* getNaming();
1227 
1228  //============================================================
1229  // Protected functions
1230  //============================================================
1231  protected:
1232  //============================================================
1233  // Manager initialize and finalization
1234  //============================================================
1262  void initManager(int argc, char** argv);
1263 
1279  void shutdown();
1280 
1288  // TAO will cause the build to fail, so don't use name "main".
1289  void mainThread();
1290 
1291  //============================================================
1292  // Logger initialize and terminator
1293  //============================================================
1294  void initLogstreamFile();
1295  void initLogstreamPlugins();
1296  void initLogstreamOthers();
1318  bool initLogger();
1319 
1337  void shutdownLogger();
1338 
1352  void shutdownManagerServant();
1353 
1354  //============================================================
1355  // ORB initialization and finalization
1356  //============================================================
1374  bool initORB();
1375 
1395  std::string createORBOptions();
1396 
1414  void createORBEndpoints(coil::vstring& endpoints);
1415 
1429  void createORBEndpointOption(std::string& opt, coil::vstring endpoints);
1430 
1449  void shutdownORB();
1450 
1451  //============================================================
1452  // NamingService initialization and finalization
1453  //============================================================
1485  bool initNaming();
1486 
1502  void shutdownNaming();
1503 
1514  NamingManager& getNamingManager();
1515 
1516  //============================================================
1517  // Component management
1518  //============================================================
1534  void shutdownComponents();
1535 
1536 
1584  bool procComponentArgs(const char* comp_arg,
1585  coil::Properties& comp_id,
1586  coil::Properties& comp_conf);
1627  bool procContextArgs(const char* ec_args,
1628  std::string& ec_id,
1629  coil::Properties& ec_conf);
1630 
1653  void configureComponent(RTObject_impl* comp, const coil::Properties& prop);
1654 
1676  bool initExecContext();
1677 
1693  bool initComposite();
1694 
1716  bool initFactories();
1717 
1718  void initCpuAffinity();
1733  void initPreConnection();
1748  void initPreActivation();
1763  void initPreCreation();
1776  void invokeInitProc();
1791  void publishPorts(RTObject_impl* comp);
1806  void subscribePorts(RTObject_impl* comp);
1821  PortServiceList* getPortsOnNameServers(const std::string& nsname, const std::string& kind);
1837  void connectDataPorts(PortService_ptr port, PortServiceList_var& target_ports);
1853  void connectServicePorts(PortService_ptr port, PortServiceList_var& target_ports);
1854 
1868  bool initManagerServant();
1869 
1870 
1871 
1879  RTM::ManagerServant* m_mgrservant{nullptr};
1880 
1906  bool mergeProperty(coil::Properties& prop, const char* file_name);
1907 
1956  std::string formatString(const char* naming_format,
1957  coil::Properties& prop);
1958 
1966  void setEndpointProperty(CORBA::Object_ptr objref);
1967 
1975  void endpointPropertySwitch(const std::string& ipver,
1976  bool& ip, std::vector<int>& ip_list);
1977 
1978  //============================================================
1979  // protected 変数
1980  //============================================================
1981 
1982  //------------------------------------------------------------
1983  // static var
1984  //------------------------------------------------------------
1992  static Manager* manager;
1993 
2001  static std::mutex mutex;
2002 
2010  static std::atomic_flag m_isRunning;
2011 
2012  //------------------------------------------------------------
2013  // CORBA var
2014  //------------------------------------------------------------
2022  CORBA::ORB_var m_pORB;
2023 
2031  PortableServer::POA_var m_pPOA;
2032 #ifdef ORB_IS_OMNIORB
2033  PortableServer::POA_var m_pShortCutPOA;
2034 #endif
2035 
2043  PortableServer::POAManager_var m_pPOAManager;
2044 
2055  coil::Argv m_argv;
2056 
2068 
2069  //------------------------------------------------------------
2070  // Manager's variable
2071  //------------------------------------------------------------
2079  ModuleInitProc m_initProc{nullptr};
2080 
2089 
2097  ModuleManager* m_module{nullptr};
2098 
2106  NamingManager* m_namingManager{nullptr};
2107 
2115  coil::Timer<coil::DelayedFunction> m_invoker;
2116 
2124  coil::Timer<coil::PeriodicFunction> m_scheduler;
2125 
2133  std::thread m_threadOrb;
2134 
2142  std::thread m_threadMain;
2143 
2151  std::list<coil::SignalAction> m_signals;
2152 
2160  bool m_needsTimer{false};
2161 
2162  //------------------------------------------------------------
2163  // Logger
2164  //------------------------------------------------------------
2173 
2181  Logger rtclog{&m_logStreamBuf};
2182 
2190  std::vector<std::filebuf*> m_logfiles;
2191 
2192  //============================================================
2193  // コンポーネントマネージャ
2194  //============================================================
2195  // ObjectManager へ渡す述語クラス
2197  {
2198  explicit InstanceName(RTObject_impl* comp);
2199  explicit InstanceName(const char* name);
2200  explicit InstanceName(std::string name);
2201  bool operator()(RTObject_impl* comp);
2202  std::string m_name;
2203  };
2204 
2205  using ComponentManager = ObjectManager<std::string, RTObject_impl,
2207 
2216 
2217  //============================================================
2218  // コンポーネントファクトリ
2219  //============================================================
2220  // コンポーネントファクトリへ渡す述語クラス
2222  {
2223  public:
2224  explicit FactoryPredicate(const char* imple_id)
2225  : m_vendor(""), m_category(""), m_impleid(imple_id), m_version("")
2226  {
2227  }
2228  explicit FactoryPredicate(const coil::Properties& prop)
2229  : m_vendor(prop["vendor"]),
2230  m_category(prop["category"]),
2231  m_impleid(prop["implementation_id"]),
2232  m_version(prop["version"])
2233  {
2234  }
2235  explicit FactoryPredicate(FactoryBase* factory)
2236  : m_vendor(factory->profile()["vendor"]),
2237  m_category(factory->profile()["category"]),
2238  m_impleid(factory->profile()["implementation_id"]),
2239  m_version(factory->profile()["version"])
2240  {
2241  }
2242  ~FactoryPredicate();
2243  bool operator()(FactoryBase* factory)
2244  {
2245  // implementation_id must not be empty
2246  if (m_impleid.empty()) return false;
2247 
2248  const coil::Properties& prop(factory->profile());
2249 
2250  if (m_impleid != prop["implementation_id"])
2251  return false;
2252  if (!m_vendor.empty() && m_vendor != prop["vendor"])
2253  return false;
2254  if (!m_category.empty() && m_category != prop["category"])
2255  return false;
2256  if (!m_version.empty() && m_version != prop["version"])
2257  return false;
2258 
2259  return true;
2260  }
2261  private:
2262  std::string m_vendor;
2263  std::string m_category;
2264  std::string m_impleid;
2265  std::string m_version;
2266  };
2267 
2269  {
2270  coil::Properties& m_prop;
2271  public:
2273  : m_prop(prop)
2274  {
2275  }
2277  {
2278  if (m_prop["implementation_id"] != prop["implementation_id"])
2279  {
2280  return false;
2281  }
2282  if (!m_prop["vendor"].empty() &&
2283  m_prop["vendor"] != prop["vendor"]) { return false; }
2284  if (!m_prop["category"].empty() &&
2285  m_prop["category"] != prop["category"]) { return false; }
2286  if (!m_prop["version"].empty() &&
2287  m_prop["version"] != prop["version"]) { return false; }
2288  return true;
2289  }
2290  };
2291 
2301 
2310 
2311  //============================================================
2312  // ExecutionContextファクトリ
2313  //============================================================
2314  // ECファクトリへ渡す述語クラス
2316  {
2317  explicit ECFactoryPredicate(const char* name) : m_name(name) {}
2319  : m_name(factory->name()) {}
2320  bool operator()(ECFactoryBase* factory)
2321  {
2322  return m_name == factory->name();
2323  }
2324  std::string m_name;
2325  };
2326  using ECFactoryManager = ObjectManager<const char*, ECFactoryBase,
2328 
2337 
2345  std::vector<ExecutionContextBase*> m_ecs;
2346 
2347  // ファクトリ名をリストアップするためのファンクタ
2349  {
2351  {
2352  modlist.emplace_back(f->profile().getProperty("implementation_id"));
2353  }
2354  std::vector<std::string> modlist;
2355  };
2356 
2357  struct Finalized
2358  {
2359  ~Finalized();
2360  std::mutex mutex;
2361  std::vector<RTObject_impl*> comps;
2362  };
2364 
2366  }; // class Manager
2367 } // namespace RTC
2368 
2369 #endif // RTC_MANAGER_H
LogStreamBuf & getLogStreamBuf()
ログバッファの取得
Definition: Manager.h:241
std::vector< std::string > modlist
Definition: Manager.h:2354
RTコンポーネント
ExecutionContextBase *(*)() ECNewFunc
Definition: ECFactory.h:29
virtual coil::Properties & profile()
コンポーネントプロファイルの取得
void(*)(ExecutionContextBase *) ECDeleteFunc
Definition: ECFactory.h:30
RTObject_impl *(*)(Manager *) RtcNewFunc
Definition: Factory.h:33
Definition: Manager.h:2315
std::thread m_threadMain
Manager のメインスレッド
Definition: Manager.h:2142
FactoryManager m_factory
ComponentManager
Definition: Manager.h:2309
RT component logger class
Definition: Manager.h:2348
ManagerのCORBA化クラス
Definition: ManagerServant.h:63
FactoryPredicate(const char *imple_id)
Definition: Manager.h:2224
ExecutionContext用基底クラス
Definition: ExecutionContextBase.h:365
std::string & getLogLevel()
コンフィグレーションのログレベルの取得
Definition: Manager.h:260
ModuleActionListener クラス
Definition: ManagerActionListener.h:192
RTコンポーネントクラス
Definition: RTObject.h:90
std::thread m_threadOrb
ORB 用のスレッド
Definition: Manager.h:2133
Definition: Manager.h:2196
coil::Timer< coil::PeriodicFunction >::TaskId TaskId
Definition: Manager.h:374
Manager クラス
Definition: Manager.h:90
ECFactoryPredicate(const char *name)
Definition: Manager.h:2317
std::string m_name
Definition: Manager.h:2202
RtcLifecycleActionListener クラス
Definition: ManagerActionListener.h:354
LocalServiceActionListener クラス
Definition: ManagerActionListener.h:715
ManagerActionListeners クラス
Definition: ManagerActionListener.h:966
::coil::LogStreamBuffer LogStreamBuf
Definition: SystemLogger.h:34
int m_argvSize
ORB_init に指定する引数
Definition: Manager.h:2067
RT-Component factory class
coil::Properties & getConfig()
マネージャコンフィギュレーションの取得
Definition: Manager.h:279
static Manager * manager
唯一の Manager へのポインタ
Definition: Manager.h:1992
ECFactoryBase 抽象クラス
Definition: ECFactory.h:115
std::vector< std::filebuf * > m_logfiles
ログ出力ファイル
Definition: Manager.h:2190
std::vector< ExecutionContextBase * > m_ecs
ExecutionContext リスト
Definition: Manager.h:2345
std::string m_name
Definition: Manager.h:2324
Definition: Manager.h:2357
LogStreamBuf m_logStreamBuf
ロガーバッファ
Definition: Manager.h:2172
Finalized m_finalized
Definition: Manager.h:2363
coil::Properties m_config
Manager の configuration を格納する Properties
Definition: Manager.h:2088
CORBA::ORB_var m_pORB
ORB へのポインタ
Definition: Manager.h:2022
coil::Timer< coil::DelayedFunction > m_invoker
Manager スレッド上での遅延呼び出し用タイマー
Definition: Manager.h:2115
void(*)(RTObject_impl *) RtcDeleteFunc
Definition: Factory.h:34
Logger クラス
Definition: SystemLogger.h:99
FactoryPredicate(const coil::Properties &prop)
Definition: Manager.h:2228
void(*)(Manager *) ModuleInitProc
Definition: Manager.h:68
モジュールマネージャクラス
Definition: ModuleManager.h:71
ECFactoryPredicate(ECFactoryBase *factory)
Definition: Manager.h:2318
Definition: Manager.h:2268
static std::atomic_flag m_isRunning
メインスレッド停止用フラグ
Definition: Manager.h:2010
ExecutionContext Factory class
Object management class
std::vector< RTObject_impl * > comps
Definition: Manager.h:2361
FactoryBase 基底クラス
Definition: Factory.h:137
component action listener class
coil::Argv m_argv
ORB_init に指定する引数
Definition: Manager.h:2055
FactoryPredicate(FactoryBase *factory)
Definition: Manager.h:2235
ComponentManager m_compManager
コンポーネントマネージャ
Definition: Manager.h:2215
RTComponent header
NamingActionListener クラス
Definition: ManagerActionListener.h:557
bool operator()(FactoryBase *factory)
Definition: Manager.h:2243
bool operator()(coil::Properties &prop)
Definition: Manager.h:2276
PortableServer::POA_var m_pPOA
POA へのポインタ
Definition: Manager.h:2031
static void removeTask(TaskId id)
周期実行タスクの削除
Definition: Manager.h:426
static std::mutex mutex
唯一の Manager へのポインタに対する mutex
Definition: Manager.h:2001
Definition: Manager.h:2221
virtual const char * name()=0
生成対象ExecutionContext名称取得用純粋仮想関数
::RTM::ManagerActionListeners m_listeners
Definition: Manager.h:2365
Definition: ManagerActionListener.h:61
ECFactoryManager m_ecfactory
ExecutionContext マネージャ
Definition: Manager.h:2336
std::list< coil::SignalAction > m_signals
シグナル管理用配列
Definition: Manager.h:2151
NamingServer 管理クラス
Definition: NamingManager.h:625
coil::Properties Properties
Definition: RTC.h:72
coil::Timer< coil::PeriodicFunction > m_scheduler
Manager スレッド上での周期呼び出し用タイマー
Definition: Manager.h:2124
bool operator()(ECFactoryBase *factory)
Definition: Manager.h:2320
RT-Middleware
Definition: FactoryInit.h:21
ModulePredicate(coil::Properties &prop)
Definition: Manager.h:2272
PortableServer::POAManager_var m_pPOAManager
POAManager へのポインタ
Definition: Manager.h:2043
std::mutex mutex
Definition: Manager.h:2360
void operator()(FactoryBase *f)
Definition: Manager.h:2350
Definition: DefaultPeriodicTask.h:23