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;
74 #if defined(ORO_ACT_DEFAULT_SEQUENTIAL)
77 ,our_act( new
Activity( this->engine(), name ) )
87 #if defined(ORO_ACT_DEFAULT_SEQUENTIAL)
96 void TaskContext::setup()
98 tcservice->setOwner(
this);
100 provides()->doc(
"The interface of this TaskContext.");
120 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.");
141 while( !musers.empty() ) {
142 musers.front()->removePeer(
this);
146 while ( !_task_map.empty() ) {
147 _task_map.begin()->second->removeUser(
this);
148 _task_map.erase( _task_map.begin() );
157 bool failure =
false;
158 const std::string& location = this->
getName();
162 for (DataFlowInterface::Ports::iterator it = myports.begin();
169 log(
Debug)<<
"Peer Task "<<peer->
getName() <<
" has no Port " << (*it)->getName() << endlog();
174 if((dynamic_cast<OutputPortInterface*>(*it) && dynamic_cast<OutputPortInterface*>(peerport)) ||
175 (dynamic_cast<InputPortInterface*>(*it) && dynamic_cast<InputPortInterface*>(peerport)))
177 log(
Debug)<< (*it)->getName() <<
" and " << peerport->
getName() <<
" have the same type" << endlog();
182 if ( !(*it)->connectTo( peerport ) ) {
183 log(
Debug)<<
"Data flow incompatible between ports " 184 <<
getName() <<
"." << (*it)->getName() <<
" and " 185 << peer->
getName() <<
"." << (*it)->getName() << endlog();
195 const std::string& location = this->
getName();
198 vector<string> myreqs = this->
requires()->getRequesterNames();
199 vector<string> peerreqs = peer->
requires()->getRequesterNames();
202 for (vector<string>::iterator it = myreqs.begin();
206 if ( !sr->ready() ) {
207 if (peer->
provides()->hasService( *it ))
208 success = sr->connectTo( peer->
provides(*it) ) && success;
210 log(
Debug)<<
"Peer Task "<<peer->
getName() <<
" provides no Service " << *it << endlog();
216 for (vector<string>::iterator it = peerreqs.begin();
217 it != peerreqs.end();
220 if ( !sr->ready() ) {
221 if (this->
provides()->hasService(*it))
222 success = sr->connectTo( this->provides(*it) ) && success;
224 log(
Debug)<<
"This Task provides no Service " << *it <<
" for peer Task "<<peer->
getName() <<
"."<< endlog();
230 bool TaskContext::prepareProvide(
const std::string& name) {
235 if (
provides()->hasService(service_name))
243 musers.push_back(peer);
248 Users::iterator it = find(musers.begin(), musers.end(), peer);
249 if ( it != musers.end() )
257 if ( !peer || _task_map.count( alias ) != 0 )
259 _task_map[ alias ] = peer;
260 peer->addUser(
this );
266 PeerMap::iterator it = _task_map.find( name );
267 if ( _task_map.end() != it ) {
268 it->second->removeUser(
this );
269 _task_map.erase( _task_map.find( name ) );
275 for( PeerMap::iterator it = _task_map.begin(); it != _task_map.end(); ++it)
276 if ( it->second == peer ) {
277 peer->removeUser(
this );
278 _task_map.erase( it );
285 if ( _task_map.count( peer->
getName() ) != 0
286 || peer->
hasPeer( this->getName() ) )
297 for (DataFlowInterface::Ports::iterator it = myports.begin();
304 while( !musers.empty() ) {
305 musers.front()->removePeer(
this);
308 while ( !_task_map.empty() ) {
309 _task_map.begin()->second->removeUser(
this);
310 _task_map.erase( _task_map.begin() );
316 if ( _task_map.end() != _task_map.find( name ) ) {
325 std::vector<std::string> res;
326 std::transform(_task_map.begin(), _task_map.end(),
327 std::back_inserter( res ),
334 return _task_map.count( peer_name ) == 1;
339 if (this->
hasPeer( peer_name ) )
340 return _task_map.find(peer_name)->second;
349 #if defined(ORO_ACT_DEFAULT_SEQUENTIAL) 351 #elif defined(ORO_ACT_DEFAULT_ACTIVITY) 373 our_act.reset( new_act );
374 our_act->run( this->
engine() );
382 return our_act.get();
408 #ifdef ORO_SIGNALLING_PORTS 409 ports()->setupHandles();
419 #ifdef ORO_SIGNALLING_PORTS 420 ports()->cleanupHandles();
430 portqueue->enqueue( port );
442 user_callbacks[port] = callback;
447 UserCallbacks::iterator it = user_callbacks.find(port);
448 if (it != user_callbacks.end() ) {
449 user_callbacks.erase(it);
453 void TaskContext::prepareUpdateHook()
457 while ( portqueue->dequeue( port ) == true ) {
458 UserCallbacks::iterator it = user_callbacks.find(port);
459 if (it != user_callbacks.end() )
TaskContext(const std::string &name, TaskState initial_state=Stopped)
Create a TaskContext.
ActivityInterface * getActivity() const
Query for the task this interface is run in.
virtual bool trigger()=0
Trigger that work has to be done.
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.
An execution engine serialises (executes one after the other) the execution of all commands...
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...
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.
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.
std::vector< base::PortInterface * > Ports
A sequence of pointers to ports.
An Activity is an object that represents a thread.
virtual bool isActive() const
Inspect if the component's ExecutionEngine is processing requests.
This object represents the default Multi-Writer, Single-Reader queue implementation used by Orocos ob...
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.
The base class of every data flow port.
Operation< Signature > & addOperation(Operation< Signature > &op)
Add an operation object to the interface.
virtual bool setPeriod(Seconds s)
Sets the period of this component.
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...