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