$search
00001 /*************************************************************************** 00002 tag: Peter Soetens Thu Jul 3 15:34:48 CEST 2008 CorbaDeploymentComponent.cpp 00003 00004 CorbaDeploymentComponent.cpp - description 00005 ------------------- 00006 begin : Thu July 03 2008 00007 copyright : (C) 2008 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 Lesser General Public * 00013 * License as published by the Free Software Foundation; either * 00014 * version 2.1 of the License, or (at your option) any later version. * 00015 * * 00016 * This library is distributed in the hope that it will be useful, * 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00019 * Lesser General Public License for more details. * 00020 * * 00021 * You should have received a copy of the GNU Lesser General Public * 00022 * License along with this library; if not, write to the Free Software * 00023 * Foundation, Inc., 59 Temple Place, * 00024 * Suite 330, Boston, MA 02111-1307 USA * 00025 * * 00026 ***************************************************************************/ 00027 00028 00029 #include "CorbaDeploymentComponent.hpp" 00030 #include <rtt/transports/corba/TaskContextProxy.hpp> 00031 #include <rtt/transports/corba/TaskContextServer.hpp> 00032 #include <rtt/deployment/ComponentLoader.hpp> 00033 #include "ocl/Component.hpp" 00034 #include <fstream> 00035 00036 namespace OCL 00037 { 00038 00043 RTT::TaskContext* createTaskContextProxy(std::string name) 00044 { 00045 log(Debug) << "createTaskContextProxy" <<endlog(); 00046 return ::RTT::corba::TaskContextProxy::Create(name, false); 00047 } 00048 00053 RTT::TaskContext* createTaskContextProxyIORFile(std::string iorfilename) 00054 { 00055 log(Debug) << "createTaskContextProxyIORFile" <<endlog(); 00056 std::ifstream iorfile( iorfilename.c_str() ); 00057 if (iorfile.is_open() && iorfile.good() ) { 00058 std::string ior; 00059 iorfile >> ior; 00060 return ::RTT::corba::TaskContextProxy::Create( ior, true); 00061 } 00062 else { 00063 log(Error) << "Could not open IORFile: '" << iorfilename <<"'."<< endlog(); 00064 return 0; 00065 } 00066 } 00067 00072 RTT::TaskContext* createTaskContextProxyIOR(std::string ior) 00073 { 00074 log(Debug) << "createTaskContextProxyIOR" <<endlog(); 00075 return ::RTT::corba::TaskContextProxy::Create( ior, true); 00076 } 00077 00078 00079 CorbaDeploymentComponent::CorbaDeploymentComponent(const std::string& name, const std::string& siteFile) 00080 : DeploymentComponent(name, siteFile) 00081 { 00082 log(Info) << "Registering TaskContextProxy factory." <<endlog(); 00083 ComponentLoader::Instance()->addFactory("TaskContextProxy", &createTaskContextProxy); 00084 ComponentLoader::Instance()->addFactory("CORBA", &createTaskContextProxy); 00085 ComponentLoader::Instance()->addFactory("IORFile", &createTaskContextProxyIORFile); 00086 ComponentLoader::Instance()->addFactory("IOR", &createTaskContextProxyIOR); 00087 00088 this->addOperation("server", &CorbaDeploymentComponent::createServer, this, ClientThread).doc("Creates a CORBA TaskContext server for the given component").arg("tc", "Name of the RTT::TaskContext (must be a peer).").arg("UseNamingService", "Set to true to use the naming service."); 00089 } 00090 00091 CorbaDeploymentComponent::~CorbaDeploymentComponent() 00092 { 00093 // removes our own server, before removing peer's. 00094 ::RTT::corba::TaskContextServer::CleanupServer(this); 00095 } 00096 00097 bool CorbaDeploymentComponent::createServer(const std::string& tc, bool use_naming) 00098 { 00099 RTT::TaskContext* peer = this->getPeer(tc); 00100 if (!peer) { 00101 log(Error)<<"No such peer: "<< tc <<endlog(); 00102 return false; 00103 } 00104 if ( ::RTT::corba::TaskContextServer::Create(peer, use_naming) != 0 ) 00105 return true; 00106 return false; 00107 } 00108 00109 00110 bool CorbaDeploymentComponent::componentLoaded(RTT::TaskContext* c) 00111 { 00112 if ( dynamic_cast<RTT::corba::TaskContextProxy*>(c) ) { 00113 // is a proxy. 00114 for ( CompList::iterator cit = comps.begin(); cit != comps.end(); ++cit) { 00115 if (cit->second.instance == c) { 00116 cit->second.proxy = true; 00117 return true; 00118 } 00119 } 00120 // impossible: proxy not found 00121 assert(false); 00122 return false; 00123 } 00124 bool use_naming = comps[c->getName()].use_naming; 00125 bool server = comps[c->getName()].server; 00126 log(Info) << "Name:"<< c->getName() << " Server: " << server << " Naming: " << use_naming <<endlog(); 00127 // create a server, use naming. 00128 if (server) 00129 ::RTT::corba::TaskContextServer::Create(c, use_naming); 00130 return true; 00131 } 00132 00133 void CorbaDeploymentComponent::componentUnloaded(TaskContext* c) 00134 { 00135 ::RTT::corba::TaskContextServer::CleanupServer( c ); 00136 } 00137 }