47 #include <boost/bind.hpp> 48 #include <boost/mem_fn.hpp> 57 #if defined(ORO_ACT_DEFAULT_SEQUENTIAL) 59 #elif defined(ORO_ACT_DEFAULT_ACTIVITY) 66 using namespace boost;
68 using namespace detail;
73 #if defined(ORO_ACT_DEFAULT_SEQUENTIAL)
76 ,our_act( new
Activity( this->engine(), name ) )
82 void TaskContext::setup()
84 tcservice->setOwner(
this);
86 provides()->doc(
"The interface of this TaskContext.");
106 this->
addOperation(
"trigger", &
TaskContext::trigger,
this,
ClientThread).doc(
"Trigger the update method for execution in the thread of this task.\n Only succeeds if the task isRunning() and allowed by the Activity executing this task.");
132 tcservice->setOwner(0);
134 tcrequests->setOwner(0);
138 while( !musers.empty() ) {
139 musers.front()->removePeer(
this);
143 while ( !_task_map.empty() ) {
144 _task_map.begin()->second->removeUser(
this);
145 _task_map.erase( _task_map.begin() );
153 bool failure =
false;
154 const std::string& location = this->
getName();
158 for (DataFlowInterface::Ports::iterator it = myports.begin();
165 log(
Debug)<<
"Peer Task "<<peer->
getName() <<
" has no Port " << (*it)->getName() << endlog();
170 if((dynamic_cast<OutputPortInterface*>(*it) && dynamic_cast<OutputPortInterface*>(peerport)) ||
171 (dynamic_cast<InputPortInterface*>(*it) && dynamic_cast<InputPortInterface*>(peerport)))
173 log(
Debug)<< (*it)->getName() <<
" and " << peerport->
getName() <<
" have the same type" << endlog();
178 if ( !(*it)->connectTo( peerport ) ) {
179 log(
Debug)<<
"Data flow incompatible between ports " 180 <<
getName() <<
"." << (*it)->getName() <<
" and " 181 << peer->
getName() <<
"." << (*it)->getName() << endlog();
191 const std::string& location = this->
getName();
194 vector<string> myreqs = this->
requires()->getRequesterNames();
195 vector<string> peerreqs = peer->
requires()->getRequesterNames();
198 for (vector<string>::iterator it = myreqs.begin();
202 if ( !sr->ready() ) {
203 if (peer->
provides()->hasService( *it ))
204 success = sr->connectTo( peer->
provides(*it) ) && success;
206 log(
Debug)<<
"Peer Task "<<peer->
getName() <<
" provides no Service " << *it << endlog();
212 for (vector<string>::iterator it = peerreqs.begin();
213 it != peerreqs.end();
216 if ( !sr->ready() ) {
217 if (this->
provides()->hasService(*it))
218 success = sr->connectTo( this->provides(*it) ) && success;
220 log(
Debug)<<
"This Task provides no Service " << *it <<
" for peer Task "<<peer->
getName() <<
"."<< endlog();
226 bool TaskContext::prepareProvide(
const std::string& name) {
231 if (
provides()->hasService(service_name))
239 musers.push_back(peer);
244 Users::iterator it = find(musers.begin(), musers.end(), peer);
245 if ( it != musers.end() )
253 if ( !peer || _task_map.count( alias ) != 0 )
255 _task_map[ alias ] = peer;
256 peer->addUser(
this );
262 PeerMap::iterator it = _task_map.find( name );
263 if ( _task_map.end() != it ) {
264 it->second->removeUser(
this );
265 _task_map.erase( _task_map.find( name ) );
271 for( PeerMap::iterator it = _task_map.begin(); it != _task_map.end(); ++it)
272 if ( it->second == peer ) {
273 peer->removeUser(
this );
274 _task_map.erase( it );
281 if ( _task_map.count( peer->
getName() ) != 0
282 || peer->
hasPeer( this->getName() ) )
293 for (DataFlowInterface::Ports::iterator it = myports.begin();
300 while( !musers.empty() ) {
301 musers.front()->removePeer(
this);
304 while ( !_task_map.empty() ) {
305 _task_map.begin()->second->removeUser(
this);
306 _task_map.erase( _task_map.begin() );
312 if ( _task_map.end() != _task_map.find( name ) ) {
321 std::vector<std::string> res;
322 std::transform(_task_map.begin(), _task_map.end(),
323 std::back_inserter( res ),
330 return _task_map.count( peer_name ) == 1;
335 if (this->
hasPeer( peer_name ) )
336 return _task_map.find(peer_name)->second;
345 #if defined(ORO_ACT_DEFAULT_SEQUENTIAL) 347 #elif defined(ORO_ACT_DEFAULT_ACTIVITY) 370 our_act.reset( new_act );
371 our_act->run( this->
engine() );
379 return our_act.get();
405 #ifdef ORO_SIGNALLING_PORTS 406 ports()->setupHandles();
416 #ifdef ORO_SIGNALLING_PORTS 417 ports()->cleanupHandles();
436 UserCallbacks::iterator it = user_callbacks.find(port);
437 if (it != user_callbacks.end() )
444 user_callbacks[port] = callback;
447 void TaskContext::removeDataOnPortCallback(
PortInterface* port) {
449 UserCallbacks::iterator it = user_callbacks.find(port);
450 if (it != user_callbacks.end() ) {
451 user_callbacks.erase(it);
TaskContext(const std::string &name, TaskState initial_state=Stopped)
Create a TaskContext.
ActivityInterface * getActivity() const
Query for the task this interface is run in.
Service::shared_ptr provides()
Returns this Service.
virtual bool trigger()
Invoke this method to trigger the thread of this TaskContext to execute its ExecutionEngine and the u...
virtual bool stop()
This method stops the execution of updateHook() of this component.
bool loadService(const std::string &service_name)
Use this method to load a service known to RTT into this component.
void forceActivity(base::ActivityInterface *new_act)
Forces the current activity to become new_act, even if this TaskContext is still running.
boost::shared_ptr< ServiceRequester > shared_ptr
const std::string & getName() const
Get the name of this Port.
virtual bool connectPeers(TaskContext *peer)
Add a two-way connection from this task to a peer task.
virtual void removePeer(const std::string &name)
Remove a one-way connection from this task to a peer task.
virtual bool isConfigured() const
Inspect if the component is configured, i.e.
virtual bool activate()
This method starts the ExecutionEngine of this component in case it was not running.
virtual bool setCpuAffinity(unsigned cpu)
Sets the cpu affinity of this component.
virtual bool isRunning() const
Inspect if the component is in the Running or RunTimeError state.
virtual bool configure()
This method instructs the component to (re-)read configuration data and try to enter the Stopped stat...
bool setActivity(base::ActivityInterface *new_act)
Sets the activity of this TaskContext.
unsigned int mCycleCounter
For each update cycle, this counter increments by one.
unsigned int mIOCounter
Number of cycles that were caused by IOReady triggers.
virtual TaskContext * getPeer(const std::string &peer_name) const
Get a pointer to a peer of this task.
virtual void error()
Call this method in a Running state to indicate a run-time error condition.
virtual Seconds getPeriod() const
Get the configured execution period of this component.
virtual bool run(RunnableInterface *r)
Run exclusively this RunnableInterface.
virtual bool inException() const
Inspect if the component is in the Exception state.
This class allows storage and retrieval of operations, ports, attributes and properties provided by a...
virtual bool hasPeer(const std::string &peer_name) const
Return true if it knows a peer by that name.
ServiceRequester::shared_ptr requires()
Returns the object that manages which methods this Task requires to be implemented by another task...
unsigned int mTimeOutCounter
Number of cycles that were caused by TimeOut triggers.
boost::function< void(base::PortInterface *)> SlotFunction
Name and add a Port to the interface of this task and add a Service with the same name of the port...
virtual void disconnect()
Disconnect this TaskContext from it's peers and ports.
DataFlowInterface * ports()
Get the Data flow ports of this task.
virtual bool inFatalError() const
Inspect if the component is in the FatalError state.
base::PortInterface * getPort(const std::string &name) const
Get an added port.
virtual PeerList getPeerList() const
Return a standard container which contains all the Peer names of this TaskContext.
bool mTriggerOnStart
Set to false in order to not trigger() when calling start().
boost::shared_ptr< ActivityInterface > shared_ptr
virtual bool update()
Invoke this method to execute the ExecutionEngine and the update() method.
Interface to start/stop and query a Activity.
virtual void disconnectPeers(const std::string &name)
Remove a two-way connection from this task to a peer task.
TaskState
Describes the different states a component can have.
bool addAttribute(const std::string &name, T &attr)
Adds a variable of any type as read/write attribute to the attribute interface.
std::vector< base::PortInterface * > Ports
A sequence of pointers to ports.
An Activity executes a RunnableInterface object in a (periodic) thread.
virtual bool isActive() const
Inspect if the component's ExecutionEngine is processing requests.
virtual bool inRunTimeError() const
Inspect if the component is in the RunTimeError state.
Notify the Logger in which 'module' the message occured.
The TaskContext is the C++ representation of an Orocos component.
virtual unsigned getCpuAffinity() const
Get the configured cpu affinity of this component.
An object that expresses you wish to use a service.
virtual bool connectPorts(TaskContext *peer)
Add a data flow connection from this task's ports to a peer's ports.
static boost::shared_ptr< PluginLoader > Instance()
Create the instance of the PluginLoader.
Ports getPorts() const
Get all ports of this interface.
virtual bool connectServices(TaskContext *peer)
Connects all requires/provides services of this component to these of a peer.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
virtual bool stop()
This method stops the execution of updateHook() of this component.
virtual void dataOnPortCallback(base::PortInterface *port)
This method implements port callbacks.
The base class of every data flow port.
virtual bool process(base::DisposableInterface *c)
Queue and execute (process) a given message.
Operation< Signature > & addOperation(Operation< Signature > &op)
Add an operation object to the interface.
virtual bool setPeriod(Seconds s)
Sets the period of this component.
unsigned int mTriggerCounter
Number of cycles that were caused by Trigger triggers.
const ExecutionEngine * engine() const
Get a const pointer to the ExecutionEngine of this Task.
virtual bool start()
This method starts the execution of the updateHook() with each trigger or period. ...
virtual bool addPeer(TaskContext *peer, std::string alias="")
Add a one-way connection from this task to a peer task.
base::ActivityInterface * getActivity()
Get a pointer to the activity running this component.
virtual bool cleanup()
This method instructs a stopped component to enter the pre-operational state again.
virtual const std::string & getName() const
Returns the name of this TaskContext.
virtual void clear()
Clear the complete interface of this Component.
MutexLock is a scope based Monitor, protecting critical sections with a Mutex object through locking ...
#define ORO_ACT_DEFAULT_ACTIVITY
virtual bool start()
This method starts the execution of the updateHook() with each trigger or period. ...
virtual bool stop()=0
Stop the activity This will stop the activity by removing it from the 'run-queue' of a thread or call...
virtual bool ready()
Checks the validity of this TaskContext.
virtual bool dataOnPortHook(base::PortInterface *port)
Reimplement this method to influence how writing to event ports is handled by the component...