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