OpenRTM-aist  2.0.2
Manager.h
Go to the documentation of this file.
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:
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 
242 
260  std::string& getLogLevel() {return m_config["logger.log_level"];}
261 
280 
306 
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  //============================================================
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 
767 
782 
802 
827 
850 
851 
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
947  bool autoclean = true);
948  void
950 
951  void
953  bool autoclean = true);
954  void
956 
957  void
959  bool autoclean = true);
960  void
962 
963  void
965  bool autoclean = true);
966  void
968 
969  void
971  bool autoclean = true);
972  void
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
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 
1197 
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  //============================================================
1318  bool initLogger();
1319 
1338 
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 
1503 
1515 
1516  //============================================================
1517  // Component management
1518  //============================================================
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 
1654 
1677 
1694 
1717 
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 
1869 
1870 
1871 
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  //------------------------------------------------------------
2080 
2089 
2098 
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 
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);
2202  std::string m_name;
2203  };
2204 
2206  InstanceName>;
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(""), m_language("")
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  m_language(prop["language"])
2234  {
2235  }
2236  explicit FactoryPredicate(FactoryBase* factory)
2237  : m_vendor(factory->profile()["vendor"]),
2238  m_category(factory->profile()["category"]),
2239  m_impleid(factory->profile()["implementation_id"]),
2240  m_version(factory->profile()["version"]),
2241  m_language(factory->profile()["language"])
2242  {
2243  }
2245  bool operator()(FactoryBase* factory)
2246  {
2247  // implementation_id must not be empty
2248  if (m_impleid.empty()) return false;
2249 
2250  const coil::Properties& prop(factory->profile());
2251 
2252  if (m_impleid != prop["implementation_id"])
2253  return false;
2254  if (!m_vendor.empty() && m_vendor != prop["vendor"])
2255  return false;
2256  if (!m_category.empty() && m_category != prop["category"])
2257  return false;
2258  if (!m_version.empty() && m_version != prop["version"])
2259  return false;
2260  if (!m_language.empty() && m_language != prop["language"])
2261  return false;
2262 
2263 
2264  return true;
2265  }
2266  private:
2267  std::string m_vendor;
2268  std::string m_category;
2269  std::string m_impleid;
2270  std::string m_version;
2271  std::string m_language;
2272  };
2273 
2275  {
2276  coil::Properties& m_prop;
2277  public:
2279  : m_prop(prop)
2280  {
2281  }
2283  {
2284  if (m_prop["implementation_id"] != prop["implementation_id"])
2285  {
2286  return false;
2287  }
2288  if (!m_prop["vendor"].empty() &&
2289  m_prop["vendor"] != prop["vendor"]) { return false; }
2290  if (!m_prop["category"].empty() &&
2291  m_prop["category"] != prop["category"]) { return false; }
2292  if (!m_prop["version"].empty() &&
2293  m_prop["version"] != prop["version"]) { return false; }
2294  return true;
2295  }
2296  };
2297 
2307 
2316 
2317  //============================================================
2318  // ExecutionContextファクトリ
2319  //============================================================
2320  // ECファクトリへ渡す述語クラス
2322  {
2323  explicit ECFactoryPredicate(const char* name) : m_name(name) {}
2325  : m_name(factory->name()) {}
2326  bool operator()(ECFactoryBase* factory)
2327  {
2328  return m_name == factory->name();
2329  }
2330  std::string m_name;
2331  };
2334 
2343 
2351  std::vector<ExecutionContextBase*> m_ecs;
2352 
2353  // ファクトリ名をリストアップするためのファンクタ
2355  {
2357  {
2358  modlist.emplace_back(f->profile().getProperty("implementation_id"));
2359  }
2360  std::vector<std::string> modlist;
2361  };
2362 
2363  struct Finalized
2364  {
2366  std::mutex mutex;
2367  std::vector<RTObject_impl*> comps;
2368  };
2370 
2372  }; // class Manager
2373 } // namespace RTC
2374 
2375 #endif // RTC_MANAGER_H
ExecutionContext Factory class.
RT-Component factory class.
component action listener class
Object management class.
RTComponent header.
RT component logger class.
ECFactoryBase abstract class.
Definition: ECFactory.h:116
virtual const char * name()=0
Pure virtual function to get names of creation target ExecutionContext.
A base class for ExecutionContext.
Definition: ExecutionContextBase.h:366
FactoryBase base class.
Definition: Factory.h:138
virtual coil::Properties & profile()
Get the component profile.
Logger class.
Definition: SystemLogger.h:101
Definition: Manager.h:2222
FactoryPredicate(FactoryBase *factory)
Definition: Manager.h:2236
FactoryPredicate(const char *imple_id)
Definition: Manager.h:2224
bool operator()(FactoryBase *factory)
Definition: Manager.h:2245
FactoryPredicate(const coil::Properties &prop)
Definition: Manager.h:2228
Definition: Manager.h:2275
bool operator()(coil::Properties &prop)
Definition: Manager.h:2282
ModulePredicate(coil::Properties &prop)
Definition: Manager.h:2278
Manager class.
Definition: Manager.h:91
std::string formatString(const char *naming_format, coil::Properties &prop)
Construct registration information when registering to Naming server.
void setEndpointProperty(CORBA::Object_ptr objref)
Setting endpoint info from corba.endpoints.
static void removeTask(TaskId id)
Remove the task from the Manager timer.
Definition: Manager.h:426
ReturnCode_t load(const std::string &fname, const std::string &initfunc)
[CORBA interface] Load module
void cleanupComponent(RTObject_impl *comp)
Unregister RT-Components.
void invokeInitProc()
static Manager * manager
The pointer to the Manager.
Definition: Manager.h:1992
bool unregisterComponent(RTObject_impl *comp)
Unregister RT-Components.
bool initComposite()
PeriodicECSharedComposite initialization.
bool initExecContext()
ExecutionContextManager initialization.
bool initORB()
CORBA ORB initialization.
coil::Properties & getConfig()
Get the manager configuration.
Definition: Manager.h:279
coil::Argv m_argv
The argument of ORB_init.
Definition: Manager.h:2055
void createORBEndpoints(coil::vstring &endpoints)
Create Endpoints.
ECFactoryManager m_ecfactory
ExecutionContext Manager.
Definition: Manager.h:2342
Manager()
Protected Constructor.
ModuleInitProc m_initProc
User's initialization function's pointer.
Definition: Manager.h:2079
void addRtcLifecycleActionListener(RTM::RtcLifecycleActionListener *listener, bool autoclean=true)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
Register RT-Component Factory.
bool initLocalService()
LocalService initialization.
Manager(const Manager &)=delete
CORBA::ORB_ptr theORB()
Get the pointer to ORB (ownership retained)
void shutdownComponents()
NamingManager finalization.
coil::Properties m_config
Managaer's configuration Properties.
Definition: Manager.h:2088
void endpointPropertySwitch(const std::string &ipver, bool &ip, std::vector< int > &ip_list)
Getting option info from corba.endpoint_property.
void initCpuAffinity()
RTM::ManagerServant * m_mgrservant
The pointer to the ManagerServant.
Definition: Manager.h:1879
CORBA::ORB_var m_pORB
The pointer to the ORB.
Definition: Manager.h:2022
PortableServer::POAManager_ptr thePOAManager()
Get POAManager that Manager has (ownership retained)
void initPreCreation()
NamingManager * m_namingManager
The pointer to the NamingManager.
Definition: Manager.h:2106
std::vector< std::filebuf * > m_logfiles
Files for log output.
Definition: Manager.h:2190
bool procContextArgs(const char *ec_args, std::string &ec_id, coil::Properties &ec_conf)
Extracting ExecutionContext's name/properties from the given string.
void initPreActivation()
CORBA::ORB_ptr getORB()
Get the pointer to ORB (ownership duplicated)
std::vector< RTObject_impl * > getComponents()
Get all RT-Components registered in the Manager.
void deleteComponent(RTObject_impl *comp)
Unregister RT-Components that have been registered to Manager.
bool registerComponent(RTObject_impl *comp)
Register RT-Component directly without Factory.
void cleanupComponents()
This method deletes RT-Components.
std::string createORBOptions()
Create ORB command options.
std::vector< coil::Properties > getLoadedModules()
Get a list of loaded modules.
void createORBEndpointOption(std::string &opt, coil::vstring endpoints)
Create a command optional line of Endpoint of ORB.
bool initLogger()
System logger initialization.
coil::Timer< coil::DelayedFunction > m_invoker
Timer Object for delay call on the Manager thread.
Definition: Manager.h:2115
static std::mutex mutex
The mutex of the pointer to the Manager.
Definition: Manager.h:2001
std::string & getLogLevel()
Get the log level of the configuration.
Definition: Manager.h:260
std::vector< coil::Properties > getFactoryProfiles()
Get profiles of factories.
void mainThread()
The main function of Manager main thread.
Finalized m_finalized
Definition: Manager.h:2369
RTObject_impl * getComponent(const char *instance_name)
Get RT-Component's pointer.
std::vector< coil::Properties > getLoadableModules()
Get a list of loadable modules.
bool initFactories()
Factories initialization.
std::thread m_threadOrb
ORB thread.
Definition: Manager.h:2133
int m_argvSize
The argument of ORB_init.
Definition: Manager.h:2067
FactoryManager m_factory
ComponentManager.
Definition: Manager.h:2315
void removeNamingActionListener(RTM::NamingActionListener *listener)
void connectDataPorts(PortService_ptr port, PortServiceList_var &target_ports)
coil::Timer< coil::PeriodicFunction >::TaskId TaskId
Definition: Manager.h:374
void removeRtcLifecycleActionListener(RTM::RtcLifecycleActionListener *listener)
void setModuleInitProc(ModuleInitProc proc)
Set initial procedure.
void notifyFinalized(RTObject_impl *comp)
This method deletes RT-Components.
PortableServer::POA_var m_pPOA
The pointer to the POA.
Definition: Manager.h:2031
void addManagerActionListener(RTM::ManagerActionListener *listener, bool autoclean=true)
static Manager & instance()
Get instance of the manager.
LogStreamBuf & getLogStreamBuf()
Get the log buffer.
Definition: Manager.h:241
PortableServer::POA_ptr getPOA()
Get a pointer to RootPOA held by Manager.
bool mergeProperty(coil::Properties &prop, const char *file_name)
Merge property information.
void addLocalServiceActionListener(RTM::LocalServiceActionListener *listener, bool autoclean=true)
NamingManager & getNamingManager()
Getting NamingManager.
void initPreConnection()
bool registerECFactory(const char *name, ECNewFunc new_func, ECDeleteFunc delete_func)
Register ExecutionContext Factory.
bool activateManager()
Activate the Manager.
ComponentManager m_compManager
ComponentManager.
Definition: Manager.h:2215
void configureComponent(RTObject_impl *comp, const coil::Properties &prop)
Configure RT-Component.
Logger rtclog
Logger stream.
Definition: Manager.h:2181
PortableServer::POAManager_ptr getPOAManager()
Get POAManager that Manager has (ownership duplicated)
void publishPorts(RTObject_impl *comp)
PortServiceList * getPortsOnNameServers(const std::string &nsname, const std::string &kind)
void unload(const char *fname)
Unload module.
void runManager(bool no_block=false)
Run the Manager.
PortableServer::POA_ptr thePOA()
Get a pointer to RootPOA held by Manager.
void shutdown()
Shutdown Manager.
std::list< coil::SignalAction > m_signals
An array for signals management.
Definition: Manager.h:2151
void initManager(int argc, char **argv)
Manager internal initialization.
bool initManagerServant()
ManagerServant initialization.
void shutdownORB()
ORB finalization.
static std::atomic_flag m_isRunning
A flag to stop the main thread.
Definition: Manager.h:2010
std::vector< ExecutionContextBase * > m_ecs
ExecutionContext list.
Definition: Manager.h:2351
void initLogstreamOthers()
ExecutionContextBase * createContext(const char *ec_args)
Create Context.
std::thread m_threadMain
Manager main thread.
Definition: Manager.h:2142
void invoke(std::function< void(void)> fn, std::chrono::nanoseconds delay=std::chrono::seconds::zero())
Run a function on the Manager main thread.
void shutdownManagerServant()
Manager Servant finalization.
bool m_needsTimer
A flag that Manager use Timer or not.
Definition: Manager.h:2160
void unloadAll()
Unload all modules.
void connectServicePorts(PortService_ptr port, PortServiceList_var &target_ports)
void initLogstreamPlugins()
void removeModuleActionListener(RTM::ModuleActionListener *listener)
void shutdownNaming()
NamingManager finalization.
NamingManager * getNaming()
Getting NamingManager.
PortableServer::POAManager_var m_pPOAManager
The pointer to the POAManager.
Definition: Manager.h:2043
bool initNaming()
NamingManager initialization.
void deleteComponent(const char *instance_name)
Unregister RT-Components that have been registered to Manager.
bool procComponentArgs(const char *comp_arg, coil::Properties &comp_id, coil::Properties &comp_conf)
Extracting component type/properties from the given string.
void removeLocalServiceActionListener(RTM::LocalServiceActionListener *listener)
void initLogstreamFile()
void addModuleActionListener(RTM::ModuleActionListener *listener, bool autoclean=true)
static void terminate()
Terminate manager.
std::vector< std::string > getModulesFactories()
Get the list of all Factories.
void subscribePorts(RTObject_impl *comp)
coil::Timer< coil::PeriodicFunction > m_scheduler
Timer Object for delay call on the Manager thread.
Definition: Manager.h:2124
LogStreamBuf m_logStreamBuf
Logger buffer.
Definition: Manager.h:2172
::RTM::ManagerActionListeners m_listeners
Definition: Manager.h:2371
void removeManagerActionListener(RTM::ManagerActionListener *listener)
TaskId addTask(std::function< void(void)> fn, std::chrono::nanoseconds period)
Add a task to the Manager timer.
static Manager * init(int argc, char **argv)
Initialize manager.
void shutdownLogger()
System Logger finalization.
void addNamingActionListener(RTM::NamingActionListener *listener, bool autoclean=true)
RTObject_impl * createComponent(const char *comp_args)
Create RT-Components.
ModuleManager * m_module
The pointer to the ModuleManager.
Definition: Manager.h:2097
RTM::ManagerServant & getManagerServant()
Getting ManagerServant.
Manager & operator=(const Manager &)=delete
void join()
Wait for Manager's termination.
ModuleManager class.
Definition: ModuleManager.h:72
NamingServer management class.
Definition: NamingManager.h:626
RT-Component class.
Definition: RTObject.h:93
LocalServiceActionListener class.
Definition: ManagerActionListener.h:716
Definition: ManagerActionListener.h:62
ManagerActionListeners class.
Definition: ManagerActionListener.h:967
Manager CORBA class.
Definition: ManagerServant.h:66
ModuleActionListener class.
Definition: ManagerActionListener.h:193
NamingActionListener class.
Definition: ManagerActionListener.h:558
RtcLifecycleActionListener class.
Definition: ManagerActionListener.h:355
RT-Component.
void(*)(Manager *) ModuleInitProc
Definition: Manager.h:68
ExecutionContextBase *(*)() ECNewFunc
Definition: ECFactory.h:29
void(*)(RTObject_impl *) RtcDeleteFunc
Definition: Factory.h:34
void(*)(ExecutionContextBase *) ECDeleteFunc
Definition: ECFactory.h:30
coil::Properties Properties
Definition: RTC.h:72
RTObject_impl *(*)(Manager *) RtcNewFunc
Definition: Factory.h:33
::coil::LogStreamBuffer LogStreamBuf
Definition: SystemLogger.h:34
RT-Middleware.
Definition: FactoryInit.h:22
Definition: DefaultPeriodicTask.h:24
Definition: Manager.h:2322
ECFactoryPredicate(ECFactoryBase *factory)
Definition: Manager.h:2324
bool operator()(ECFactoryBase *factory)
Definition: Manager.h:2326
std::string m_name
Definition: Manager.h:2330
ECFactoryPredicate(const char *name)
Definition: Manager.h:2323
Definition: Manager.h:2364
std::mutex mutex
Definition: Manager.h:2366
std::vector< RTObject_impl * > comps
Definition: Manager.h:2367
Definition: Manager.h:2197
InstanceName(std::string name)
InstanceName(const char *name)
InstanceName(RTObject_impl *comp)
std::string m_name
Definition: Manager.h:2202
bool operator()(RTObject_impl *comp)
Definition: Manager.h:2355
void operator()(FactoryBase *f)
Definition: Manager.h:2356
std::vector< std::string > modlist
Definition: Manager.h:2360