cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mtsManagerLocal.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Min Yang Jung
7  Created on: 2009-12-07
8 
9  (C) Copyright 2009-2013 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 */
20 
21 
55 #ifndef _mtsManagerLocal_h
56 #define _mtsManagerLocal_h
57 
59 #include <cisstCommon/cmnUnits.h>
67 
68 #include <stack>
69 
71 
72 // Helper macro (useful to connect interfaces in main.cpp)
73 #define CONNECT_LOCAL(_clientComp, _reqInt, _serverComp, _prvInt)\
74  if (!mtsManagerLocal::GetInstance()->Connect(_clientComp, _reqInt, _serverComp, _prvInt)) {\
75  CMN_LOG_INIT_ERROR << "Failed to connect: "\
76  << _clientComp << ":" << _reqInt << " - "\
77  << _serverComp << ":" << _prvInt << std::endl;\
78  exit(1);\
79  }
80 
81 #define CONNECT_REMOTE(_clientProc, _clientComp, _reqInt, _serverProc, _serverComp, _prvInt)\
82  if (!mtsManagerLocal::GetInstance()->Connect(_clientProc, _clientComp, _reqInt, _serverProc, _serverComp, _prvInt)) {\
83  CMN_LOG_INIT_ERROR << "Failed to connect: "\
84  << _clientProc << ":" << _clientComp << ":" << _reqInt << " - "\
85  << _serverProc << ":" << _serverComp << ":" << _prvInt << std::endl;\
86  exit(1);\
87  }
88 
90 {
91  // for unit-testing
92  friend class mtsManagerLocalTest;
93  friend class mtsManagerGlobalTest;
94  // for internal access to manage proxy objects
95  friend class mtsManagerGlobal;
96  friend class mtsManagerProxyClient;
97  // for dynamic creation of a component
99  // for reconfiguration
100  friend class mtsComponentProxy;
101 
103 
104 public:
107  // Standalone mode: supports only local components/connections
109  // Networked mode: supports both local and remote components/connections
111  // Networked mode with global component manager: basically identical to
112  // LCM_CONFIG_NETWORKED configuration except that LCM runs with the
113  // global component manager on the same process.
114  LCM_CONFIG_NETWORKED_WITH_GCM
115  };
116 
117 
118 private:
120  static mtsManagerLocal * Instance;
121 
123  osaThreadId MainThreadId;
124 
126  std::stack<std::string> MainTaskNames;
127 
129  mtsTaskContinuous * CurrentMainTask;
130 
132  static bool UnitTestEnabled;
133 
136  static bool UnitTestNetworkProxyEnabled;
137 
138  ConfigurationType Configuration;
139 
142  static mtsManagerLocal * InstanceReconfiguration;
143  static mtsManagerLocal * GetSafeInstance(void);
144 
147  typedef cmnNamedMap<mtsComponent> ComponentMapType;
148  ComponentMapType ComponentMap;
149 
151  // MJ: Move this to mtsManagerLocal.cpp (for system-wide logging)
152  //osaTimeServer TimeServer;
153 
156  std::string ProcessName;
157 
159  const std::string GlobalComponentManagerIP;
160 
162  std::string ProcessIP;
163 
165  std::vector<std::string> ProcessIPList;
166 
168  osaMutex ComponentMapChange;
169 
172  static osaMutex ConfigurationChange;
173 
181  mtsManagerGlobalInterface * ManagerGlobal;
182 
184  struct {
187  } ManagerComponent;
188 
190  bool GCMConnected;
191 
193  osaThread LogThead;
194  osaThreadSignal LogTheadFinished;
195  bool LogThreadFinishWaiting;
196  void* LogDispatchThread(void * arg);
197 
199  void SetupSystemLogger(void);
200 
203  bool MCCReadyForLogForwarding(void) const;
204 
205 public:
207  static void LogDispatcher(const char * str, int len);
208 
210  static void SetLogForwarding(bool activate);
211 
213  static void GetLogForwardingState(bool & state);
214  static bool GetLogForwardingState(void);
215 
217  static bool IsLogForwardingEnabled(void);
218 
220  static bool IsLogAllowed(void);
221 
222 protected:
224  mtsManagerLocal(void);
225 
226  mtsManagerLocal(const std::string & globalComponentManagerIP,
227  const std::string & thisProcessName,
228  const std::string & thisProcessIP);
229  mtsManagerLocal(mtsManagerGlobal & globalComponentManager);
230 
232  virtual ~mtsManagerLocal();
233 
235  void Initialize(void);
236  void InitializeLocal(void);
237 
240  bool CreateManagerComponents(void);
241 
247  bool AddManagerComponent(const std::string & processName, const bool isServer = false);
248 
254  bool ConnectManagerComponentClientToServer(void);
255 
259  bool ConnectToManagerComponentClient(const std::string & componentName);
260 
262  void SetIPAddress(void);
263 
266  bool ConnectToGlobalComponentManager(void);
267 
273  bool RegisterInterfaces(mtsComponent * component);
274  bool RegisterInterfaces(const std::string & componentName);
275 
276  // PK: following two methods were part of Connect method
277  ConnectionIDType ConnectSetup(const std::string & clientComponentName, const std::string & clientInterfaceRequiredName,
278  const std::string & serverComponentName, const std::string & serverInterfaceProvidedName);
279 
280  bool ConnectNotify(ConnectionIDType connectionId,
281  const std::string & clientComponentName, const std::string & clientInterfaceRequiredName,
282  const std::string & serverComponentName, const std::string & serverInterfaceProvidedName);
283 
285  bool RemoveComponent(mtsComponent * component, const bool notifyGCM);
286  bool RemoveComponent(const std::string & componentName, const bool notifyGCM);
287 
289  // MJ: Current implemention should be reviwed -- interfaces have to be removed in a thread-safe way
290  bool RemoveInterfaceProvided(const std::string & componentName, const std::string & interfaceProvidedName);
291 
293  // MJ: Current implemention should be reviwed -- interfaces have to be removed in a thread-safe way
294  bool RemoveInterfaceRequired(const std::string & componentName, const std::string & interfaceRequiredName);
295 
296  //-------------------------------------------------------------------------
297  // Methods required by mtsManagerLocalInterface
298  //
299  // See mtsManagerLocalInterface.h for detailed documentation.
300  //-------------------------------------------------------------------------
305  bool CreateComponentProxy(const std::string & componentProxyName, const std::string & listenerID = "");
306 
312  bool RemoveComponentProxy(const std::string & componentProxyName, const std::string & listenerID = "");
313 
318  const std::string & serverComponentProxyName,
319  const mtsInterfaceProvidedDescription & providedInterfaceDescription, const std::string & listenerID = "");
320 
324  const std::string & clientComponentProxyName,
325  const mtsInterfaceRequiredDescription & requiredInterfaceDescription, const std::string & listenerID = "");
326 
329  const std::string & componentProxyName, const std::string & providedInterfaceProxyName, const std::string & listenerID = "");
330 
333  const std::string & componentProxyName, const std::string & requiredInterfaceProxyName, const std::string & listenerID = "");
334 
336  bool ConnectServerSideInterface(const mtsDescriptionConnection & description, const std::string & listenerID = "");
337 
339  bool ConnectClientSideInterface(const mtsDescriptionConnection & description, const std::string & listenerID = "");
340 
343  const std::string & serverComponentName,
344  const std::string & providedInterfaceName,
345  mtsInterfaceProvidedDescription & providedInterfaceDescription, const std::string & listenerID = "");
346 
350  const std::string & componentName,
351  const std::string & requiredInterfaceName,
352  mtsInterfaceRequiredDescription & requiredInterfaceDescription, const std::string & listenerID = "");
353 
355  inline void SetGCMConnected(const bool connected) {
356  GCMConnected = connected;
357  }
358 
359 public:
360  //-------------------------------------------------------------------------
361  // Component Management
362  //-------------------------------------------------------------------------
364  mtsComponent * CreateComponentDynamically(const std::string & className, const std::string & componentName,
365  const std::string & constructorArgSerialized);
366 
369  bool AddComponent(mtsComponent * component);
370  bool CISST_DEPRECATED AddTask(mtsTask * component); // For backward compatibility
371  bool CISST_DEPRECATED AddDevice(mtsComponent * component); // For backward compatibility
372 
374  bool RemoveComponent(mtsComponent * component);
375  bool RemoveComponent(const std::string & componentName);
376 
378  mtsComponent * GetComponent(const std::string & componentName) const;
379  mtsTask * GetComponentAsTask(const std::string & componentName) const;
380 
381  mtsComponent CISST_DEPRECATED * GetDevice(const std::string & deviceName); // For backward compatibility
382  mtsTask CISST_DEPRECATED * GetTask(const std::string & taskName); // For backward compatibility
383 
385  bool FindComponent(const std::string & componentName) const;
386 
390  bool WaitForStateAll(mtsComponentState desiredState, double timeout = 3.0 * cmn_minute) const;
391 
394  void CreateAll(void);
395 
397  bool CreateAllAndWait(double timeoutInSeconds);
398 
401  void StartAll(void);
402 
404  bool StartAllAndWait(double timeoutInSeconds);
405 
408  void KillAll(void);
409 
411  bool KillAllAndWait(double timeoutInSeconds);
412 
418  void Cleanup(void);
419 
420  //-------------------------------------------------------------------------
421  // Connection Management
422  //-------------------------------------------------------------------------
432  bool Connect(const std::string & clientComponentName, const std::string & clientInterfaceRequiredName,
433  const std::string & serverComponentName, const std::string & serverInterfaceProvidedName);
434 
458  bool Connect(const std::string & clientProcessName, const std::string & clientComponentName,
459  const std::string & clientInterfaceRequiredName,
460  const std::string & serverProcessName, const std::string & serverComponentName,
461  const std::string & serverInterfaceProvidedName,
462  const unsigned int retryCount = 10);
463 
465  bool Disconnect(const ConnectionIDType connectionID);
466 
467  bool Disconnect(const std::string & clientComponentName, const std::string & clientInterfaceRequiredName,
468  const std::string & serverComponentName, const std::string & serverInterfaceProvidedName);
469 
470  bool Disconnect(const std::string & clientProcessName, const std::string & clientComponentName,
471  const std::string & clientInterfaceRequiredName,
472  const std::string & serverProcessName, const std::string & serverComponentName,
473  const std::string & serverInterfaceProvidedName);
474 
475  //-------------------------------------------------------------------------
476  // Getters and Utilities
477  //-------------------------------------------------------------------------
479  static const std::string ProcessNameOfLCMDefault;
480 
482  static const std::string ProcessNameOfLCMWithGCM;
483 
493  static mtsManagerLocal * GetInstance(void);
494 
504  static mtsManagerLocal * GetInstance(const std::string & globalComponentManagerIP,
505  const std::string & thisProcessName = "",
506  const std::string & thisProcessIP = "");
507 
511  static mtsManagerLocal * GetInstance(mtsManagerGlobal & globalComponentManager);
512 
514  std::vector<std::string> GetNamesOfComponents(void) const;
515  void GetNamesOfComponents(std::vector<std::string>& namesOfComponents) const;
516 
518 #if 0
519  inline const osaTimeServer & GetTimeServer(void) const {
520  return TimeServer;
521  }
522 #endif
523  const osaTimeServer & GetTimeServer(void) const;
524 
526  inline const std::string GetProcessName(const std::string & CMN_UNUSED(listenerID) = "") const {
527  return ProcessName;
528  }
529 
532  return Configuration;
533  }
534 
536  inline bool IsGCMActive(void) const {
537  return GCMConnected;
538  }
539 
543  void SetMainThreadId(void) { MainThreadId = osaGetCurrentThreadId(); }
544 
548  void SetMainThreadId(const osaThreadId &threadId) { MainThreadId = threadId; }
549 
551  osaThreadId GetMainThreadId(void) const { return MainThreadId; }
552 
554  void PushCurrentMainTask(mtsTaskContinuous *cur);
555 
557  mtsTaskContinuous *PopCurrentMainTask();
558 
560  mtsTaskContinuous *GetCurrentMainTask(void) const { return CurrentMainTask; }
561 
563  void GetNamesOfCommands(std::vector<std::string>& namesOfCommands,
564  const std::string & componentName,
565  const std::string & providedInterfaceName,
566  const std::string & CMN_UNUSED(listenerID) = "");
567 
569  void GetNamesOfEventGenerators(std::vector<std::string>& namesOfEventGenerators,
570  const std::string & componentName,
571  const std::string & providedInterfaceName,
572  const std::string & CMN_UNUSED(listenerID) = "");
573 
575  void GetNamesOfFunctions(std::vector<std::string>& namesOfFunctions,
576  const std::string & componentName,
577  const std::string & requiredInterfaceName,
578  const std::string & CMN_UNUSED(listenerID) = "");
579 
581  void GetNamesOfEventHandlers(std::vector<std::string>& namesOfEventHandlers,
582  const std::string & componentName,
583  const std::string & requiredInterfaceName,
584  const std::string & CMN_UNUSED(listenerID) = "");
585 
587  void GetDescriptionOfCommand(std::string & description,
588  const std::string & componentName,
589  const std::string & providedInterfaceName,
590  const std::string & commandName,
591  const std::string & CMN_UNUSED(listenerID) = "");
592 
594  void GetDescriptionOfEventGenerator(std::string & description,
595  const std::string & componentName,
596  const std::string & providedInterfaceName,
597  const std::string & eventGeneratorName,
598  const std::string & CMN_UNUSED(listenerID) = "");
599 
601  void GetDescriptionOfFunction(std::string & description,
602  const std::string & componentName,
603  const std::string & requiredInterfaceName,
604  const std::string & functionName,
605  const std::string & CMN_UNUSED(listenerID) = "");
606 
608  void GetDescriptionOfEventHandler(std::string & description,
609  const std::string & componentName,
610  const std::string & requiredInterfaceName,
611  const std::string & eventHandlerName,
612  const std::string & CMN_UNUSED(listenerID) = "");
613 
615  inline const std::string & GetIPAddress(void) const { return ProcessIP; }
616 
618  static std::vector<std::string> GetIPAddressList(void);
619  static void GetIPAddressList(std::vector<std::string> & ipAddresses);
620 
622  inline const std::string GetName(void) const {
623  return GetProcessName();
624  }
625 
627  bool SetInterfaceProvidedProxyAccessInfo(const ConnectionIDType connectionID, const std::string & endpointInfo);
628 
629  //returns the list or processes in the system and their absolute time differences relative to GCM
630  bool GetGCMProcTimeSyncInfo(std::vector<std::string> &processNames, std::vector<double> &timeOffsets);
631 
633  void /*CISST_DEPRECATED*/ ToStream(std::ostream & outputStream) const;
634 
637  void /*CISST_DEPRECATED*/ ToStreamDot(std::ostream & outputStream) const;
638 };
639 
641 
642 #endif // _mtsManagerLocal_h
643 
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
virtual bool GetInterfaceProvidedDescription(const std::string &serverComponentName, const std::string &providedInterfaceName, mtsInterfaceProvidedDescription &providedInterfaceDescription, const std::string &listenerID="")=0
Extract all information about provided interface such as command objects and event generators...
virtual bool RemoveComponentProxy(const std::string &componentProxyName, const std::string &listenerID="")=0
Declaration of osaMutex.
Define a Mutex object.
Definition: osaMutex.h:48
#define CISST_DEPRECATED
Definition: cmnPortability.h:310
virtual bool GetInterfaceRequiredDescription(const std::string &componentName, const std::string &requiredInterfaceName, mtsInterfaceRequiredDescription &requiredInterfaceDescription, const std::string &listenerID="")=0
Extract all information about required interface such as function objects and event handlers...
virtual const std::string GetProcessName(const std::string &listenerID="") const =0
ConfigurationType GetConfiguration(void) const
Definition: mtsManagerLocal.h:531
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
static const std::string ProcessNameOfLCMDefault
Definition: mtsManagerLocal.h:479
void SetMainThreadId(void)
Definition: mtsManagerLocal.h:543
virtual bool ConnectClientSideInterface(const mtsDescriptionConnection &description, const std::string &listenerID="")=0
Connect interfaces at client side.
mtsTaskContinuous * GetCurrentMainTask(void) const
Definition: mtsManagerLocal.h:560
Definition: mtsManagerGlobalInterface.h:43
Declaration of cmnNamedMap.
unsigned int ConnectionIDType
Definition: mtsForwardDeclarations.h:150
Definition: mtsInterfaceCommon.h:1046
mtsManagerComponentServer * Server
Definition: mtsManagerLocal.h:186
Declaration of osaThread.
Define a thread object.
Definition: osaThread.h:164
Declaration of units and unit conversion methodsThis file include the definition and implementation o...
virtual bool CreateInterfaceRequiredProxy(const std::string &clientComponentProxyName, const mtsInterfaceRequiredDescription &requiredInterfaceDescription, const std::string &listenerID="")=0
virtual bool ConnectServerSideInterface(const mtsDescriptionConnection &description, const std::string &listenerID="")=0
Connect interfaces at server side.
bool IsGCMActive(void) const
Definition: mtsManagerLocal.h:536
Definition: mtsManagerLocal.h:89
virtual bool RemoveInterfaceRequiredProxy(const std::string &componentProxyName, const std::string &requiredInterfaceProxyName, const std::string &listenerID="")=0
Declaration of mtsManagerGlobalInterfaceThis class declares an interface used by local component mana...
CISST_EXPORT osaThreadId osaGetCurrentThreadId(void)
Declaration of Manager Component ClientThis class defines the manager component client which is manag...
Definition: mtsManagerComponentClient.h:41
osaThreadId GetMainThreadId(void) const
Definition: mtsManagerLocal.h:551
virtual void GetNamesOfFunctions(std::vector< std::string > &namesOfFunctions, const std::string &componentName, const std::string &requiredInterfaceName, const std::string &listenerID="")=0
virtual void GetDescriptionOfEventGenerator(std::string &description, const std::string &componentName, const std::string &providedInterfaceName, const std::string &eventGeneratorName, const std::string &listenerID="")=0
virtual void GetNamesOfCommands(std::vector< std::string > &namesOfCommands, const std::string &componentName, const std::string &providedInterfaceName, const std::string &listenerID="")=0
Definition: mtsInterfaceCommon.h:928
Definition: mtsParameterTypes.h:533
Definition: mtsTask.h:60
virtual bool RemoveInterfaceProvidedProxy(const std::string &componentProxyName, const std::string &providedInterfaceProxyName, const std::string &listenerID="")=0
virtual void ToStream(std::ostream &outputStream) const
Forward declarations and #define for cisstMultiTask.
const std::string & GetIPAddress(void) const
Definition: mtsManagerLocal.h:615
Definition: mtsComponent.h:150
virtual void GetDescriptionOfEventHandler(std::string &description, const std::string &componentName, const std::string &requiredInterfaceName, const std::string &eventHandlerName, const std::string &listenerID="")=0
virtual void GetDescriptionOfFunction(std::string &description, const std::string &componentName, const std::string &requiredInterfaceName, const std::string &functionName, const std::string &listenerID="")=0
ThreadId type.
Definition: osaThread.h:77
Definition: mtsComponentState.h:34
virtual void GetDescriptionOfCommand(std::string &description, const std::string &componentName, const std::string &providedInterfaceName, const std::string &commandName, const std::string &listenerID="")=0
#define CMN_DECLARE_SERVICES(hasDynamicCreation, lod)
Definition: cmnClassRegisterMacros.h:116
Declaration of osaThreadBuddy.
virtual bool CreateComponentProxy(const std::string &componentProxyName, const std::string &listenerID="")=0
void SetMainThreadId(const osaThreadId &threadId)
Definition: mtsManagerLocal.h:548
Definition of mtsManagerLocalInterfaceThis class defines an interface used by the global component ma...
Definition: mtsManagerComponentServer.h:45
static const std::string ProcessNameOfLCMWithGCM
Definition: mtsManagerLocal.h:482
Class for relative time.
Definition: osaTimeServer.h:73
void SetGCMConnected(const bool connected)
Definition: mtsManagerLocal.h:355
virtual void GetNamesOfEventGenerators(std::vector< std::string > &namesOfEventGenerators, const std::string &componentName, const std::string &providedInterfaceName, const std::string &listenerID="")=0
Definition: mtsManagerLocal.h:108
Rules of exporting.
ConfigurationType
Definition: mtsManagerLocal.h:106
Definition: osaThreadSignal.h:40
Definition: mtsTaskContinuous.h:77
Definition: mtsManagerGlobal.h:51
virtual void GetNamesOfEventHandlers(std::vector< std::string > &namesOfEventHandlers, const std::string &componentName, const std::string &requiredInterfaceName, const std::string &listenerID="")=0
const double cmn_minute
Definition: cmnUnits.h:217
const int CMN_NO_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:328
#define CMN_DECLARE_SERVICES_INSTANTIATION(className)
Definition: cmnClassRegisterMacros.h:202
virtual bool CreateInterfaceProvidedProxy(const std::string &serverComponentProxyName, const mtsInterfaceProvidedDescription &providedInterfaceDescription, const std::string &listenerID="")=0
Definition: mtsManagerLocal.h:110
Definition: mtsManagerLocalInterface.h:54
const std::string GetName(void) const
Definition: mtsManagerLocal.h:622
#define CMN_LOG_ALLOW_DEFAULT
Definition: cmnLogLoD.h:76
mtsManagerComponentClient * Client
Definition: mtsManagerLocal.h:185
const std::string GetProcessName(const std::string &CMN_UNUSED(listenerID)="") const
Definition: mtsManagerLocal.h:526