00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "CorbaDeploymentComponent.hpp"
00030 #include <rtt/corba/ControlTaskProxy.hpp>
00031 #include <rtt/corba/ControlTaskServer.hpp>
00032 #include <rtt/Method.hpp>
00033 #include "ocl/ComponentLoader.hpp"
00034 #include <fstream>
00035
00036 namespace OCL
00037 {
00038
00043 TaskContext* createControlTaskProxy(std::string name)
00044 {
00045 log(Debug) << "createControlTaskProxy" <<endlog();
00046 return ::RTT::Corba::ControlTaskProxy::Create(name, false);
00047 }
00048
00053 TaskContext* createControlTaskProxyIORFile(std::string iorfilename)
00054 {
00055 log(Debug) << "createControlTaskProxyIORFile" <<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::ControlTaskProxy::Create( ior, true);
00061 }
00062 else {
00063 log(Error) << "Could not open IORFile: '" << iorfilename <<"'."<< endlog();
00064 return 0;
00065 }
00066 }
00067
00072 TaskContext* createControlTaskProxyIOR(std::string ior)
00073 {
00074 log(Debug) << "createControlTaskProxyIOR" <<endlog();
00075 return ::RTT::Corba::ControlTaskProxy::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 ControlTaskProxy factory." <<endlog();
00083 getFactories()["ControlTaskProxy"] = &createControlTaskProxy;
00084 getFactories()["CORBA"] = &createControlTaskProxy;
00085 getFactories()["IORFile"] = &createControlTaskProxyIORFile;
00086 getFactories()["IOR"] = &createControlTaskProxyIOR;
00087
00088 this->methods()->addMethod(method("server",&CorbaDeploymentComponent::createServer,this),
00089 "Creates a CORBA ControlTask server for the given component",
00090 "tc", "Name of the TaskContext (must be a peer).",
00091 "UseNamingService","Set to true to use the naming service.");
00092
00093 this->methods()->addMethod(method("corbaComponent",&CorbaDeploymentComponent::corbaComponent,this),
00094 "Similar to loadComponent, but also makes it a corba server with Naming Service registration.",
00095 "Name", "Name of the new component.",
00096 "Type","The component type (C++ 'namespace::classname'");
00097 }
00098
00099 CorbaDeploymentComponent::~CorbaDeploymentComponent()
00100 {
00101
00102 ::RTT::Corba::ControlTaskServer::CleanupServer(this);
00103 }
00104
00105 bool CorbaDeploymentComponent::corbaComponent(const std::string& name, const std::string& type)
00106 {
00107 TaskContext* peer = this->getPeer(name);
00108 if (peer) {
00109 log(Error)<<"Peer already exists: "<< name << ". Use server(\""<< name <<"\", true) to make an existing peer a CORBA server." <<endlog();
00110 return false;
00111 }
00112 comps[name].use_naming=true;
00113 comps[name].server=true;
00114 return loadComponent(name, type);
00115 }
00116
00117 bool CorbaDeploymentComponent::createServer(const std::string& tc, bool use_naming)
00118 {
00119 TaskContext* peer = this->getPeer(tc);
00120 if (!peer) {
00121 log(Error)<<"No such peer: "<< tc <<endlog();
00122 return false;
00123 }
00124 if ( ::RTT::Corba::ControlTaskServer::Create(peer, use_naming) != 0 )
00125 return true;
00126 return false;
00127 }
00128
00129
00130 bool CorbaDeploymentComponent::componentLoaded(TaskContext* c)
00131 {
00132 if ( dynamic_cast<RTT::Corba::ControlTaskProxy*>(c) ) {
00133
00134 for ( CompList::iterator cit = comps.begin(); cit != comps.end(); ++cit) {
00135 if (cit->second.instance == c) {
00136 cit->second.proxy = true;
00137 return true;
00138 }
00139 }
00140
00141 assert(false);
00142 return false;
00143 }
00144 bool use_naming = comps[c->getName()].use_naming;
00145 bool server = comps[c->getName()].server;
00146 log(Info) << "Name:"<< c->getName() << " Server: " << server << " Naming: " << use_naming <<endlog();
00147
00148 if (server)
00149 ::RTT::Corba::ControlTaskServer::Create(c, use_naming);
00150 return true;
00151 }
00152
00153 void CorbaDeploymentComponent::componentUnloaded(TaskContext* c)
00154 {
00155 ::RTT::Corba::ControlTaskServer::CleanupServer( c );
00156 }
00157 }