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


rtt
Author(s): RTT Developers
autogenerated on Wed Aug 26 2015 16:16:19