Orocos Real-Time Toolkit  2.8.3
TaskContextProxy.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Wed Jan 18 14:09:49 CET 2006 TaskContextProxy.cxx
3 
4  TaskContextProxy.cxx - description
5  -------------------
6  begin : Wed January 18 2006
7  copyright : (C) 2006 Peter Soetens
8  email : peter.soetens@fmtc.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 #include "TaskContextProxy.hpp"
39 #include "TaskContextServer.hpp"
40 #include "TaskContextC.h"
42 #include "CorbaLib.hpp"
43 #include "OperationCallerProxy.hpp"
44 
45 #include "../../types/Types.hpp"
46 #include "../../extras/SequentialActivity.hpp"
47 #include "corba.h"
48 #ifdef CORBA_IS_TAO
49 #include "tao/TimeBaseC.h"
50 #include "tao/Messaging/Messaging.h"
51 #include "tao/Messaging/Messaging_RT_PolicyC.h"
52 #include "orbsvcs/CosNamingC.h"
53 #include <ace/String_Base.h>
54 #else
55 #include <omniORB4/Naming.hh>
56 #endif
57 #include <iostream>
58 #include <fstream>
59 #include <string>
60 
61 #include "RemotePorts.hpp"
62 
63 using namespace std;
64 using namespace RTT::detail;
65 
66 namespace RTT
67 {namespace corba
68 {
69  IllegalServer::IllegalServer() : reason("This server does not exist or has the wrong type.") {}
70 
71  IllegalServer::IllegalServer(const std::string& r) : reason(r) {}
72 
74 
75  const char* IllegalServer::what() const throw() { return reason.c_str(); }
76 
77 
78  std::map<TaskContextProxy*, corba::CTaskContext_ptr> TaskContextProxy::proxies;
79 
80  PortableServer::POA_var TaskContextProxy::proxy_poa;
81 
83  {
84  log(Info) << "Terminating TaskContextProxy for " << this->getName() <<endlog();
85  if ( this->properties() ) {
86  deletePropertyBag( *this->properties() );
87  }
88  this->attributes()->clear();
89  for (list<PortInterface*>::iterator it = port_proxies.begin(); it != port_proxies.end(); ++it)
90  delete *it;
91  proxies.erase(this);
92  }
93 
94  TaskContextProxy::TaskContextProxy(std::string name, bool is_ior)
95  : TaskContext("NotFound")
96  {
97  initFromURIOrTaskname(name, is_ior);
98  }
99 
101  {
102 
103  }
104 
105  void TaskContextProxy::initFromURIOrTaskname(string name, bool is_ior)
106  {
107  Logger::In in("TaskContextProxy");
108  this->clear();
109  this->setActivity( new SequentialActivity() );
110  try {
111  if (is_ior) {
112  // Use the first argument to create the task object reference,
113  // in real applications we use the naming service, but let's do
114  // the easy part first!
115  CORBA::Object_var task_object =
116  orb->string_to_object ( name.c_str() );
117 
118  // Now downcast the object reference to the appropriate type
119  mtask = corba::CTaskContext::_narrow (task_object.in ());
120  } else {
121  // NameService
122  CORBA::Object_var rootObj;
123  CosNaming::NamingContext_var rootContext;
124  try {
125  rootObj = orb->resolve_initial_references("NameService");
126  rootContext = CosNaming::NamingContext::_narrow(rootObj);
127  } catch (...) {}
128 
129  if (CORBA::is_nil(rootContext)) {
130  std::string err("TaskContextProxy could not acquire NameService.");
131  log(Error) << err <<endlog();
132  throw IllegalServer(err);
133  }
134  Logger::log() <<Logger::Debug << "TaskContextProxy found CORBA NameService."<<endlog();
135  CosNaming::Name serverName;
136  serverName.length(2);
137  serverName[0].id = CORBA::string_dup("TaskContexts");
138  serverName[1].id = CORBA::string_dup( name.c_str() );
139 
140  // Get object reference
141  CORBA::Object_var task_object = rootContext->resolve(serverName);
142  mtask = corba::CTaskContext::_narrow (task_object.in ());
143  }
144  if ( CORBA::is_nil( mtask ) ) {
145  std::string err("Failed to acquire TaskContextServer '"+name+"'.");
146  Logger::log() << Logger::Error << err <<endlog();
147  throw IllegalServer(err);
148  }
149  CORBA::String_var nm = mtask->getName(); // force connect to object.
150  std::string newname( nm.in() );
151  this->provides()->setName( newname );
152  Logger::log() << Logger::Info << "Successfully connected to TaskContextServer '"+name+"'."<<endlog();
153  proxies[this] = mtask.in();
154  }
155  catch (CORBA::Exception &e) {
156  log(Error)<< "CORBA exception raised when resolving Object !" << endlog();
157  Logger::log() << CORBA_EXCEPTION_INFO(e) << endlog();
158  throw;
159  }
160  catch (IllegalServer& e) {
161  // rethrow
162  throw e;
163  }
164  catch (...) {
165  log(Error) <<"Unknown Exception in TaskContextProxy construction!"<<endlog();
166  throw;
167  }
168 
169  this->synchronize();
170  }
171 
172  TaskContextProxy::TaskContextProxy( ::RTT::corba::CTaskContext_ptr taskc)
173  : TaskContext("CORBAProxy"), mtask( corba::CTaskContext::_duplicate(taskc) )
174  {
175  Logger::In in("TaskContextProxy");
176  this->clear();
177  // We can't use setActivity() since that would check isRunning() first.
178  this->forceActivity( new SequentialActivity );
179  try {
180  CORBA::String_var nm = mtask->getName(); // force connect to object.
181  std::string name( nm.in() );
182  this->provides()->setName( name );
183  proxies[this] = mtask.in();
184  }
185  catch (CORBA::Exception &e) {
186  log(Error) << "CORBA exception raised when creating TaskContextProxy!" << Logger::nl;
187  Logger::log() << CORBA_EXCEPTION_INFO(e) << endlog();
188  }
189  catch (...) {
190  throw;
191  }
192  this->synchronize();
193  }
194 
196  {
197  // Add here the interfaces that need to be synchronised every time a lookup is done.
198  // Detect already added parts of an interface, does not yet detect removed parts...
199  if (CORBA::is_nil(mtask))
200  return;
201 
202  CService_var serv = mtask->getProvider("this");
203  this->fetchServices(this->provides(), serv.in() );
204 
205  CServiceRequester_var srq = mtask->getRequester("this");
206  this->fetchRequesters(this->requires(), srq.in() );
207  log(Debug) << "All Done."<<endlog();
208  }
209 
210  void TaskContextProxy::fetchRequesters(ServiceRequester::shared_ptr parent, CServiceRequester_ptr csrq)
211  {
212  COperationCallerNames_var opcnames = csrq->getOperationCallerNames();
213 
214  log(Debug) << "Fetching OperationCallers of " << parent->getRequestName()<<endlog();
215  for ( size_t i=0; i < opcnames->length(); ++i) {
216  if ( parent->getOperationCaller( string(opcnames[i].in() )))
217  continue; // already added.
218  log(Debug) << "Requiring operation: "<< opcnames[i].in() <<endlog();
219  parent->addOperationCaller( * new OperationCallerProxy(string(opcnames[i].in() ), CServiceRequester::_duplicate(csrq) ));
220  }
221 
222  CRequestNames_var rqlist = csrq->getRequestNames();
223 
224  for( size_t i =0; i != rqlist->length(); ++i) {
225  if ( string( rqlist[i] ) == "this")
226  continue;
227  CServiceRequester_var cobj = csrq->getRequest(rqlist[i]);
228 
229  ServiceRequester::shared_ptr tobj = this->requires(std::string(rqlist[i]));
230 
231  // Recurse:
232  this->fetchRequesters( tobj, cobj.in() );
233  }
234  }
235 
236  // Recursively fetch remote objects and create local proxies.
237  void TaskContextProxy::fetchServices(Service::shared_ptr parent, CService_ptr serv)
238  {
239  log(Debug) << "Fetching "<<parent->getName()<<" Service:"<<endlog();
240 
241  // Fetch ports
242  this->fetchPorts(parent, serv);
243 
244  // load command and method factories.
245  // methods:
246  log(Debug) << "Fetching Operations."<<endlog();
247  COperationInterface::COperationList_var objs;
248  objs = serv->getOperations();
249  for ( size_t i=0; i < objs->length(); ++i) {
250  if ( parent->hasMember( string(objs[i].in() )))
251  continue; // already added.
252  log(Debug) << "Providing operation: "<< objs[i].in() <<endlog();
253  parent->add( objs[i].in(), new CorbaOperationCallerFactory( objs[i].in(), serv, ProxyPOA() ) );
254  }
255 
256  // first do properties:
257  log(Debug) << "Fetching Properties."<<endlog();
258  // a dot-separated list of subbags and items
259  CConfigurationInterface::CPropertyNames_var props = serv->getPropertyList();
260 
261  for (size_t i=0; i != props->length(); ++i) {
262  if ( findProperty( *parent->properties(), string(props[i].name.in()), "." ) )
263  continue; // previously added.
264  if ( !serv->hasProperty( props[i].name.in() ) ) {
265  log(Error) <<"Property "<< string(props[i].name.in()) << " present in getPropertyList() but not accessible."<<endlog();
266  continue;
267  }
268  // If the type is known, immediately build the correct property and datasource.
269  CORBA::String_var tn = serv->getPropertyTypeName(props[i].name.in());
270  TypeInfo* ti = TypeInfoRepository::Instance()->type( tn.in() );
271 
272  // decode the prefix and property name from the given name:
273  string pname = string( props[i].name.in() );
274  pname = pname.substr( pname.rfind(".") + 1 );
275  string prefix = string( props[i].name.in() );
276  if ( prefix.rfind(".") == string::npos ) {
277  prefix.clear();
278  }
279  else {
280  prefix = prefix.substr( 0, prefix.rfind(".") );
281  }
282 
283  if ( ti && ti->hasProtocol(ORO_CORBA_PROTOCOL_ID)) {
285  assert(ctt);
286  // data source needs full remote path name
287  DataSourceBase::shared_ptr ds = ctt->createPropertyDataSource( serv, props[i].name.in() );
288  storeProperty( *parent->properties(), prefix, ti->buildProperty( pname, props[i].description.in(), ds));
289  log(Debug) << "Looked up Property " << tn.in() << " "<< pname <<": created."<<endlog();
290  }
291  else {
292  if ( string("PropertyBag") == tn.in() ) {
293  storeProperty(*parent->properties(), prefix, new Property<PropertyBag>( pname, props[i].description.in()) );
294  log(Debug) << "Looked up PropertyBag " << tn.in() << " "<< pname <<": created."<<endlog();
295  } else
296  log(Error) << "Looked up Property " << tn.in() << " "<< pname <<": type not known. Check your RTT_COMPONENT_PATH ( \""<<getenv("RTT_COMPONENT_PATH")<<" \")."<<endlog();
297  }
298  }
299 
300  log(Debug) << "Fetching Attributes."<<endlog();
301  CConfigurationInterface::CAttributeNames_var attrs = serv->getAttributeList();
302  for (size_t i=0; i != attrs->length(); ++i) {
303  if ( parent->hasAttribute( string(attrs[i].in()) ) )
304  continue; // previously added.
305  if ( !serv->hasAttribute( attrs[i].in() ) ) {
306  log(Error) <<"Attribute '"<< string(attrs[i].in()) << "' present in getAttributeList() but not accessible."<<endlog();
307  continue;
308  }
309  // If the type is known, immediately build the correct attribute and datasource,
310  CORBA::String_var tn = serv->getAttributeTypeName( attrs[i].in() );
311  TypeInfo* ti = TypeInfoRepository::Instance()->type( tn.in() );
312  if ( ti && ti->hasProtocol(ORO_CORBA_PROTOCOL_ID) ) {
313  log(Debug) << "Looking up Attribute " << tn.in() <<": found!"<<endlog();
315  assert(ctt);
316  // this function should check itself for const-ness of the remote Attribute:
317  DataSourceBase::shared_ptr ds = ctt->createAttributeDataSource( serv, attrs[i].in() );
318  if ( serv->isAttributeAssignable( attrs[i].in() ) )
319  parent->setValue( ti->buildAttribute( attrs[i].in(), ds));
320  else
321  parent->setValue( ti->buildConstant( attrs[i].in(), ds));
322  } else {
323  log(Error) << "Looking up Attribute " << tn.in();
324  Logger::log() <<": type not known. Check your RTT_COMPONENT_PATH ( \""<<getenv("RTT_COMPONENT_PATH")<<" \")."<<endlog();
325  }
326  }
327 
328  CService::CProviderNames_var plist = serv->getProviderNames();
329 
330  for( size_t i =0; i != plist->length(); ++i) {
331  if ( string( plist[i] ) == "this")
332  continue;
333  CService_var cobj = serv->getService(plist[i]);
334  CORBA::String_var descr = cobj->getServiceDescription();
335 
336  Service::shared_ptr tobj = parent->provides(std::string(plist[i]));
337  tobj->doc( descr.in() );
338 
339  // Recurse:
340  this->fetchServices( tobj, cobj.in() );
341  }
342  }
343 
344  // Fetch remote ports and create local proxies
345  void TaskContextProxy::fetchPorts(RTT::Service::shared_ptr parent, CDataFlowInterface_ptr dfact)
346  {
347  log(Debug) << "Fetching Ports for service "<<parent->getName()<<"."<<endlog();
349  if (dfact) {
350  CDataFlowInterface::CPortDescriptions_var objs = dfact->getPortDescriptions();
351  for ( size_t i=0; i < objs->length(); ++i) {
352  CPortDescription port = objs[i];
353  if (parent->getPort( port.name.in() ))
354  continue; // already added.
355 
356  TypeInfo const* type_info = type_repo->type(port.type_name.in());
357  if (!type_info)
358  {
359  log(Warning) << "remote port " << port.name
360  << " has a type that cannot be marshalled over CORBA: " << port.type_name << ". "
361  << "It is ignored by TaskContextProxy" << endlog();
362  }
363  else
364  {
365  PortInterface* new_port;
366  if (port.type == RTT::corba::CInput)
367  new_port = new RemoteInputPort( type_info, dfact, port.name.in(), ProxyPOA() );
368  else
369  new_port = new RemoteOutputPort( type_info, dfact, port.name.in(), ProxyPOA() );
370 
371  parent->addPort(*new_port);
372  port_proxies.push_back(new_port); // see comment in definition of port_proxies
373  }
374  }
375  }
376  }
377 
379  {
380  try {
381  // Destroy the POA, waiting until the destruction terminates
382  //poa->destroy (1, 1);
383  if (orb) {
384  orb->destroy();
385  rootPOA = 0;
386  orb = 0;
387  std::cerr <<"Orb destroyed."<<std::endl;
388  }
389  }
390  catch (CORBA::Exception &e) {
391  log(Error) << "Orb Init : CORBA exception raised!" << Logger::nl;
392  Logger::log() << CORBA_EXCEPTION_INFO(e) << endlog();
393  }
394  }
395 
396  TaskContextProxy* TaskContextProxy::Create(std::string name, bool is_ior /*=false*/) {
397  if ( CORBA::is_nil(orb) ) {
398  log(Error) << "Won't create a proxy for '"<<name<<"' : orb is nill. Call TaskContextProxy::InitOrb(argc, argv); before TaskContextProxy::Create()." <<endlog();
399  return 0;
400  }
401  if ( name.empty() ) {
402  log(Error) << "Can't create a proxy with an empty name." <<endlog();
403  return 0;
404  }
405  // create new:
406  try {
407  TaskContextProxy* ctp = new TaskContextProxy( name, is_ior );
408  return ctp;
409  }
410  catch( IllegalServer& is ) {
411  cerr << is.what() << endl;
412  }
413  return 0;
414  }
415 
417  if ( CORBA::is_nil(orb) ) {
418  log(Error) << "Won't create a proxy for '"<<name<<"' : orb is nill. Call TaskContextProxy::InitOrb(argc, argv); before TaskContextProxy::Create()." <<endlog();
419  return 0;
420  }
421  if ( name.empty() ) {
422  log(Error) << "Can't create a proxy with an empty file name." <<endlog();
423  return 0;
424  }
425 
426  // create new:
427  ifstream namestream( name.c_str() );
428  string ior;
429  namestream >> ior;
430  return Create( ior, true);
431  }
432 
433  TaskContext* TaskContextProxy::Create(::RTT::corba::CTaskContext_ptr t, bool force_remote) {
434  Logger::In in("TaskContextProxy::Create");
435  if ( CORBA::is_nil(orb) ) {
436  log(Error) << "Can not create proxy when ORB is nill !"<<endlog();
437  return 0;
438  }
439  if ( CORBA::is_nil(t) ) {
440  log(Error) << "Can not create proxy for nill peer !" <<endlog();
441  return 0;
442  }
443 
444  // proxy present for this object ?
445  // is_equivalent is actually our best try.
446  for (PMap::iterator it = proxies.begin(); it != proxies.end(); ++it)
447  if ( (it->second)->_is_equivalent( t ) ) {
448  log(Debug) << "Existing proxy found !" <<endlog();
449  return it->first;
450  }
451 
452  // Check if the CTaskContext is actually a local TaskContext
453  if (! force_remote)
454  {
455  for (TaskContextServer::ServerMap::iterator it = TaskContextServer::servers.begin(); it != TaskContextServer::servers.end(); ++it)
456  if ( it->second->server()->_is_equivalent( t ) ) {
457  log(Debug) << "Local server found !" <<endlog();
458  return it->first;
459  }
460  }
461 
462  log(Debug) << "No local taskcontext found..." <<endlog();
463  // create new:
464  try {
465  TaskContextProxy* ctp = new TaskContextProxy( t );
466  return ctp;
467  }
468  catch( IllegalServer& is ) {
469  cerr << is.what() << endl;
470  }
471  return 0;
472  }
473 
475  try {
476  if (! CORBA::is_nil(mtask) )
477  return mtask->start();
478  } catch(...) {
479  mtask = CTaskContext::_nil();
480  this->setName("NotFound");
481  this->clear();
482  }
483  return false;
484  }
485 
487  try {
488  if (! CORBA::is_nil(mtask) )
489  return mtask->stop();
490  } catch(...) {
491  mtask = CTaskContext::_nil();
492  this->setName("NotFound");
493  this->clear();
494  }
495  return false;
496  }
497 
499  {
500  try {
501  if (! CORBA::is_nil(mtask) )
502  return mtask->recover();
503  } catch(...) {
504  mtask = CTaskContext::_nil();
505  this->setName("NotFound");
506  this->clear();
507  }
508  return false;
509  }
510 
511 
513  try {
514  if (! CORBA::is_nil(mtask) )
515  return mtask->activate();
516  } catch(...) {
517  mtask = CTaskContext::_nil();
518  this->setName("NotFound");
519  this->clear();
520  }
521  return false;
522  }
523 
525  try {
526  if (! CORBA::is_nil(mtask) )
527  return mtask->isActive();
528  } catch(...) {
529  mtask = CTaskContext::_nil();
530  }
531  return false;
532  }
533 
535  try {
536  if (! CORBA::is_nil(mtask) )
537  return mtask->isRunning();
538  } catch(...) {
539  mtask = CTaskContext::_nil();
540  }
541  return false;
542  }
543 
545  try {
546  if (! CORBA::is_nil(mtask) )
547  return mtask->configure();
548  } catch(...) {
549  mtask = CTaskContext::_nil();
550  this->setName("NotFound");
551  this->clear();
552  }
553  return false;
554  }
555 
557  try {
558  if (! CORBA::is_nil(mtask) )
559  return mtask->cleanup();
560  } catch(...) {
561  mtask = CTaskContext::_nil();
562  this->setName("NotFound");
563  this->clear();
564  }
565  return false;
566  }
567 
569  try {
570  if (! CORBA::is_nil(mtask) )
571  return mtask->isConfigured();
572  } catch(...) {
573  mtask = CTaskContext::_nil();
574  }
575  return false;
576  }
577 
579  try {
580  if (! CORBA::is_nil(mtask) )
581  return mtask->inFatalError();
582  } catch(...) {
583  mtask = CTaskContext::_nil();
584  }
585  return false;
586  }
587 
589  try {
590  if (! CORBA::is_nil(mtask) )
591  return mtask->inRunTimeError();
592  } catch(...) {
593  mtask = CTaskContext::_nil();
594  }
595  return false;
596  }
597 
599  {
600  try {
601  if (! CORBA::is_nil(mtask) )
602  return mtask->inException();
603  } catch(...) {
604  mtask = CTaskContext::_nil();
605  }
606  return false;
607  }
608 
609 
611  try {
612  if (! CORBA::is_nil(mtask) )
613  return TaskContext::TaskState( mtask->getTaskState() );
614  } catch(...) {
615  mtask = CTaskContext::_nil();
616  }
617  return TaskContext::Init;
618  }
619 
620  void TaskContextProxy::setName(const std::string& n)
621  {
622  //mtask->setName( n.c_str() );
623  }
624 
625  bool TaskContextProxy::addPeer( TaskContext* peer, std::string alias /*= ""*/ )
626  {
627  try {
628  if (CORBA::is_nil(mtask))
629  return false;
630 
631  // if peer is a proxy, add the proxy, otherwise, create new server.
632  TaskContextProxy* ctp = dynamic_cast<TaskContextProxy*>( peer );
633  if (ctp) {
634  if ( mtask->addPeer( ctp->server(), alias.c_str() ) ) {
635  this->synchronize();
636  return true;
637  }
638  return false;
639  }
640  // no server yet, create it.
642  if ( mtask->addPeer( newpeer->server(), alias.c_str() ) ) {
643  this->synchronize();
644  return true;
645  }
646  } catch(...) {
647  mtask = CTaskContext::_nil();
648  this->setName("NotFound");
649  this->clear();
650  }
651  return false;
652  }
653 
654  void TaskContextProxy::removePeer( const std::string& name )
655  {
656  try {
657  if (CORBA::is_nil(mtask))
658  return;
659  mtask->removePeer( name.c_str() );
660  } catch(...) {
661  mtask = CTaskContext::_nil();
662  this->setName("NotFound");
663  this->clear();
664  }
665  }
666 
668  {
669  try {
670  if (CORBA::is_nil(mtask))
671  return;
672  mtask->removePeer( peer->getName().c_str() );
673  } catch(...) {
674  mtask = CTaskContext::_nil();
675  this->setName("NotFound");
676  this->clear();
677  }
678  }
679 
681  {
682  try {
683  if (CORBA::is_nil(mtask))
684  return false;
686  return mtask->connectPeers( newpeer->server() );
687  } catch(...) {
688  mtask = CTaskContext::_nil();
689  this->setName("NotFound");
690  this->clear();
691  }
692  return false;
693  }
694 
695  void TaskContextProxy::disconnectPeers( const std::string& name )
696  {
697  try {
698  if (! CORBA::is_nil(mtask) )
699  mtask->disconnectPeers( name.c_str() );
700  } catch(...) {
701  mtask = CTaskContext::_nil();
702  this->setName("NotFound");
703  this->clear();
704  }
705  }
706 
708  {
709 
710  TaskContext::PeerList vlist;
711  try {
712  if (! CORBA::is_nil(mtask) ) {
713  corba::CTaskContext::CPeerNames_var plist = mtask->getPeerList();
714  for( size_t i =0; i != plist->length(); ++i)
715  vlist.push_back( std::string( plist[i] ) );
716  }
717  } catch(...) {
718  mtask = CTaskContext::_nil();
719  }
720  return vlist;
721  }
722 
723  bool TaskContextProxy::hasPeer( const std::string& peer_name ) const
724  {
725  try {
726  if (! CORBA::is_nil(mtask))
727  return mtask->hasPeer( peer_name.c_str() );
728  } catch(...) {
729  mtask = CTaskContext::_nil();
730  }
731  return false;
732  }
733 
734  TaskContext* TaskContextProxy::getPeer(const std::string& peer_name ) const
735  {
736  try {
737  if (CORBA::is_nil(mtask))
738  return 0;
739  corba::CTaskContext_ptr ct = mtask->getPeer( peer_name.c_str() );
740  if ( CORBA::is_nil(ct) )
741  return 0;
742  return TaskContextProxy::Create( ct );
743  } catch(...) {
744  mtask = CTaskContext::_nil();
745  }
746  return 0;
747  }
748 
750  {
751  try {
752  if (CORBA::is_nil(mtask))
753  return false;
755  return mtask->connectPorts( newpeer->server() );
756  } catch(...) {
757  mtask = CTaskContext::_nil();
758  this->setName("NotFound");
759  this->clear();
760  }
761  return false;
762  }
763 
765  {
766  try {
767  if (CORBA::is_nil(mtask))
768  return false;
770  return mtask->connectServices( newpeer->server() );
771  } catch(...) {
772  mtask = CTaskContext::_nil();
773  this->setName("NotFound");
774  this->clear();
775  }
776  return false;
777  }
778 
780  {
781  if (CORBA::is_nil(mtask)) {
782  this->clear();
783  return false;
784  }
785  try {
786  mtask->getName(); // basic check
787  return true;
788  } catch(...) {
789  // we could also try to re-establish the connection in case of naming...
790  this->clear();
791  mtask = CTaskContext::_nil();
792  }
793  return false;
794  }
795 
796  corba::CTaskContext_ptr TaskContextProxy::server() const {
797  if ( CORBA::is_nil(mtask) )
798  return CTaskContext::_nil();
799  return mtask.in();
800  }
801 
802  PortableServer::POA_ptr TaskContextProxy::ProxyPOA() {
803  if ( CORBA::is_nil(orb) )
804  return PortableServer::POA::_nil();
805  if ( CORBA::is_nil(proxy_poa) ) {
806  CORBA::Object_var poa_object =
807  orb->resolve_initial_references ("RootPOA");
808 
809  // new POA for the proxies:
810  // Use default manager, is already activated !
811  //PortableServer::POAManager_var proxy_manager = poa->the_POAManager ();
812  //CORBA::PolicyList pol;
813  //proxy_poa = poa->create_POA( "ProxyPOA", proxy_manager, pol );
814  proxy_poa =
815  PortableServer::POA::_narrow (poa_object.in ());
816  }
817  // note: do not use _retn(): user must duplicate in constructor.
818  return proxy_poa.in();
819  }
820 }}
821 
This class manages the creation of TaskContext Corba Servers and a Corba Object Request Broker (Orb) ...
static PortableServer::POA_var proxy_poa
For now one POA handles all proxies.
void fetchRequesters(ServiceRequester::shared_ptr parent, CServiceRequester_ptr csrq)
virtual void disconnectPeers(const std::string &name)
Remove a two-way connection from this task to a peer task.
virtual bool isRunning() const
Inspect if the component is in the Running or RunTimeError state.
base::AttributeBase * buildAttribute(std::string name, base::DataSourceBase::shared_ptr source=0) const
Build an Attribute of this type.
Definition: TypeInfo.hpp:221
bool storeProperty(PropertyBag &bag, const std::string &path, base::PropertyBase *item, const std::string &separator)
Stores a property in a bag given a certain path with transfer of ownership.
void fetchPorts(Service::shared_ptr parent, CDataFlowInterface_ptr serv)
Service::shared_ptr provides()
Returns this Service.
corba::CTaskContext_ptr server() const
Get the Corba Object of the CTaskContext.
The default, thread-less activity for any newly created TaskContext.
virtual bool activate()
This method starts the ExecutionEngine of this component in case it was not running.
virtual bool start()
This method starts the execution of the updateHook() with each trigger or period. ...
static std::ostream & nl(std::ostream &__os)
Insert a newline &#39; &#39; in the ostream.
Definition: Logger.cpp:373
STL namespace.
void forceActivity(base::ActivityInterface *new_act)
Forces the current activity to become new_act, even if this TaskContext is still running.
#define CORBA_EXCEPTION_INFO(x)
Definition: corba.h:70
boost::shared_ptr< ServiceRequester > shared_ptr
virtual bool isActive() const
Inspect if the component&#39;s ExecutionEngine is processing requests.
virtual bool stop()
This method stops the execution of updateHook() of this component.
A local factory for creating remote Corba methods.
virtual void setName(const std::string &n)
virtual bool inException() const
Inspect if the component is in the Exception state.
bool setActivity(base::ActivityInterface *new_act)
Sets the activity of this TaskContext.
basic_ostreams & endl(basic_ostreams &s)
Flush and newline.
Definition: rtstreams.cpp:110
virtual base::DataSourceBase::shared_ptr createAttributeDataSource(CService_ptr serv, const std::string &vname)=0
boost::shared_ptr< Service > shared_ptr
Definition: Service.hpp:101
virtual base::DataSourceBase::shared_ptr createPropertyDataSource(CService_ptr serv, const std::string &vname)=0
Create a data source for an attribute or property.
Proxy for a remote output port.
Definition: RemotePorts.hpp:96
ServiceRequester::shared_ptr requires()
Returns the object that manages which methods this Task requires to be implemented by another task...
PropertyBase * findProperty(const PropertyBag &bag, const std::string &nameSequence, const std::string &separator)
This function locates a Property in nested PropertyBags.
corba::CTaskContext_var mtask
Thrown if a server does not exist or has the wrong type.
virtual bool connectServices(TaskContext *peer)
Connects all requires/provides services of this component to these of a peer.
Convenient short notation for every sub-namespace of RTT.
void deletePropertyBag(PropertyBag &target)
This function iterates over a PropertyBag and recursively deletes all Property objects.
const char * what() const
static PortableServer::POA_var rootPOA
The root POA of this process.
This class manages the access of remote TaskContext Corba Servers and a Corba Object Request Broker (...
static TaskContextServer * Create(TaskContext *tc, bool use_naming=true, bool require_name_service=false)
Factory method: create a CORBA server for an existing TaskContext.
void initFromURIOrTaskname(std::string location, bool is_ior)
initializes the class from a stringified ior or taskname in NameServer.
base::AttributeBase * buildConstant(std::string name, base::DataSourceBase::shared_ptr source, int sizehint) const
Build a non modifyable instance of this type.
Definition: TypeInfo.hpp:172
static TaskContextProxy * CreateFromFile(std::string filename)
Factory method: create a CORBA Proxy for an existing TaskContextServer.
A property represents a named value of any type with a description.
Definition: Property.hpp:76
bool hasProtocol(int protocol_id) const
Check if this type is transporable over a given protocol.
Definition: TypeInfo.cpp:168
virtual void removePeer(const std::string &name)
Remove a one-way connection from this task to a peer task.
std::vector< std::string > PeerList
A list of Peer TaskContext names.
The state during component construction.
Definition: TaskCore.hpp:100
static PortableServer::POA_ptr ProxyPOA()
Returns the default POA for all proxies.
A class for representing a user type, and which can build instances of that type. ...
Definition: TypeInfo.hpp:66
virtual bool hasPeer(const std::string &peer_name) const
Return true if it knows a peer by that name.
TaskState
Describes the different states a component can have.
Definition: TaskCore.hpp:99
base::PropertyBase * buildProperty(const std::string &name, const std::string &desc, base::DataSourceBase::shared_ptr source=0) const
Build a Property of this type.
Definition: TypeInfo.hpp:213
std::list< base::PortInterface * > port_proxies
CDataFlowInterface does not delete ports automatically, because they can then be defined as members o...
virtual PeerList getPeerList() const
Return a standard container which contains all the Peer names of this TaskContext.
static void DestroyOrb()
Invoke this method once to cleanup the orb.
virtual TaskState getTaskState() const
Returns the current state of the TaskCore.
virtual bool inFatalError() const
Inspect if the component is in the FatalError state.
TypeTransporter * getProtocol(int protocol_id) const
Returns this type&#39;s transport for a given protocol.
Definition: TypeInfo.cpp:150
virtual bool inRunTimeError() const
Inspect if the component is in the RunTimeError state.
Notify the Logger in which &#39;module&#39; the message occured.
Definition: Logger.hpp:159
virtual bool ready()
Checks the validity of this TaskContext.
#define ORO_CORBA_PROTOCOL_ID
Definition: CorbaLib.hpp:45
static Logger & log()
As Instance(), but more userfriendly.
Definition: Logger.cpp:117
virtual bool connectPeers(TaskContext *peer)
Add a two-way connection from this task to a peer task.
The TaskContext is the C++ representation of an Orocos component.
Definition: TaskContext.hpp:93
virtual TaskContext * getPeer(const std::string &peer_name) const
Get a pointer to a peer of this task.
virtual bool addPeer(TaskContext *peer, std::string alias="")
Add a one-way connection from this task to a peer task.
virtual bool cleanup()
This method instructs a stopped component to enter the pre-operational state again.
TaskContextProxy()
A Private constructor which does nothing.
Mirrors a remote operation caller in a ServiceRequestor.
boost::intrusive_ptr< DataSourceBase > shared_ptr
Use this type to store a pointer to a DataSourceBase.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
virtual bool configure()
This method instructs the component to (re-)read configuration data and try to enter the Stopped stat...
void fetchServices(Service::shared_ptr parent, CService_ptr mtask)
The base class of every data flow port.
Proxy for a remote input port.
boost::shared_ptr< TypeInfoRepository > shared_ptr
virtual bool isConfigured() const
Inspect if the component is configured, i.e.
virtual bool connectPorts(TaskContext *peer)
Add a data flow connection from this task&#39;s ports to a peer&#39;s ports.
static TaskContextProxy * Create(std::string name, bool is_ior=false)
Factory method: create a CORBA Proxy for an existing TaskContextServer.
virtual bool recover()
Call this method in a RunTimeError or Exception state to indicate that the run-time error conditions ...
CTaskContext_ptr server() const
Get the Corba Object of this TaskContext.
static CORBA::ORB_var orb
The orb of this process.
virtual const std::string & getName() const
Returns the name of this TaskContext.
Extends the TypeTransporter in order to allow the creation of channel elements or output halves for a...
virtual void clear()
Clear the complete interface of this Component.
A CTaskContext is the main entry point of a distributed component and maps to a RTT::TaskContext.
Definition: TaskContext.idl:33