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