📄 controltaskproxy.cpp
字号:
/*************************************************************************** tag: Peter Soetens Wed Jan 18 14:09:49 CET 2006 ControlTaskProxy.cxx ControlTaskProxy.cxx - description ------------------- begin : Wed January 18 2006 copyright : (C) 2006 Peter Soetens email : peter.soetens@fmtc.be *************************************************************************** * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public * * License as published by the Free Software Foundation; * * version 2 of the License. * * * * As a special exception, you may use this file as part of a free * * software library without restriction. Specifically, if other files * * instantiate templates or use macros or inline functions from this * * file, or you compile this file and link it with other files to * * produce an executable, this file does not by itself cause the * * resulting executable to be covered by the GNU General Public * * License. This exception does not however invalidate any other * * reasons why the executable file might be covered by the GNU General * * Public License. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU General Public * * License along with this library; if not, write to the Free Software * * Foundation, Inc., 59 Temple Place, * * Suite 330, Boston, MA 02111-1307 USA * * * ***************************************************************************/ #include "ControlTaskProxy.hpp"#include "ControlTaskServer.hpp"#include "ControlTaskC.h"#include "CorbaMethodFactory.hpp"#include "CorbaCommandFactory.hpp"#include "CORBAExpression.hpp"#include "ScriptingAccessProxy.hpp"#include "CorbaPort.hpp"#include "CommandInterface.hpp"#include "Types.hpp"#include "orbsvcs/CosNamingC.h"#include <iostream>#include <ace/String_Base.h>using namespace std;namespace RTT{namespace Corba{ IllegalServer::IllegalServer() : reason("This server does not exist or has the wrong type.") {} IllegalServer::~IllegalServer() throw() {} const char* IllegalServer::what() const throw() { return reason.c_str(); } std::map<Corba::ControlTask_ptr, ControlTaskProxy*> ControlTaskProxy::proxies; PortableServer::POA_var ControlTaskProxy::proxy_poa; ControlTaskProxy::~ControlTaskProxy() { if ( this->properties() ) { flattenPropertyBag( *this->properties() ); deleteProperties( *this->properties() ); } this->attributes()->clear(); } ControlTaskProxy::ControlTaskProxy(std::string name, bool is_ior) : TaskContext("NotFound") { try { if (is_ior) { // Use the first argument to create the task object reference, // in real applications we use the naming service, but let's do // the easy part first! CORBA::Object_var task_object = orb->string_to_object ( name.c_str() ); // Now downcast the object reference to the appropriate type mtask = Corba::ControlTask::_narrow (task_object.in ()); } else { // NameService CORBA::Object_var rootObj = orb->resolve_initial_references("NameService"); CosNaming::NamingContext_var rootContext = CosNaming::NamingContext::_narrow(rootObj.in()); if (CORBA::is_nil(rootContext.in() )) { log(Error) << "ControlTaskProxy could not acquire NameService."<<endlog(); throw IllegalServer(); } Logger::log() <<Logger::Debug << "ControlTaskProxy found CORBA NameService."<<endlog(); CosNaming::Name serverName; serverName.length(2); serverName[0].id = CORBA::string_dup("ControlTasks"); serverName[1].id = CORBA::string_dup( name.c_str() ); // Get object reference CORBA::Object_var task_object = rootContext->resolve(serverName); mtask = Corba::ControlTask::_narrow (task_object.in ()); } if ( CORBA::is_nil( mtask.in() ) ) { Logger::log() << Logger::Error << "Failed to acquire ControlTaskServer '"+name+"'."<<endlog(); throw IllegalServer(); } CORBA::String_var nm = mtask->getName(); // force connect to object. std::string newname( nm.in() ); this->mtask_name = newname; Logger::log() << Logger::Info << "Successfully connected to ControlTaskServer '"+newname+"'."<<endlog(); proxies[mtask] = this; } catch (CORBA::Exception &e) { log(Error)<< "CORBA exception raised when resolving Object !" << endlog(); Logger::log() << e._info().c_str() << endlog(); } catch (...) { log(Error) <<"Unknown Exception in ControlTaskProxy construction!"<<endlog(); throw; } this->synchronize(); } ControlTaskProxy::ControlTaskProxy( ::RTT::Corba::ControlTask_ptr taskc) : TaskContext("CORBAProxy"), mtask( taskc ) { try { CORBA::String_var nm = mtask->getName(); // force connect to object. std::string name( nm.in() ); this->mtask_name = name; proxies[mtask] = this; } catch (CORBA::Exception &e) { log(Error) << "CORBA exception raised when creating ControlTaskProxy!" << Logger::nl; Logger::log() << e._info().c_str() << endlog(); } catch (...) { throw; } this->synchronize(); } void ControlTaskProxy::synchronize() { if (!mtask) return; // load command and method factories. // methods: log(Debug) << "Fetching Methods."<<endlog(); MethodInterface_var mfact = mtask->methods(); if (mfact) { MethodList_var objs; objs = mfact->getMethods(); for ( size_t i=0; i < objs->length(); ++i) { this->methods()->add( objs[i].in(), new CorbaMethodFactory( objs[i].in(), mfact.in(), ProxyPOA() ) ); } } // commands: log(Debug) << "Fetching Commands."<<endlog(); CommandInterface_var cfact = mtask->commands(); if (cfact) { CommandList_var objs; objs = cfact->getCommands(); for ( size_t i=0; i < objs->length(); ++i) { this->commands()->add( objs[i].in(), new CorbaCommandFactory( objs[i].in(), cfact.in(), ProxyPOA() ) ); } } // first do properties: log(Debug) << "Fetching Properties."<<endlog(); AttributeInterface::PropertyNames_var props = mtask->attributes()->getPropertyList(); for (size_t i=0; i != props->length(); ++i) { if ( this->attributes()->hasProperty( string(props[i].name.in()) ) ) continue; // previously added. Expression_var expr = mtask->attributes()->getProperty( props[i].name.in() ); if ( CORBA::is_nil( expr ) ) { log(Error) <<"Property "<< string(props[i].name.in()) << " present in getPropertyList() but not accessible."<<endlog(); continue; }#if 0 // This code may trigger endless recurse if server has recursive prop bags. // By using Property<PropertyBag>::narrow( ... ) this is no longer needed. // See if it is a PropertyBag: CORBA::Any_var any = expr->get(); PropertyBag bag; if ( AnyConversion<PropertyBag>::update( *any, bag ) ) { Property<PropertyBag>* pbag = new Property<PropertyBag>( string(props[i].name.in()), string(props[i].description.in()), bag); this->attributes()->addProperty( pbag ); continue; }#endif AssignableExpression_var as_expr = AssignableExpression::_narrow( expr.in() ); // addProperty also adds as attribute... if ( CORBA::is_nil( as_expr ) ) { log(Error) <<"Property "<< string(props[i].name.in()) << " was not writable !"<<endlog(); } else { // If the type is known, immediately build the correct property and datasource, // otherwise, build a property of CORBA::Any. CORBA::String_var tn = as_expr->getTypeName(); TypeInfo* ti = TypeInfoRepository::Instance()->type( tn.in() ); Logger::log() <<Logger::Info << "Looking up Property " << tn.in(); if ( ti ) { this->attributes()->addProperty( ti->buildProperty( props[i].name.in(), props[i].description.in(), ti->buildCorbaProxy( as_expr.in() ) ) ); Logger::log() <<Logger::Info <<" found!"<<endlog(); } else { this->attributes()->addProperty( new Property<CORBA::Any_ptr>( string(props[i].name.in()), string(props[i].description.in()), new CORBAAssignableExpression<Property<CORBA::Any_ptr>::DataSourceType>( as_expr.in() ) ) ); Logger::log() <<Logger::Info<<" not found :-("<<endlog(); } } } log(Debug) << "Fetching Attributes."<<endlog(); // add attributes not yet added by properties: AttributeInterface::AttributeNames_var attrs = mtask->attributes()->getAttributeList(); for (size_t i=0; i != attrs->length(); ++i) { if ( this->attributes()->hasAttribute( string(attrs[i].in()) ) ) continue; // previously added. Expression_var expr = mtask->attributes()->getAttribute( attrs[i].in() ); if ( CORBA::is_nil( expr ) ) { log(Error) <<"Attribute "<< string(attrs[i].in()) << " present in getAttributeList() but not accessible."<<endlog(); continue; } AssignableExpression_var as_expr = AssignableExpression::_narrow( expr.in() ); // If the type is known, immediately build the correct attribute and datasource, // otherwise, build a attribute of CORBA::Any. CORBA::String_var tn = expr->getTypeName(); TypeInfo* ti = TypeInfoRepository::Instance()->type( tn.in() ); log(Info) << "Looking up Attribute " << tn.in(); if ( ti ) { Logger::log() <<": found!"<<endlog(); if ( CORBA::is_nil( as_expr ) ) { this->attributes()->setValue( ti->buildConstant( attrs[i].in(), ti->buildCorbaProxy( expr.in() ) ) ); } else { this->attributes()->setValue( ti->buildAttribute( attrs[i].in(), ti->buildCorbaProxy( as_expr.in() ) ) ); } } else { Logger::log() <<": not found :-("<<endlog(); if ( CORBA::is_nil( as_expr ) ) this->attributes()->setValue( new Constant<CORBA::Any_ptr>( attrs[i].in(), new CORBAExpression<CORBA::Any_ptr>( expr.in() ) ) ); else this->attributes()->setValue( new Attribute<CORBA::Any_ptr>( attrs[i].in(), new CORBAAssignableExpression<CORBA::Any_ptr>( as_expr.in() ) ) ); } } log(Debug) << "Fetching ScriptingAccess."<<endlog(); Corba::ScriptingAccess_var saC = mtask->scripting(); if ( saC ) { delete mscriptAcc; mscriptAcc = new ScriptingAccessProxy( saC.in() ); } log(Debug) << "Fetching Ports."<<endlog(); DataFlowInterface_var dfact = mtask->ports(); if (dfact) { DataFlowInterface::PortNames_var objs; objs = dfact->getPorts(); for ( size_t i=0; i < objs->length(); ++i) { this->ports()->addPort( new CorbaPort( objs[i].in(), dfact.in(), ProxyPOA() ) ); } } this->fetchObjects(this, mtask.in() ); log(Debug) << "All Done."<<endlog(); } // Recursively fetch remote objects and create local proxies. void ControlTaskProxy::fetchObjects(OperationInterface* parent, ControlObject_ptr mtask) { log(Debug) << "Fetching Objects of "<<parent->getName()<<":"<<endlog(); Corba::ObjectList_var plist = mtask->getObjectList(); for( size_t i =0; i != plist->length(); ++i) { if ( string( plist[i] ) == "this") continue; ControlObject_var cobj = mtask->getObject(plist[i]); CORBA::String_var descr = cobj->getDescription(); TaskObject* tobj = new TaskObject( std::string(plist[i]), std::string(descr.in()) ); // add attributes: log(Info) << plist[i] << ": fetching Attributes."<<endlog(); AttributeInterface::AttributeNames_var attrs = cobj->attributes()->getAttributeList(); for (size_t i=0; i != attrs->length(); ++i) { if ( tobj->attributes()->hasAttribute( string(attrs[i].in()) ) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -