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__
yarp::manager::ExecutableFuncPtr
void(Executable::* ExecutableFuncPtr)()
Definition: executable.h:172
yarp::manager::DYING
@ DYING
Definition: executable.h:38
yarp::manager::BrokerType::local
@ local
yarp::manager::__RSTATE
__RSTATE
Definition: executable.h:33
yarp::manager::Executable::getPostExecWait
double getPostExecWait()
Definition: executable.h:115
yarp::os::Semaphore
A class for thread synchronization and mutual exclusion.
Definition: Semaphore.h:29
yarp::manager::MEvent::onCnnStablished
virtual void onCnnStablished(void *which)
Definition: executable.h:61
yarp::manager::Executable::Executable
Executable(Broker *_broker, MEvent *_event, Module *module, bool bWatchDog=true)
Definition: executable.cpp:14
yarp::manager::ConcurentWrapper::run
void run() override
Main body of the new thread.
Definition: executable.h:183
t
float t
Definition: FfmpegWriter.cpp:74
yarp::manager::Connection
Class Connection.
Definition: application.h:60
yarp::manager::Executable::setOriginalPostStopWait
void setOriginalPostStopWait(double t)
Definition: executable.h:121
yarp::manager::Executable::restoreOriginalPostExecWait
void restoreOriginalPostExecWait()
Definition: executable.h:120
yarp::manager::CnnContainer
std::vector< Connection > CnnContainer
Definition: application.h:153
yarp::manager::MEvent::MEvent
MEvent()
Definition: executable.h:54
yarp::manager::Executable::getHost
const char * getHost()
Definition: executable.h:107
execstate.h
yarp::manager::Executable::shouldChangeBroker
bool shouldChangeBroker()
Definition: executable.cpp:185
yarp::manager::ConcurentRateWrapper
Definition: executable.h:199
yarp::manager::Executable::getModule
Module * getModule()
Definition: executable.h:112
yarp::manager::ConcurentRateWrapper::~ConcurentRateWrapper
~ConcurentRateWrapper() override
Definition: executable.h:205
yarp::manager::Executable::onBrokerStdout
void onBrokerStdout(const char *msg) override
Definition: executable.cpp:223
yarp::manager::Executable::startWatchDog
bool startWatchDog()
Definition: executable.cpp:210
yarp::manager::READY
@ READY
Definition: executable.h:35
yarp::manager::Executable::setPostExecWait
void setPostExecWait(double t)
Definition: executable.h:114
yarp::manager::BrokerType::invalid
@ invalid
module.h
yarp::manager::MEvent::onCnnReleased
virtual void onCnnReleased(void *which)
Definition: executable.h:62
yarp::manager::Executable::~Executable
~Executable() override
Definition: executable.cpp:40
yarp::manager::Executable::getResources
ResourceContainer & getResources()
Definition: executable.h:95
yarp::os::PeriodicThread::PeriodicThread
PeriodicThread(double period, ShouldUseSystemClock useSystemClock=ShouldUseSystemClock::No)
Constructor.
Definition: PeriodicThread.cpp:271
yarp::manager::ErrorLogger
Singleton class ErrorLogger.
Definition: utility.h:60
yarp::manager::Broker
Class Broker.
Definition: broker.h:34
yarp::manager::Executable::setStdio
void setStdio(const char *val)
Definition: executable.h:88
yarp::manager::Executable::addResource
void addResource(ResYarpPort &res)
Definition: executable.h:94
yarp::os::Thread
An abstraction for a thread of execution.
Definition: Thread.h:25
yarp::manager::RSTATE
enum yarp::manager::__RSTATE RSTATE
yarp::manager::Executable::getParam
const char * getParam()
Definition: executable.h:106
yarp::manager::Executable::setPostStopWait
void setPostStopWait(double t)
Definition: executable.h:116
yarp::manager::Executable::kill
void kill()
Definition: executable.cpp:122
yarp::os::PeriodicThread::isRunning
bool isRunning() const
Returns true when the thread is started, false otherwise.
Definition: PeriodicThread.cpp:316
yarp::manager::Executable::getEnv
const char * getEnv()
Definition: executable.h:110
yarp::manager::Executable::stop
void stop()
Definition: executable.cpp:102
yarp::manager::Executable::getStdio
const char * getStdio()
Definition: executable.h:108
yarp::manager::SUSPENDED
@ SUSPENDED
Definition: executable.h:34
yarp::manager::Module
Class Module.
Definition: module.h:103
yarp::os::Thread::isRunning
bool isRunning()
Returns true if the thread is running (Thread::start has been called successfully and the thread has ...
Definition: Thread.cpp:108
yarp::manager::ResourceContainer
std::vector< ResYarpPort > ResourceContainer
Definition: application.h:156
yarp::manager::Executable::stopWatchDog
void stopWatchDog()
Definition: executable.cpp:218
yarp::manager::Executable::setHost
void setHost(const char *val)
Definition: executable.h:87
yarp::manager::STUNKNOWN
@ STUNKNOWN
Definition: executable.h:40
yarp::manager::Executable::restoreOriginalPostStopWait
void restoreOriginalPostStopWait()
Definition: executable.h:122
yarp::manager::MEvent::onExecutableStart
virtual void onExecutableStart(void *which)
Definition: executable.h:56
yarp::manager::MEvent
Definition: executable.h:51
yarp::manager::ExecutablePIterator
std::vector< Executable * >::iterator ExecutablePIterator
Definition: executable.h:171
yarp::manager::BrokerType
BrokerType
Definition: executable.h:44
ymm-types.h
yarp::manager::MEvent::onExecutableDied
virtual void onExecutableDied(void *which)
Definition: executable.h:58
yarp::manager::ResYarpPort
Definition: logicresource.h:47
yarp::manager::RUNNING
@ RUNNING
Definition: executable.h:37
Thread.h
yarp::manager::Executable::setEnv
void setEnv(const char *val)
Definition: executable.h:90
yarp::manager::Executable::addConnection
void addConnection(Connection &cnn)
Definition: executable.h:92
yarp::manager::ConcurentRateWrapper::run
void run() override
Loop function.
Definition: executable.h:208
Semaphore.h
yarp::manager::Executable::getEvent
MEvent * getEvent()
Definition: executable.h:104
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
yarp::manager::Executable::getBroker
Broker * getBroker()
Definition: executable.h:100
yarp::manager::Executable::getWorkDir
const char * getWorkDir()
Definition: executable.h:109
PeriodicThread.h
yarp::manager::ConcurentWrapper
Definition: executable.h:175
yarp::manager::MEvent::onExecutableStdout
virtual void onExecutableStdout(void *which, const char *msg)
Definition: executable.h:60
yarp::manager::MEvent::onError
virtual void onError(void *which)
Definition: executable.h:64
yarp::manager::Executable::getID
int getID()
Definition: executable.h:111
yarp::manager::Executable::disableAutoConnect
void disableAutoConnect()
Definition: executable.h:125
yarp::manager::Executable::getBrokerType
BrokerType getBrokerType()
Definition: executable.cpp:168
yarp::manager::CONNECTING
@ CONNECTING
Definition: executable.h:36
yarp::manager::Executable::setWorkDir
void setWorkDir(const char *val)
Definition: executable.h:89
yarp::manager::Executable::setOriginalPostExecWait
void setOriginalPostExecWait(double t)
Definition: executable.h:119
yarp::manager::MEvent::onExecutableStop
virtual void onExecutableStop(void *which)
Definition: executable.h:57
WDOG_PERIOD
#define WDOG_PERIOD
Definition: executable.h:31
yarp::manager::MEvent::~MEvent
virtual ~MEvent()=default
yarp::manager::Executable::start
bool start()
Definition: executable.cpp:79
yarp::manager::ConcurentWrapper::ConcurentWrapper
ConcurentWrapper(Executable *ptrExecutable, ExecutableFuncPtr ptrLabor)
Definition: executable.h:177
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::manager::Executable::getCommand
const char * getCommand()
Definition: executable.h:105
broker.h
yarp::manager::Executable::setCommand
void setCommand(const char *val)
Definition: executable.h:85
yarp::os::PeriodicThread::stop
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
Definition: PeriodicThread.cpp:296
yarp::manager::Executable::setID
void setID(int id)
Definition: executable.h:84
yarp::manager::Executable::enableAutoConnect
void enableAutoConnect()
Definition: executable.h:124
yarp::manager::Executable::setAndInitializeBroker
void setAndInitializeBroker(Broker *_broker)
Definition: executable.cpp:201
yarp::manager::ExecutablePContainer
std::vector< Executable * > ExecutablePContainer
Definition: executable.h:170
yarp::manager::ConcurentWrapper::~ConcurentWrapper
~ConcurentWrapper() override
Definition: executable.h:180
yarp::os::Thread::stop
bool stop()
Stop the thread.
Definition: Thread.cpp:84
yarp::manager::ConcurentRateWrapper::ConcurentRateWrapper
ConcurentRateWrapper(Executable *ptrExecutable, ExecutableFuncPtr ptrLabor)
Definition: executable.h:202
yarp::manager::Executable::state
RSTATE state()
Definition: executable.cpp:139
yarp::manager::ExecMachine
Class ExecMachine.
Definition: execstate.h:200
yarp::manager::BrokerEventSink
Definition: broker.h:23
yarp::manager::Executable::getConnections
CnnContainer & getConnections()
Definition: executable.h:93
yarp::manager::DEAD
@ DEAD
Definition: executable.h:39
yarp::manager::Executable
Class Executable.
Definition: executable.h:75
yarp::manager::Executable::removeBroker
void removeBroker()
Definition: executable.h:102
yarp::manager::MEvent::onExecutableFailed
virtual void onExecutableFailed(void *which)
Definition: executable.h:59
yarp::manager::Executable::getPostStopWait
double getPostStopWait()
Definition: executable.h:117
yarp::manager::Executable::setParam
void setParam(const char *val)
Definition: executable.h:86
application.h
yarp::manager::Executable::autoConnect
bool autoConnect()
Definition: executable.h:126
yarp::manager::MEvent::onCnnFailed
virtual void onCnnFailed(void *which)
Definition: executable.h:63