00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef ORO_TASK_CONTEXT_HPP
00040 #define ORO_TASK_CONTEXT_HPP
00041
00042
00043 #include "rtt-config.h"
00044 #include "Service.hpp"
00045 #include "ServiceRequester.hpp"
00046 #include "DataFlowInterface.hpp"
00047 #include "ExecutionEngine.hpp"
00048 #include "base/TaskCore.hpp"
00049 #include <boost/make_shared.hpp>
00050
00051 #include <string>
00052 #include <map>
00053
00054 namespace RTT
00055 {
00093 class RTT_API TaskContext
00094 : public base::TaskCore
00095 {
00096 public:
00100 typedef std::vector< std::string > PeerList;
00101
00110 TaskContext( const std::string& name, TaskState initial_state = Stopped );
00111
00121 TaskContext(const std::string& name, ExecutionEngine* parent, TaskState initial_state = Stopped );
00122
00123 virtual ~TaskContext();
00124
00128 virtual const std::string& getName() { return tcservice->getName(); }
00129
00144 bool setActivity( base::ActivityInterface* new_act );
00145
00151 base::ActivityInterface* getActivity();
00152
00172 template<typename T>
00173 T* getActivity() { return dynamic_cast<T*>(getActivity()); }
00174
00180 virtual void clear();
00181
00190 virtual bool ready();
00191
00192 virtual bool start();
00193 virtual bool stop();
00194
00209 virtual bool addPeer( TaskContext* peer, std::string alias = "" );
00210
00214 virtual void removePeer( const std::string& name );
00215
00219 virtual void removePeer( TaskContext* peer );
00220
00224 virtual bool connectPeers( TaskContext* peer );
00225
00231 virtual void disconnect();
00232
00236 virtual void disconnectPeers( const std::string& name );
00237
00242 virtual PeerList getPeerList() const;
00243
00247 virtual bool hasPeer( const std::string& peer_name ) const;
00248
00253 virtual TaskContext* getPeer(const std::string& peer_name ) const;
00268 Service::shared_ptr provides() { return tcservice; }
00269
00275 Service::shared_ptr provides(const std::string& service_name) { return tcservice->provides(service_name); }
00276
00281 ServiceRequester* requires() { return tcrequests; }
00282
00287 ServiceRequester* requires(const std::string& service_name) {
00288 return tcrequests->requires(service_name);
00289 }
00290
00294 virtual bool connectServices( TaskContext* peer);
00295
00308 template<class ServiceType>
00309 boost::shared_ptr<ServiceType> getProvider(const std::string& name) {
00310 if (!prepareProvide(name)) return boost::shared_ptr<ServiceType>();
00311 LocalServices::iterator it = localservs.find(name);
00312 if ( it != localservs.end() ) {
00313 return boost::dynamic_pointer_cast<ServiceType>(it->second);
00314 }
00315 boost::shared_ptr<ServiceType> st = boost::make_shared<ServiceType>(this);
00316 st->connectTo( provides(name) );
00317 localservs[name] = st;
00318 return st;
00319 }
00320
00339 template<class Signature>
00340 Operation<Signature>& addOperation( Operation<Signature>& op )
00341 {
00342 return tcservice->addOperation(op);
00343 }
00344
00349 template<class Func, class Service>
00350 Operation< typename internal::GetSignature<Func>::Signature >&
00351 addOperation( const std::string name, Func func, Service* serv, ExecutionThread et = ClientThread )
00352 {
00353 return tcservice->addOperation(name,func, serv, et);
00354 }
00355
00360 template<class Signature>
00361 Operation< Signature >&
00362 addOperation( const std::string name, Signature* func, ExecutionThread et = ClientThread )
00363 {
00364 return tcservice->addOperation(name, func, et);
00365 }
00366
00376 OperationInterfacePart* getOperation( std::string name )
00377 {
00378 return tcservice->getOperation(name);
00379 }
00380
00384 OperationInterface* operations() { return this->provides().get(); }
00385
00403 template<class T>
00404 bool addAttribute( const std::string& name, T& attr) {
00405 return tcservice->addAttribute(name, attr);
00406 }
00407
00416 template<class T>
00417 bool addConstant( const std::string& name, const T& attr) {
00418 return tcservice->addConstant(name, attr);
00419 }
00420
00430 bool addAttribute( base::AttributeBase& a )
00431 {
00432 return tcservice->addAttribute(a);
00433 }
00434
00443 base::AttributeBase* getAttribute( const std::string& name ) const
00444 {
00445 return tcservice->getAttribute(name);
00446 }
00447
00451 ConfigurationInterface* attributes() { return this->provides().get(); }
00452
00471 template<class T>
00472 Property<T>& addProperty( const std::string& name, T& attr) {
00473 return tcservice->addProperty(name, attr);
00474 }
00475
00480 bool addProperty( base::PropertyBase& pb ) {
00481 return tcservice->addProperty(pb);
00482 }
00483
00491 base::PropertyBase* getProperty(const std::string& name) const
00492 {
00493 return tcservice->getProperty(name);
00494 }
00495
00499 PropertyBag* properties() { return this->provides()->properties(); }
00500
00516 base::PortInterface& addPort(const std::string& name, base::PortInterface& port) {
00517 port.setName(name);
00518 return ports()->addPort(port);
00519 }
00520
00526 base::PortInterface& addPort(base::PortInterface& port) {
00527 return ports()->addPort(port);
00528 }
00529
00539 base::InputPortInterface& addEventPort(const std::string& name, base::InputPortInterface& port, base::InputPortInterface::NewDataOnPortEvent::SlotFunction callback = base::InputPortInterface::NewDataOnPortEvent::SlotFunction() ) {
00540 port.setName(name);
00541 return ports()->addEventPort(port,callback);
00542 }
00543
00552 base::InputPortInterface& addEventPort(base::InputPortInterface& port, base::InputPortInterface::NewDataOnPortEvent::SlotFunction callback = base::InputPortInterface::NewDataOnPortEvent::SlotFunction() ) {
00553 return ports()->addEventPort(port,callback);
00554 }
00555
00561 base::PortInterface* getPort(const std::string& name) const {
00562 return ports()->getPort(name);
00563 }
00564
00565
00569 DataFlowInterface* ports() {
00570 return tcservice.get();
00571 }
00572
00576 const DataFlowInterface* ports() const {
00577 return tcservice.get();
00578 }
00579
00583 virtual bool connectPorts( TaskContext* peer );
00586 protected:
00594 void forceActivity( base::ActivityInterface* new_act);
00595 private:
00596
00597 typedef std::map< std::string, TaskContext* > PeerMap;
00598 typedef std::vector< TaskContext* > Users;
00600 PeerMap _task_map;
00602 Users musers;
00603
00608 void addUser(TaskContext* user);
00609
00614 void removeUser(TaskContext* user);
00615
00619 void setup();
00620
00621 friend class DataFlowInterface;
00622 internal::MWSRQueue<base::PortInterface*>* portqueue;
00623 typedef std::map<base::PortInterface*, boost::shared_ptr<base::InputPortInterface::NewDataOnPortEvent> > UserCallbacks;
00624 UserCallbacks user_callbacks;
00625
00630 void dataOnPort(base::PortInterface* port);
00636 bool dataOnPortSize(unsigned int max);
00640 void dataOnPortCallback(base::InputPortInterface* port, base::InputPortInterface::SlotFunction callback);
00644 void dataOnPortRemoved(base::PortInterface* port);
00645
00650 void prepareUpdateHook();
00651
00659 bool prepareProvide(const std::string& name);
00660
00661 typedef std::map<std::string, boost::shared_ptr<ServiceRequester> > LocalServices;
00662 LocalServices localservs;
00663
00664 Service::shared_ptr tcservice;
00665 ServiceRequester* tcrequests;
00666 os::Mutex mportlock;
00667
00668
00669 TaskContext( TaskContext& );
00670
00675 base::ActivityInterface::shared_ptr our_act;
00676 };
00677
00683 RTT_API bool connectPorts(TaskContext* A, TaskContext* B);
00684
00691 RTT_API bool connectPeers(TaskContext* A, TaskContext* B);
00692 }
00693
00694 #endif