TaskContextProxy.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Wed Jan 18 14:09:49 CET 2006  TaskContextProxy.cxx
00003 
00004                         TaskContextProxy.cxx -  description
00005                            -------------------
00006     begin                : Wed January 18 2006
00007     copyright            : (C) 2006 Peter Soetens
00008     email                : peter.soetens@fmtc.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 #include "TaskContextProxy.hpp"
00039 #include "TaskContextServer.hpp"
00040 #include "TaskContextC.h"
00041 #include "CorbaOperationCallerFactory.hpp"
00042 #include "CorbaLib.hpp"
00043 #include "OperationCallerProxy.hpp"
00044 
00045 #include "../../types/Types.hpp"
00046 #include "../../extras/SequentialActivity.hpp"
00047 #include "corba.h"
00048 #ifdef CORBA_IS_TAO
00049 #include "tao/TimeBaseC.h"
00050 #include "tao/Messaging/Messaging.h"
00051 #include "tao/Messaging/Messaging_RT_PolicyC.h"
00052 #include "orbsvcs/CosNamingC.h"
00053 #include <ace/String_Base.h>
00054 #else
00055 #include <omniORB4/Naming.hh>
00056 #endif
00057 #include <iostream>
00058 #include <fstream>
00059 #include <string>
00060 
00061 #include "RemotePorts.hpp"
00062 
00063 using namespace std;
00064 using namespace RTT::detail;
00065 
00066 namespace RTT
00067 {namespace corba
00068 {
00069     IllegalServer::IllegalServer() : reason("This server does not exist or has the wrong type.") {}
00070 
00071     IllegalServer::IllegalServer(const std::string& r) : reason(r) {}
00072 
00073     IllegalServer::~IllegalServer() throw() {}
00074 
00075     const char* IllegalServer::what() const throw() { return reason.c_str(); }
00076 
00077 
00078     std::map<TaskContextProxy*, corba::CTaskContext_ptr> TaskContextProxy::proxies;
00079 
00080     PortableServer::POA_var TaskContextProxy::proxy_poa;
00081 
00082     TaskContextProxy::~TaskContextProxy()
00083     {
00084         log(Info) << "Terminating TaskContextProxy for " <<  this->getName() <<endlog();
00085         if ( this->properties() ) {
00086             deletePropertyBag( *this->properties() );
00087         }
00088         this->attributes()->clear();
00089         for (list<PortInterface*>::iterator it = port_proxies.begin(); it != port_proxies.end(); ++it)
00090             delete *it;
00091         proxies.erase(this);
00092     }
00093 
00094     TaskContextProxy::TaskContextProxy(std::string name, bool is_ior)
00095         : TaskContext("NotFound")
00096     {
00097         initFromURIOrTaskname(name, is_ior);
00098     }
00099 
00100     TaskContextProxy::TaskContextProxy(): TaskContext("NotFound")
00101     {
00102 
00103     }
00104 
00105     void TaskContextProxy::initFromURIOrTaskname(string name, bool is_ior)
00106     {
00107         Logger::In in("TaskContextProxy");
00108         this->clear();
00109         this->setActivity( new SequentialActivity() );
00110         try {
00111             if (is_ior) {
00112                 // Use the first argument to create the task object reference,
00113                 // in real applications we use the naming service, but let's do
00114                 // the easy part first!
00115                 CORBA::Object_var task_object =
00116                     orb->string_to_object ( name.c_str() );
00117 
00118                 // Now downcast the object reference to the appropriate type
00119                 mtask = corba::CTaskContext::_narrow (task_object.in ());
00120             } else {
00121                 // NameService
00122                 CORBA::Object_var rootObj;
00123                 CosNaming::NamingContext_var rootContext;
00124                 try {
00125                     rootObj = orb->resolve_initial_references("NameService");
00126                     rootContext = CosNaming::NamingContext::_narrow(rootObj);
00127                 } catch (...) {}
00128 
00129                 if (CORBA::is_nil(rootContext)) {
00130                     std::string err("TaskContextProxy could not acquire NameService.");
00131                     log(Error) << err <<endlog();
00132                     throw IllegalServer(err);
00133                 }
00134                 Logger::log() <<Logger::Debug << "TaskContextProxy found CORBA NameService."<<endlog();
00135                 CosNaming::Name serverName;
00136                 serverName.length(2);
00137                 serverName[0].id = CORBA::string_dup("TaskContexts");
00138                 serverName[1].id = CORBA::string_dup( name.c_str() );
00139 
00140                 // Get object reference
00141                 CORBA::Object_var task_object = rootContext->resolve(serverName);
00142                 mtask = corba::CTaskContext::_narrow (task_object.in ());
00143             }
00144             if ( CORBA::is_nil( mtask ) ) {
00145                 std::string err("Failed to acquire TaskContextServer '"+name+"'.");
00146                 Logger::log() << Logger::Error << err <<endlog();
00147                 throw IllegalServer(err);
00148             }
00149             CORBA::String_var nm = mtask->getName(); // force connect to object.
00150             std::string newname( nm.in() );
00151             this->provides()->setName( newname );
00152             Logger::log() << Logger::Info << "Successfully connected to TaskContextServer '"+newname+"'."<<endlog();
00153             proxies[this] = mtask.in();
00154         }
00155         catch (CORBA::Exception &e) {
00156             log(Error)<< "CORBA exception raised when resolving Object !" << endlog();
00157             Logger::log() << CORBA_EXCEPTION_INFO(e) << endlog();
00158             throw;
00159         }
00160         catch (IllegalServer& e) {
00161             // rethrow
00162             throw e;
00163         }
00164         catch (...) {
00165             log(Error) <<"Unknown Exception in TaskContextProxy construction!"<<endlog();
00166             throw;
00167         }
00168 
00169         this->synchronize();
00170     }
00171     
00172     TaskContextProxy::TaskContextProxy( ::RTT::corba::CTaskContext_ptr taskc)
00173         : TaskContext("CORBAProxy"), mtask( corba::CTaskContext::_duplicate(taskc) )
00174     {
00175         Logger::In in("TaskContextProxy");
00176         this->clear();
00177         // We can't use setActivity() since that would check isRunning() first.
00178         this->forceActivity( new SequentialActivity );
00179         try {
00180             CORBA::String_var nm = mtask->getName(); // force connect to object.
00181             std::string name( nm.in() );
00182             this->provides()->setName( name );
00183             proxies[this] = mtask.in();
00184         }
00185         catch (CORBA::Exception &e) {
00186             log(Error) << "CORBA exception raised when creating TaskContextProxy!" << Logger::nl;
00187             Logger::log() << CORBA_EXCEPTION_INFO(e) << endlog();
00188         }
00189         catch (...) {
00190             throw;
00191         }
00192         this->synchronize();
00193     }
00194 
00195     void TaskContextProxy::synchronize()
00196     {
00197         // Add here the interfaces that need to be synchronised every time a lookup is done.
00198         // Detect already added parts of an interface, does not yet detect removed parts...
00199         if (CORBA::is_nil(mtask))
00200             return;
00201         
00202         CService_var serv = mtask->getProvider("this");
00203         this->fetchServices(this->provides(), serv.in() );
00204 
00205         CServiceRequester_var srq = mtask->getRequester("this");
00206         this->fetchRequesters(this->requires(), srq.in() );
00207         log(Debug) << "All Done."<<endlog();
00208     }
00209 
00210     void TaskContextProxy::fetchRequesters(ServiceRequester::shared_ptr parent, CServiceRequester_ptr csrq)
00211     {
00212         COperationCallerNames_var opcnames = csrq->getOperationCallerNames();
00213 
00214         log(Debug) << "Fetching OperationCallers of " << parent->getRequestName()<<endlog();
00215         for ( size_t i=0; i < opcnames->length(); ++i) {
00216             if ( parent->getOperationCaller( string(opcnames[i].in() )))
00217                 continue; // already added.
00218             log(Debug) << "Requiring operation: "<< opcnames[i].in() <<endlog();
00219             parent->addOperationCaller( * new OperationCallerProxy(string(opcnames[i].in() ), CServiceRequester::_duplicate(csrq) ));
00220         }
00221 
00222         CRequestNames_var rqlist = csrq->getRequestNames();
00223 
00224         for( size_t i =0; i != rqlist->length(); ++i) {
00225             if ( string( rqlist[i] ) == "this")
00226                 continue;
00227             CServiceRequester_var cobj = csrq->getRequest(rqlist[i]);
00228 
00229             ServiceRequester::shared_ptr tobj = this->requires(std::string(rqlist[i]));
00230 
00231             // Recurse:
00232             this->fetchRequesters( tobj, cobj.in() );
00233         }
00234     }
00235 
00236     // Recursively fetch remote objects and create local proxies.
00237     void TaskContextProxy::fetchServices(Service::shared_ptr parent, CService_ptr serv)
00238     {
00239         log(Debug) << "Fetching "<<parent->getName()<<" Service:"<<endlog();
00240 
00241         // Fetch ports
00242         this->fetchPorts(parent, serv);
00243 
00244         // load command and method factories.
00245         // methods:
00246         log(Debug) << "Fetching Operations."<<endlog();
00247         COperationInterface::COperationList_var objs;
00248         objs = serv->getOperations();
00249         for ( size_t i=0; i < objs->length(); ++i) {
00250             if ( parent->hasMember( string(objs[i].in() )))
00251                 continue; // already added.
00252             log(Debug) << "Providing operation: "<< objs[i].in() <<endlog();
00253             parent->add( objs[i].in(), new CorbaOperationCallerFactory( objs[i].in(), serv, ProxyPOA() ) );
00254         }
00255 
00256         // first do properties:
00257         log(Debug) << "Fetching Properties."<<endlog();
00258         // a dot-separated list of subbags and items
00259         CConfigurationInterface::CPropertyNames_var props = serv->getPropertyList();
00260 
00261         for (size_t i=0; i != props->length(); ++i) {
00262             if ( findProperty( *parent->properties(), string(props[i].name.in()), "." ) )
00263                 continue; // previously added.
00264             if ( !serv->hasProperty( props[i].name.in() ) ) {
00265                 log(Error) <<"Property "<< string(props[i].name.in()) << " present in getPropertyList() but not accessible."<<endlog();
00266                 continue;
00267             }
00268            // If the type is known, immediately build the correct property and datasource.
00269             CORBA::String_var tn = serv->getPropertyTypeName(props[i].name.in());
00270             TypeInfo* ti = TypeInfoRepository::Instance()->type( tn.in() );
00271 
00272             // decode the prefix and property name from the given name:
00273             string pname = string( props[i].name.in() );
00274             pname = pname.substr( pname.rfind(".") + 1 );
00275             string prefix = string( props[i].name.in() );
00276             if ( prefix.rfind(".") == string::npos ) {
00277                 prefix.clear();
00278             }
00279             else {
00280                 prefix = prefix.substr( 0, prefix.rfind(".") );
00281             }
00282 
00283             if ( ti && ti->hasProtocol(ORO_CORBA_PROTOCOL_ID)) {
00284                 CorbaTypeTransporter* ctt = dynamic_cast<CorbaTypeTransporter*>(ti->getProtocol(ORO_CORBA_PROTOCOL_ID));
00285                 assert(ctt);
00286                 // data source needs full remote path name
00287                 DataSourceBase::shared_ptr ds = ctt->createPropertyDataSource( serv, props[i].name.in() );
00288                 storeProperty( *parent->properties(), prefix, ti->buildProperty( pname, props[i].description.in(), ds));
00289                 log(Debug) << "Looked up Property " << tn.in() << " "<< pname <<": created."<<endlog();
00290             }
00291             else {
00292                 if ( string("PropertyBag") == tn.in() ) {
00293                     storeProperty(*parent->properties(), prefix, new Property<PropertyBag>( pname, props[i].description.in()) );
00294                     log(Debug) << "Looked up PropertyBag " << tn.in() << " "<< pname <<": created."<<endlog();
00295                 } else
00296                     log(Error) << "Looked up Property " << tn.in() << " "<< pname <<": type not known. Check your RTT_COMPONENT_PATH ( \""<<getenv("RTT_COMPONENT_PATH")<<" \")."<<endlog();
00297             }
00298         }
00299 
00300         log(Debug) << "Fetching Attributes."<<endlog();
00301         CConfigurationInterface::CAttributeNames_var attrs = serv->getAttributeList();
00302         for (size_t i=0; i != attrs->length(); ++i) {
00303             if ( parent->hasAttribute( string(attrs[i].in()) ) )
00304                 continue; // previously added.
00305             if ( !serv->hasAttribute( attrs[i].in() ) ) {
00306                 log(Error) <<"Attribute '"<< string(attrs[i].in()) << "' present in getAttributeList() but not accessible."<<endlog();
00307                 continue;
00308             }
00309             // If the type is known, immediately build the correct attribute and datasource,
00310             CORBA::String_var tn = serv->getAttributeTypeName( attrs[i].in() );
00311             TypeInfo* ti = TypeInfoRepository::Instance()->type( tn.in() );
00312             if ( ti && ti->hasProtocol(ORO_CORBA_PROTOCOL_ID) ) {
00313                 log(Debug) << "Looking up Attribute " << tn.in() <<": found!"<<endlog();
00314                 CorbaTypeTransporter* ctt = dynamic_cast<CorbaTypeTransporter*>(ti->getProtocol(ORO_CORBA_PROTOCOL_ID));
00315                 assert(ctt);
00316                 // this function should check itself for const-ness of the remote Attribute:
00317                 DataSourceBase::shared_ptr ds = ctt->createAttributeDataSource( serv, attrs[i].in() );
00318                 if ( serv->isAttributeAssignable( attrs[i].in() ) )
00319                     parent->setValue( ti->buildAttribute( attrs[i].in(), ds));
00320                 else
00321                     parent->setValue( ti->buildConstant( attrs[i].in(), ds));
00322             } else {
00323                 log(Error) << "Looking up Attribute " << tn.in();
00324                 Logger::log() <<": type not known. Check your RTT_COMPONENT_PATH ( \""<<getenv("RTT_COMPONENT_PATH")<<" \")."<<endlog();
00325             }
00326         }
00327 
00328         CService::CProviderNames_var plist = serv->getProviderNames();
00329 
00330         for( size_t i =0; i != plist->length(); ++i) {
00331             if ( string( plist[i] ) == "this")
00332                 continue;
00333             CService_var cobj = serv->getService(plist[i]);
00334             CORBA::String_var descr = cobj->getServiceDescription();
00335 
00336             Service::shared_ptr tobj = parent->provides(std::string(plist[i]));
00337             tobj->doc( descr.in() );
00338 
00339             // Recurse:
00340             this->fetchServices( tobj, cobj.in() );
00341         }
00342     }
00343 
00344     // Fetch remote ports and create local proxies
00345     void TaskContextProxy::fetchPorts(RTT::Service::shared_ptr parent, CDataFlowInterface_ptr dfact)
00346     {
00347         log(Debug) << "Fetching Ports for service "<<parent->getName()<<"."<<endlog();
00348         TypeInfoRepository::shared_ptr type_repo = TypeInfoRepository::Instance();
00349         if (dfact) {
00350             CDataFlowInterface::CPortDescriptions_var objs = dfact->getPortDescriptions();
00351             for ( size_t i=0; i < objs->length(); ++i) {
00352                 CPortDescription port = objs[i];
00353                 if (parent->getPort( port.name.in() ))
00354                     continue; // already added.
00355 
00356                 TypeInfo const* type_info = type_repo->type(port.type_name.in());
00357                 if (!type_info)
00358                 {
00359                     log(Warning) << "remote port " << port.name
00360                         << " has a type that cannot be marshalled over CORBA: " << port.type_name << ". "
00361                         << "It is ignored by TaskContextProxy" << endlog();
00362                 }
00363                 else
00364                 {
00365                     PortInterface* new_port;
00366                     if (port.type == RTT::corba::CInput)
00367                         new_port = new RemoteInputPort( type_info, dfact, port.name.in(), ProxyPOA() );
00368                     else
00369                         new_port = new RemoteOutputPort( type_info, dfact, port.name.in(), ProxyPOA() );
00370 
00371                     parent->addPort(*new_port);
00372                     port_proxies.push_back(new_port); // see comment in definition of port_proxies
00373                 }
00374             }
00375         }
00376     }
00377 
00378     void TaskContextProxy::DestroyOrb()
00379     {
00380         try {
00381             // Destroy the POA, waiting until the destruction terminates
00382             //poa->destroy (1, 1);
00383                 if (orb) {
00384                         orb->destroy();
00385                         rootPOA = 0;
00386                         orb = 0;
00387                         std::cerr <<"Orb destroyed."<<std::endl;
00388                 }
00389         }
00390         catch (CORBA::Exception &e) {
00391             log(Error) << "Orb Init : CORBA exception raised!" << Logger::nl;
00392             Logger::log() << CORBA_EXCEPTION_INFO(e) << endlog();
00393         }
00394     }
00395 
00396     TaskContextProxy* TaskContextProxy::Create(std::string name, bool is_ior /*=false*/) {
00397         if ( CORBA::is_nil(orb) ) {
00398             log(Error) << "Won't create a proxy for '"<<name<<"' : orb is nill. Call TaskContextProxy::InitOrb(argc, argv); before TaskContextProxy::Create()." <<endlog();
00399             return 0;
00400         }
00401         if ( name.empty() ) {
00402             log(Error) << "Can't create a proxy with an empty name." <<endlog();
00403             return 0;
00404         }
00405         // create new:
00406         try {
00407             TaskContextProxy* ctp = new TaskContextProxy( name, is_ior );
00408             return ctp;
00409         }
00410         catch( IllegalServer& is ) {
00411             cerr << is.what() << endl;
00412         }
00413         return 0;
00414     }
00415 
00416     TaskContextProxy* TaskContextProxy::CreateFromFile(std::string name) {
00417         if ( CORBA::is_nil(orb) ) {
00418             log(Error) << "Won't create a proxy for '"<<name<<"' : orb is nill. Call TaskContextProxy::InitOrb(argc, argv); before TaskContextProxy::Create()." <<endlog();
00419             return 0;
00420         }
00421         if ( name.empty() ) {
00422             log(Error) << "Can't create a proxy with an empty file name." <<endlog();
00423             return 0;
00424         }
00425 
00426         // create new:
00427         ifstream namestream( name.c_str() );
00428         string ior;
00429         namestream >> ior;
00430         return Create( ior, true);
00431     }
00432 
00433     TaskContext* TaskContextProxy::Create(::RTT::corba::CTaskContext_ptr t, bool force_remote) {
00434         Logger::In in("TaskContextProxy::Create");
00435         if ( CORBA::is_nil(orb) ) {
00436             log(Error) << "Can not create proxy when ORB is nill !"<<endlog();
00437             return 0;
00438         }
00439         if ( CORBA::is_nil(t) ) {
00440             log(Error) << "Can not create proxy for nill peer !" <<endlog();
00441             return 0;
00442         }
00443 
00444         // proxy present for this object ?
00445         // is_equivalent is actually our best try.
00446         for (PMap::iterator it = proxies.begin(); it != proxies.end(); ++it)
00447             if ( (it->second)->_is_equivalent( t ) ) {
00448                 log(Debug) << "Existing proxy found !" <<endlog();
00449                 return it->first;
00450             }
00451 
00452         // Check if the CTaskContext is actually a local TaskContext
00453         if (! force_remote)
00454         {
00455             for (TaskContextServer::ServerMap::iterator it = TaskContextServer::servers.begin(); it != TaskContextServer::servers.end(); ++it)
00456                 if ( it->second->server()->_is_equivalent( t ) ) {
00457                     log(Debug) << "Local server found !" <<endlog();
00458                     return it->first;
00459                 }
00460         }
00461 
00462         log(Debug) << "No local taskcontext found..." <<endlog();
00463         // create new:
00464         try {
00465             TaskContextProxy* ctp = new TaskContextProxy( t );
00466             return ctp;
00467         }
00468         catch( IllegalServer& is ) {
00469             cerr << is.what() << endl;
00470         }
00471         return 0;
00472     }
00473 
00474     bool TaskContextProxy::start() {
00475         try {
00476             if (! CORBA::is_nil(mtask) )
00477                 return mtask->start();
00478         } catch(...) {
00479             mtask = CTaskContext::_nil();
00480             this->setName("NotFound");
00481             this->clear();
00482         }
00483         return false;
00484     }
00485 
00486     bool TaskContextProxy::stop() {
00487         try {
00488             if (! CORBA::is_nil(mtask) )
00489                 return mtask->stop();
00490         } catch(...) {
00491             mtask = CTaskContext::_nil();
00492             this->setName("NotFound");
00493             this->clear();
00494         }
00495         return false;
00496     }
00497 
00498     bool TaskContextProxy::activate() {
00499         try {
00500             if (! CORBA::is_nil(mtask) )
00501                 return mtask->activate();
00502         } catch(...) {
00503             mtask = CTaskContext::_nil();
00504             this->setName("NotFound");
00505             this->clear();
00506         }
00507         return false;
00508     }
00509 
00510     bool TaskContextProxy::isActive() const {
00511         try {
00512             if (! CORBA::is_nil(mtask) )
00513                 return mtask->isActive();
00514         } catch(...) {
00515             mtask = CTaskContext::_nil();
00516         }
00517         return false;
00518     }
00519 
00520     bool TaskContextProxy::isRunning() const {
00521         try {
00522             if (! CORBA::is_nil(mtask) )
00523                 return mtask->isRunning();
00524         } catch(...) {
00525             mtask = CTaskContext::_nil();
00526         }
00527         return false;
00528     }
00529 
00530     bool TaskContextProxy::configure() {
00531         try {
00532             if (! CORBA::is_nil(mtask) )
00533                 return mtask->configure();
00534         } catch(...) {
00535             mtask = CTaskContext::_nil();
00536             this->setName("NotFound");
00537             this->clear();
00538         }
00539         return false;
00540     }
00541 
00542     bool TaskContextProxy::cleanup() {
00543         try {
00544             if (! CORBA::is_nil(mtask) )
00545                 return mtask->cleanup();
00546         } catch(...) {
00547             mtask = CTaskContext::_nil();
00548             this->setName("NotFound");
00549             this->clear();
00550         }
00551         return false;
00552     }
00553 
00554     bool TaskContextProxy::isConfigured() const {
00555         try {
00556             if (! CORBA::is_nil(mtask) )
00557                 return mtask->isConfigured();
00558         } catch(...) {
00559             mtask = CTaskContext::_nil();
00560         }
00561         return false;
00562     }
00563 
00564     bool TaskContextProxy::inFatalError() const {
00565         try {
00566             if (! CORBA::is_nil(mtask) )
00567                 return mtask->inFatalError();
00568         } catch(...) {
00569             mtask = CTaskContext::_nil();
00570         }
00571         return false;
00572     }
00573 
00574     bool TaskContextProxy::inRunTimeError() const {
00575         try {
00576             if (! CORBA::is_nil(mtask) )
00577                 return mtask->inRunTimeError();
00578         } catch(...) {
00579             mtask = CTaskContext::_nil();
00580         }
00581         return false;
00582     }
00583 
00584     TaskContext::TaskState TaskContextProxy::getTaskState() const {
00585         try {
00586             if (! CORBA::is_nil(mtask) )
00587                 return TaskContext::TaskState( mtask->getTaskState() );
00588         } catch(...) {
00589             mtask = CTaskContext::_nil();
00590         }
00591         return TaskContext::Init;
00592     }
00593 
00594     void TaskContextProxy::setName(const std::string& n)
00595     {
00596         //mtask->setName( n.c_str() );
00597     }
00598 
00599     bool TaskContextProxy::addPeer( TaskContext* peer, std::string alias /*= ""*/ )
00600     {
00601         try {
00602             if (CORBA::is_nil(mtask))
00603                 return false;
00604 
00605             // if peer is a proxy, add the proxy, otherwise, create new server.
00606             TaskContextProxy* ctp = dynamic_cast<TaskContextProxy*>( peer );
00607             if (ctp) {
00608                 if ( mtask->addPeer( ctp->server(), alias.c_str() ) ) {
00609                     this->synchronize();
00610                     return true;
00611                 }
00612                 return false;
00613             }
00614             // no server yet, create it.
00615             TaskContextServer* newpeer = TaskContextServer::Create(peer);
00616             if ( mtask->addPeer( newpeer->server(), alias.c_str() ) ) {
00617                 this->synchronize();
00618                 return true;
00619             }
00620         } catch(...) {
00621             mtask = CTaskContext::_nil();
00622             this->setName("NotFound");
00623             this->clear();
00624         }
00625         return false;
00626     }
00627 
00628     void TaskContextProxy::removePeer( const std::string& name )
00629     {
00630         try {
00631             if (CORBA::is_nil(mtask))
00632                 return;
00633             mtask->removePeer( name.c_str() );
00634         } catch(...) {
00635             mtask = CTaskContext::_nil();
00636             this->setName("NotFound");
00637             this->clear();
00638         }
00639     }
00640 
00641     void TaskContextProxy::removePeer( TaskContext* peer )
00642     {
00643         try {
00644             if (CORBA::is_nil(mtask))
00645                 return;
00646             mtask->removePeer( peer->getName().c_str() );
00647         } catch(...) {
00648             mtask = CTaskContext::_nil();
00649             this->setName("NotFound");
00650             this->clear();
00651         }
00652     }
00653 
00654     bool TaskContextProxy::connectPeers( TaskContext* peer )
00655     {
00656         try {
00657             if (CORBA::is_nil(mtask))
00658                 return false;
00659             TaskContextServer* newpeer = TaskContextServer::Create(peer);
00660             return mtask->connectPeers( newpeer->server() );
00661         } catch(...) {
00662             mtask = CTaskContext::_nil();
00663             this->setName("NotFound");
00664             this->clear();
00665         }
00666         return false;
00667     }
00668 
00669     void TaskContextProxy::disconnectPeers( const std::string& name )
00670     {
00671         try {
00672             if (! CORBA::is_nil(mtask) )
00673                 mtask->disconnectPeers( name.c_str() );
00674         } catch(...) {
00675             mtask = CTaskContext::_nil();
00676             this->setName("NotFound");
00677             this->clear();
00678         }
00679     }
00680 
00681     TaskContext::PeerList TaskContextProxy::getPeerList() const
00682     {
00683 
00684         TaskContext::PeerList vlist;
00685         try {
00686             if (! CORBA::is_nil(mtask) ) {
00687                 corba::CTaskContext::CPeerNames_var plist = mtask->getPeerList();
00688                 for( size_t i =0; i != plist->length(); ++i)
00689                     vlist.push_back( std::string( plist[i] ) );
00690             }
00691         } catch(...) {
00692             mtask = CTaskContext::_nil();
00693         }
00694         return vlist;
00695     }
00696 
00697     bool TaskContextProxy::hasPeer( const std::string& peer_name ) const
00698     {
00699         try {
00700             if (! CORBA::is_nil(mtask))
00701                 return mtask->hasPeer( peer_name.c_str() );
00702         } catch(...) {
00703             mtask = CTaskContext::_nil();
00704         }
00705         return false;
00706     }
00707 
00708     TaskContext* TaskContextProxy::getPeer(const std::string& peer_name ) const
00709     {
00710         try {
00711             if (CORBA::is_nil(mtask))
00712                 return 0;
00713             corba::CTaskContext_ptr ct = mtask->getPeer( peer_name.c_str() );
00714             if ( CORBA::is_nil(ct) )
00715                 return 0;
00716             return TaskContextProxy::Create( ct );
00717         } catch(...) {
00718             mtask = CTaskContext::_nil();
00719         }
00720         return 0;
00721     }
00722 
00723     bool TaskContextProxy::connectPorts( TaskContext* peer )
00724     {
00725         try {
00726             if (CORBA::is_nil(mtask))
00727                 return false;
00728             TaskContextServer* newpeer = TaskContextServer::Create(peer);
00729             return mtask->connectPorts( newpeer->server() );
00730         } catch(...) {
00731             mtask = CTaskContext::_nil();
00732             this->setName("NotFound");
00733             this->clear();
00734         }
00735         return false;
00736     }
00737 
00738     bool TaskContextProxy::connectServices( TaskContext* peer )
00739     {
00740         try {
00741             if (CORBA::is_nil(mtask))
00742                 return false;
00743             TaskContextServer* newpeer = TaskContextServer::Create(peer);
00744             return mtask->connectServices( newpeer->server() );
00745         } catch(...) {
00746             mtask = CTaskContext::_nil();
00747             this->setName("NotFound");
00748             this->clear();
00749         }
00750         return false;
00751     }
00752 
00753     bool TaskContextProxy::ready()
00754     {
00755         if (CORBA::is_nil(mtask)) {
00756             this->clear();
00757             return false;
00758         }
00759         try {
00760             mtask->getName(); // basic check
00761             return true;
00762         } catch(...) {
00763             // we could also try to re-establish the connection in case of naming...
00764             this->clear();
00765             mtask = CTaskContext::_nil();
00766         }
00767         return false;
00768     }
00769 
00770     corba::CTaskContext_ptr TaskContextProxy::server() const {
00771         if ( CORBA::is_nil(mtask) )
00772             return CTaskContext::_nil();
00773         return mtask.in();
00774     }
00775 
00776     PortableServer::POA_ptr TaskContextProxy::ProxyPOA() {
00777         if ( CORBA::is_nil(orb) )
00778             return PortableServer::POA::_nil();
00779         if ( CORBA::is_nil(proxy_poa) ) {
00780             CORBA::Object_var poa_object =
00781                 orb->resolve_initial_references ("RootPOA");
00782 
00783             // new POA for the proxies:
00784             // Use default manager, is already activated !
00785             //PortableServer::POAManager_var proxy_manager = poa->the_POAManager ();
00786             //CORBA::PolicyList pol;
00787             //proxy_poa = poa->create_POA( "ProxyPOA", proxy_manager, pol );
00788             proxy_poa =
00789                 PortableServer::POA::_narrow (poa_object.in ());
00790         }
00791         // note: do not use _retn(): user must duplicate in constructor.
00792         return proxy_poa.in();
00793     }
00794 }}
00795 


rtt
Author(s): RTT Developers
autogenerated on Sat Jun 8 2019 18:46:32