YARP
Yet Another Robot Platform
executable.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_MANAGER_Executable
7 #define YARP_MANAGER_Executable
8 
9 #include <string>
10 #include <vector>
11 
12 #include <yarp/os/PeriodicThread.h>
13 #include <yarp/os/Thread.h>
14 #include <yarp/os/Semaphore.h>
15 
16 #include <yarp/manager/ymm-types.h>
17 #include <yarp/manager/broker.h>
18 #include <yarp/manager/module.h>
20 #include <yarp/manager/execstate.h>
21 
22 
23 namespace yarp {
24 namespace manager {
25 
26 
27 #define DEF_PERIOD 0.1 //s
28 #define WDOG_PERIOD 5.0 //s
29 
30 typedef enum __RSTATE {
37  STUNKNOWN
39 
40 enum class BrokerType
41 {
42  invalid,
43  local,
44  yarp
45 };
46 
47 
48 class MEvent{
49 
50 public:
51  MEvent() {}
52  virtual ~MEvent() = default;
53  virtual void onExecutableStart(void* which) {}
54  virtual void onExecutableStop(void* which) {}
55  virtual void onExecutableDied(void* which) {}
56  virtual void onExecutableFailed(void* which) {}
57  virtual void onExecutableStdout(void* which, const char* msg) {}
58  virtual void onCnnStablished(void* which) {}
59  virtual void onCnnReleased(void* which) {}
60  virtual void onCnnFailed(void* which) {}
61  virtual void onError(void* which) {}
62 };
63 
64 
65 class ConcurentWrapper;
66 class ConcurentRateWrapper;
67 
72 {
73 public:
74  Executable(Broker* _broker, MEvent* _event, Module* module, bool bWatchDog=true);
75  ~Executable() override;
76 
77  bool start();
78  void stop();
79  void kill();
80 
81  void setID(int id) { theID = id;}
82  void setCommand(const char* val) { if(val) { strCommand = val; } }
83  void setParam(const char* val) { if(val) { strParam = val; } }
84  void setHost(const char* val) { if(val) { strHost = val; } }
85  void setStdio(const char* val) { if(val) { strStdio = val; } }
86  void setWorkDir(const char* val) { if(val) { strWorkdir = val; } }
87  void setEnv(const char* val) { if(val) { strEnv = val; } }
88 
89  void addConnection(Connection &cnn) { connections.push_back(cnn); }
90  CnnContainer& getConnections() { return connections;}
91  void addResource(ResYarpPort &res) { resources.push_back(res); }
92  ResourceContainer& getResources() { return resources; }
93 
94  RSTATE state();
96  bool shouldChangeBroker();
97  Broker* getBroker() { return broker; }
98  void setAndInitializeBroker(Broker* _broker);
99  void removeBroker() { delete broker;}
100 
101  MEvent* getEvent() { return event; }
102  const char* getCommand() { return strCommand.c_str(); }
103  const char* getParam() { return strParam.c_str(); }
104  const char* getHost() { return strHost.c_str(); }
105  const char* getStdio() { return strStdio.c_str(); }
106  const char* getWorkDir() { return strWorkdir.c_str(); }
107  const char* getEnv() { return strEnv.c_str(); }
108  int getID() { return theID; }
109  Module* getModule() { return module; }
110 
111  void setPostExecWait(double t) { waitStart = t; }
112  double getPostExecWait() { return waitStart; }
113  void setPostStopWait(double t) { waitStop = t; }
114  double getPostStopWait() { return waitStop; }
115 
116  void setOriginalPostExecWait(double t){ originalWaitStart = t; }
117  void restoreOriginalPostExecWait(){ waitStart = originalWaitStart; }
118  void setOriginalPostStopWait(double t){ originalWaitStop = t; }
119  void restoreOriginalPostStopWait(){ waitStop = originalWaitStop; }
120 
121  void enableAutoConnect() { bAutoConnect = true; }
122  void disableAutoConnect() { bAutoConnect = false; }
123  bool autoConnect() { return bAutoConnect; }
124 
125  bool startWatchDog();
126  void stopWatchDog();
127 
128 public: // from BrokerEventSink
129  void onBrokerStdout(const char* msg) override;
130 
131 private:
132  bool bAutoConnect;
133  std::string strCommand;
134  std::string strParam;
135  std::string strHost;
136  std::string strStdio;
137  std::string strWorkdir;
138  std::string strEnv;
139  int theID;
140  double waitStart;
141  double waitStop;
142  double originalWaitStart;
143  double originalWaitStop;
144  bool bWatchDog;
145  Broker* broker;
146  MEvent* event;
147  Module* module;
148  CnnContainer connections;
149  ResourceContainer resources;
150 
151  ExecMachine* execMachine;
152  ErrorLogger* logger;
153  ConcurentWrapper* startWrapper;
154  ConcurentWrapper* stopWrapper;
155  ConcurentWrapper* killWrapper;
156  ConcurentRateWrapper* watchdogWrapper;
157  yarp::os::Semaphore semInitialize;
158 
159  void startImplement();
160  void stopImplement();
161  void killImplement();
162  void watchdogImplement();
163  bool initialize();
164 };
165 
166 
167 typedef std::vector<Executable*> ExecutablePContainer;
168 typedef std::vector<Executable*>::iterator ExecutablePIterator;
169 typedef void (Executable::*ExecutableFuncPtr)();
170 
172 {
173 public:
175  : labor(ptrLabor), executable(ptrExecutable) { }
176 
177  ~ConcurentWrapper() override { if(isRunning()) { stop(); } }
178 
179 
180  void run() override {
181  if(labor && executable) {
182  (executable->*labor)();
183  }
184  }
185 
186  //bool threadInit();
188 
189 private:
190  ExecutableFuncPtr labor;
191  Executable* executable;
192 };
193 
194 
196 {
197 public:
198 
200  : PeriodicThread(WDOG_PERIOD), labor(ptrLabor), executable(ptrExecutable) { }
201 
202  ~ConcurentRateWrapper() override { if(isRunning()) { stop(); } }
203 
204 
205  void run() override {
206  if(labor && executable) {
207  (executable->*labor)();
208  }
209  }
210 
211  //bool threadInit();
213 
214 private:
215  ExecutableFuncPtr labor;
216  Executable* executable;
217 };
218 
219 } // namespace yarp
220 } // namespace manager
221 
222 
223 #endif // __YARP_MANAGER_Executable__
float t
Class Broker.
Definition: broker.h:31
void run() override
Loop function.
Definition: executable.h:205
ConcurentRateWrapper(Executable *ptrExecutable, ExecutableFuncPtr ptrLabor)
Definition: executable.h:199
ConcurentWrapper(Executable *ptrExecutable, ExecutableFuncPtr ptrLabor)
Definition: executable.h:174
void run() override
Main body of the new thread.
Definition: executable.h:180
Class Connection.
Definition: application.h:57
Singleton class ErrorLogger.
Definition: utility.h:57
Class ExecMachine.
Definition: execstate.h:197
Class Executable.
Definition: executable.h:72
void onBrokerStdout(const char *msg) override
Definition: executable.cpp:235
const char * getStdio()
Definition: executable.h:105
void setParam(const char *val)
Definition: executable.h:83
void setWorkDir(const char *val)
Definition: executable.h:86
void setOriginalPostStopWait(double t)
Definition: executable.h:118
void setPostStopWait(double t)
Definition: executable.h:113
const char * getCommand()
Definition: executable.h:102
const char * getWorkDir()
Definition: executable.h:106
const char * getEnv()
Definition: executable.h:107
Executable(Broker *_broker, MEvent *_event, Module *module, bool bWatchDog=true)
Definition: executable.cpp:11
void setPostExecWait(double t)
Definition: executable.h:111
const char * getParam()
Definition: executable.h:103
ResourceContainer & getResources()
Definition: executable.h:92
void setOriginalPostExecWait(double t)
Definition: executable.h:116
CnnContainer & getConnections()
Definition: executable.h:90
void setHost(const char *val)
Definition: executable.h:84
void setStdio(const char *val)
Definition: executable.h:85
const char * getHost()
Definition: executable.h:104
void setEnv(const char *val)
Definition: executable.h:87
void addResource(ResYarpPort &res)
Definition: executable.h:91
void setCommand(const char *val)
Definition: executable.h:82
void addConnection(Connection &cnn)
Definition: executable.h:89
void setAndInitializeBroker(Broker *_broker)
Definition: executable.cpp:210
virtual void onExecutableStdout(void *which, const char *msg)
Definition: executable.h:57
virtual void onCnnFailed(void *which)
Definition: executable.h:60
virtual void onExecutableFailed(void *which)
Definition: executable.h:56
virtual void onExecutableStop(void *which)
Definition: executable.h:54
virtual void onExecutableDied(void *which)
Definition: executable.h:55
virtual void onCnnStablished(void *which)
Definition: executable.h:58
virtual void onCnnReleased(void *which)
Definition: executable.h:59
virtual ~MEvent()=default
virtual void onExecutableStart(void *which)
Definition: executable.h:53
virtual void onError(void *which)
Definition: executable.h:61
Class Module.
Definition: module.h:100
An abstraction for a periodic thread.
PeriodicThread(double period, ShouldUseSystemClock useSystemClock=ShouldUseSystemClock::No, PeriodicThreadClock clockAccuracy=PeriodicThreadClock::Relative)
Constructor.
bool isRunning() const
Returns true when the thread is started, false otherwise.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
A class for thread synchronization and mutual exclusion.
Definition: Semaphore.h:26
An abstraction for a thread of execution.
Definition: Thread.h:22
bool stop()
Stop the thread.
Definition: Thread.cpp:81
bool isRunning()
Returns true if the thread is running (Thread::start has been called successfully and the thread has ...
Definition: Thread.cpp:105
#define WDOG_PERIOD
Definition: executable.h:28
std::vector< Executable * >::iterator ExecutablePIterator
Definition: executable.h:168
enum yarp::manager::__RSTATE RSTATE
std::vector< ResYarpPort > ResourceContainer
Definition: application.h:153
std::vector< Executable * > ExecutablePContainer
Definition: executable.h:167
void(Executable::* ExecutableFuncPtr)()
Definition: executable.h:169
std::vector< Connection > CnnContainer
Definition: application.h:150
The main, catch-all namespace for YARP.
Definition: dirs.h:16