YARP
Yet Another Robot Platform
executable.cpp
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 
8 
9 using namespace yarp::manager;
10 
12  bool _bWatchDog)
13 {
14  bAutoConnect = true;
15  broker = _broker;
16  event = _event;
17  bWatchDog = _bWatchDog;
18  waitStart = 0.0;
19  waitStop = 0.0;
20  originalWaitStart = 0.0;
21  originalWaitStop = 0.0;
22  Executable::module = module;
23  logger = ErrorLogger::Instance();
24  broker->setEventSink(dynamic_cast<BrokerEventSink*>(this));
25  execMachine = new ExecMachine(this);
26  startWrapper = new ConcurentWrapper(this, &Executable::startImplement);
27  stopWrapper = new ConcurentWrapper(this, &Executable::stopImplement);
28  killWrapper = new ConcurentWrapper(this, &Executable::killImplement);
29  theID = -1;
30 
31  if (bWatchDog) {
32  watchdogWrapper = new ConcurentRateWrapper(this, &Executable::watchdogImplement);
33  } else {
34  watchdogWrapper = nullptr;
35  }
36 }
37 
39 {
40  if (watchdogWrapper) {
41  delete watchdogWrapper;
42  }
43  delete startWrapper;
44  delete stopWrapper;
45  delete killWrapper;
46  delete execMachine;
47  removeBroker();
48 }
49 
50 
51 bool Executable::initialize()
52 {
53  __CHECK_NULLPTR(broker);
54  __CHECK_NULLPTR(event);
55 
56  semInitialize.wait();
57  bool ret = broker->init(strCommand.c_str(),
58  strParam.c_str(),
59  strHost.c_str(),
60  strStdio.c_str(),
61  strWorkdir.c_str(),
62  strEnv.c_str());
63  semInitialize.post();
64  if(!ret)
65  {
66  OSTRINGSTREAM msg;
67  msg<<"cannot initialize broker. : ";
68  if (broker->error()) {
69  msg << broker->error();
70  }
71  logger->addError(msg);
72  event->onExecutableDied(this);
73  return false;
74  }
75  return true;
76 }
77 
78 
80 {
81  if(!initialize()) {
82  event->onExecutableDied(this);
83  return false;
84  }
85 
86  if(!startWrapper->isRunning()) {
87  startWrapper->start();
88  return true;
89  }
90  return false;
91 }
92 
93 
94 void Executable::startImplement()
95 {
96  execMachine->start();
97  execMachine->startModule();
98  execMachine->connectAllPorts();
99 }
100 
101 
103 {
104  if (!broker->initialized()) {
105  initialize();
106  }
107 
108  if(!stopWrapper->isRunning()) {
109  stopWatchDog();
110  stopWrapper->start();
111  }
112 }
113 
114 void Executable::stopImplement()
115 {
116  execMachine->stop();
117  execMachine->disconnectAllPorts();
118  execMachine->refresh();
119  execMachine->stopModule();
120 }
121 
122 
124 {
125  if (!broker->initialized()) {
126  initialize();
127  }
128 
129  // Notice that kill can be called from multiple threads
130  stopWatchDog();
131  killWrapper->start();
132 }
133 
134 void Executable::killImplement()
135 {
136  execMachine->kill();
137  execMachine->killModule();
138 }
139 
140 
142 {
143 
144  if (!broker->initialized()) {
145  initialize();
146  }
147 
148  // Updating real module state on demand
149  // Notice that this is a blocking method
150  execMachine->refresh();
151 
152  const char* strState = execMachine->currentState()->getName();
153 
154  if (compareString(strState, "SUSPENDED")) {
155  return SUSPENDED;
156  }
157  if (compareString(strState, "READY")) {
158  return READY;
159  }
160  if (compareString(strState, "CONNECTING")) {
161  return CONNECTING;
162  }
163  if (compareString(strState, "RUNNING")) {
164  return RUNNING;
165  }
166  if (compareString(strState, "DEAD")) {
167  return DEAD;
168  }
169  if (compareString(strState, "DYING")) {
170  return DYING;
171  }
172 
173  std::cerr<<"Unknown state!"<<std::endl;
174  return STUNKNOWN;
175 }
176 
178 {
179  if (broker == nullptr)
180  {
181  return BrokerType::invalid;
182  }
183  else if (dynamic_cast<YarpBroker*>(broker))
184  {
185  return BrokerType::yarp;
186  }
187  else
188  {
189  return BrokerType::local;
190  }
191 
192 }
193 
195 {
196  if (getBrokerType() == BrokerType::local &&
197  strHost != "localhost")
198  {
199  return true;
200  }
201  else if (getBrokerType() == BrokerType::yarp &&
202  strHost == "localhost")
203  {
204  return true;
205  }
206  return false;
207 
208 }
209 
211 {
212  if (_broker)
213  {
214  broker = _broker;
215  initialize();
216  }
217 }
218 
220  if (watchdogWrapper == nullptr) {
221  return false;
222  }
223  if (!watchdogWrapper->isRunning()) {
224  watchdogWrapper->start();
225  }
226  return true;
227 }
228 
230  if (watchdogWrapper && watchdogWrapper->isRunning()) {
231  watchdogWrapper->stop();
232  }
233 }
234 
235 void Executable::onBrokerStdout(const char* msg)
236 {
237  event->onExecutableStdout(this, msg);
238 }
239 
240 
241 void Executable::watchdogImplement()
242 {
243  if (!broker->running()) {
244  execMachine->moduleFailed();
245  }
246 
247  if(bAutoConnect)
248  {
249  CnnIterator itr;
250  for (itr = connections.begin(); itr != connections.end(); itr++) {
251  if (!broker->connected((*itr).from(), (*itr).to(), (*itr).carrier())) {
252  execMachine->connectionFailed(&(*itr));
253  }
254  }
255  }
256 }
bool ret
static RFModule * module
Definition: RFModule.cpp:231
const char * getName()
Definition: fsm.h:86
StateBase * currentState()
Definition: fsm.h:115
Class Broker.
Definition: broker.h:31
virtual int running()=0
virtual bool init()=0
virtual const char * error()=0
virtual bool initialized()=0
void setEventSink(BrokerEventSink *pEventSink)
Definition: broker.cpp:21
virtual bool connected(const char *from, const char *to, const char *carrier)=0
void addError(const char *szError)
Definition: utility.cpp:118
static ErrorLogger * Instance()
Singleton class ErrorLogger.
Definition: utility.cpp:98
Class ExecMachine.
Definition: execstate.h:197
void connectionFailed(void *which)
Definition: execstate.cpp:709
void onBrokerStdout(const char *msg) override
Definition: executable.cpp:235
Executable(Broker *_broker, MEvent *_event, Module *module, bool bWatchDog=true)
Definition: executable.cpp:11
void setAndInitializeBroker(Broker *_broker)
Definition: executable.cpp:210
Class Module.
Definition: module.h:100
bool isRunning() const
Returns true when the thread is started, false otherwise.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
void wait()
Decrement the counter, even if we must wait to do that.
Definition: Semaphore.cpp:96
void post()
Increment the counter.
Definition: Semaphore.cpp:111
bool isRunning()
Returns true if the thread is running (Thread::start has been called successfully and the thread has ...
Definition: Thread.cpp:105
bool start()
Start the new thread running.
Definition: Thread.cpp:93
bool compareString(const char *szFirst, const char *szSecond)
Definition: utility.cpp:310
enum yarp::manager::__RSTATE RSTATE
std::vector< Connection >::iterator CnnIterator
Definition: application.h:151
std::stringstream OSTRINGSTREAM
Definition: utility.h:49
#define __CHECK_NULLPTR(_ptr)
Definition: ymm-types.h:80