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 }
executable.h
yarp::manager::DYING
@ DYING
Definition: executable.h:38
FSM::StateMachineBase::currentState
StateBase * currentState()
Definition: fsm.h:118
yarp::manager::BrokerType::local
@ local
yarp::manager::Broker::error
virtual const char * error()=0
yarp::manager::Executable::Executable
Executable(Broker *_broker, MEvent *_event, Module *module, bool bWatchDog=true)
Definition: executable.cpp:14
yarp::manager::ExecMachine::startModule
void startModule()
Definition: execstate.cpp:648
yarp::manager::Broker::running
virtual int running()=0
yarp::manager::Executable::shouldChangeBroker
bool shouldChangeBroker()
Definition: executable.cpp:185
yarp::manager::ConcurentRateWrapper
Definition: executable.h:199
yarp::manager::ExecMachine::disconnectAllPorts
void disconnectAllPorts()
Definition: execstate.cpp:680
yarp::manager::OSTRINGSTREAM
std::stringstream OSTRINGSTREAM
Definition: utility.h:52
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
Definition: application.h:24
yarp::manager::BrokerType::invalid
@ invalid
yarp::manager::Executable::~Executable
~Executable() override
Definition: executable.cpp:40
yarp::manager::Broker
Class Broker.
Definition: broker.h:34
yarp::manager::RSTATE
enum yarp::manager::__RSTATE RSTATE
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::manager::CnnIterator
std::vector< Connection >::iterator CnnIterator
Definition: application.h:154
yarp::os::Semaphore::wait
void wait()
Decrement the counter, even if we must wait to do that.
Definition: Semaphore.cpp:99
yarp::manager::Executable::kill
void kill()
Definition: executable.cpp:122
yarp::manager::compareString
bool compareString(const char *szFirst, const char *szSecond)
Definition: utility.cpp:305
yarp::os::PeriodicThread::isRunning
bool isRunning() const
Returns true when the thread is started, false otherwise.
Definition: PeriodicThread.cpp:316
yarp::manager::Executable::stop
void stop()
Definition: executable.cpp:102
yarp::manager::ExecMachine::moduleFailed
void moduleFailed()
Definition: execstate.cpp:688
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::os::Semaphore::post
void post()
Increment the counter.
Definition: Semaphore.cpp:114
yarp::manager::ExecMachine::kill
void kill()
Definition: execstate.cpp:640
yarp::manager::ErrorLogger::Instance
static ErrorLogger * Instance()
Singleton class ErrorLogger.
Definition: utility.cpp:102
yarp::manager::ExecMachine::stop
void stop()
Definition: execstate.cpp:632
yarp::manager::Executable::stopWatchDog
void stopWatchDog()
Definition: executable.cpp:218
yarp::manager::STUNKNOWN
@ STUNKNOWN
Definition: executable.h:40
yarp::manager::Broker::init
virtual bool init()=0
yarp::manager::MEvent
Definition: executable.h:51
yarp::os::PeriodicThread::start
bool start()
Call this to start the thread.
Definition: PeriodicThread.cpp:311
yarp::manager::ErrorLogger::addError
void addError(const char *szError)
Definition: utility.cpp:121
yarp::manager::BrokerType
BrokerType
Definition: executable.h:44
yarp::manager::Broker::initialized
virtual bool initialized()=0
yarp::manager::RUNNING
@ RUNNING
Definition: executable.h:37
yarp::manager::YarpBroker
Class Broker.
Definition: yarpbroker.h:37
module
static RFModule * module
Definition: RFModule.cpp:234
yarp::manager::ExecMachine::connectAllPorts
void connectAllPorts()
Definition: execstate.cpp:672
__CHECK_NULLPTR
#define __CHECK_NULLPTR(_ptr)
Definition: ymm-types.h:83
yarpbroker.h
yarp::manager::ConcurentWrapper
Definition: executable.h:175
yarp::manager::Broker::connected
virtual bool connected(const char *from, const char *to, const char *carrier)=0
yarp::manager::ExecMachine::killModule
void killModule()
Definition: execstate.cpp:664
yarp::manager::Executable::getBrokerType
BrokerType getBrokerType()
Definition: executable.cpp:168
yarp::manager::CONNECTING
@ CONNECTING
Definition: executable.h:36
yarp::manager::ExecMachine::refresh
void refresh()
Definition: execstate.cpp:616
yarp::manager::Executable::start
bool start()
Definition: executable.cpp:79
FSM::StateBase::getName
const char * getName()
Definition: fsm.h:89
yarp::manager::BrokerType::yarp
@ yarp
yarp::manager::ExecMachine::start
void start()
Definition: execstate.cpp:624
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::os::Thread::start
bool start()
Start the new thread running.
Definition: Thread.cpp:96
yarp::manager::Executable::setAndInitializeBroker
void setAndInitializeBroker(Broker *_broker)
Definition: executable.cpp:201
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::ExecMachine::stopModule
void stopModule()
Definition: execstate.cpp:656
yarp::manager::DEAD
@ DEAD
Definition: executable.h:39
yarp::manager::ExecMachine::connectionFailed
void connectionFailed(void *which)
Definition: execstate.cpp:696
yarp::manager::Executable::removeBroker
void removeBroker()
Definition: executable.h:102
yarp::manager::Broker::setEventSink
void setEventSink(BrokerEventSink *pEventSink)
Definition: broker.cpp:24