OrocosComponentLibrary  2.9.0
CorbaDeploymentComponent.cpp
1 /***************************************************************************
2  tag: Peter Soetens Thu Jul 3 15:34:48 CEST 2008 CorbaDeploymentComponent.cpp
3 
4  CorbaDeploymentComponent.cpp - description
5  -------------------
6  begin : Thu July 03 2008
7  copyright : (C) 2008 Peter Soetens
8  email : peter.soetens@fmtc.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU Lesser General Public *
13  * License as published by the Free Software Foundation; either *
14  * version 2.1 of the License, or (at your option) any later version. *
15  * *
16  * This library is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19  * Lesser General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU Lesser General Public *
22  * License along with this library; if not, write to the Free Software *
23  * Foundation, Inc., 59 Temple Place, *
24  * Suite 330, Boston, MA 02111-1307 USA *
25  * *
26  ***************************************************************************/
27 
28 
29 #include "CorbaDeploymentComponent.hpp"
30 #include <rtt/transports/corba/TaskContextProxy.hpp>
31 #include <rtt/transports/corba/TaskContextServer.hpp>
32 #include <rtt/deployment/ComponentLoader.hpp>
33 #include "ocl/Component.hpp"
34 #include <fstream>
35 
36 namespace OCL
37 {
38 
43  RTT::TaskContext* createTaskContextProxy(std::string name)
44  {
45  log(Debug) << "createTaskContextProxy" <<endlog();
46  return ::RTT::corba::TaskContextProxy::Create(name, false);
47  }
48 
53  RTT::TaskContext* createTaskContextProxyIORFile(std::string iorfilename)
54  {
55  log(Debug) << "createTaskContextProxyIORFile" <<endlog();
56  std::ifstream iorfile( iorfilename.c_str() );
57  if (iorfile.is_open() && iorfile.good() ) {
58  std::string ior;
59  iorfile >> ior;
60  return ::RTT::corba::TaskContextProxy::Create( ior, true);
61  }
62  else {
63  log(Error) << "Could not open IORFile: '" << iorfilename <<"'."<< endlog();
64  return 0;
65  }
66  }
67 
72  RTT::TaskContext* createTaskContextProxyIOR(std::string ior)
73  {
74  log(Debug) << "createTaskContextProxyIOR" <<endlog();
75  return ::RTT::corba::TaskContextProxy::Create( ior, true);
76  }
77 
78 
79 CorbaDeploymentComponent::CorbaDeploymentComponent(const std::string& name, const std::string& siteFile)
80  : DeploymentComponent(name, siteFile)
81  {
82  log(Info) << "Registering TaskContextProxy factory." <<endlog();
83  ComponentLoader::Instance()->addFactory("TaskContextProxy", &createTaskContextProxy);
84  ComponentLoader::Instance()->addFactory("CORBA", &createTaskContextProxy);
85  ComponentLoader::Instance()->addFactory("IORFile", &createTaskContextProxyIORFile);
86  ComponentLoader::Instance()->addFactory("IOR", &createTaskContextProxyIOR);
87 
88  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.");
89  this->addOperation("aliasServer", &CorbaDeploymentComponent::createAliasServer, this, ClientThread).doc("Creates a CORBA TaskContext server for the given component using an alias").arg("tc", "Name of the RTT::TaskContext (must be a peer).").arg("alias", "Alias to use when registering to the CORBA Namingservice").arg("UseNamingService", "Set to true to use the naming service.");
90  }
91 
92  CorbaDeploymentComponent::~CorbaDeploymentComponent()
93  {
94  // removes our own server, before removing peer's.
95  ::RTT::corba::TaskContextServer::CleanupServer(this);
96  }
97 
98  bool CorbaDeploymentComponent::createServer(const std::string& tc, bool use_naming)
99  {
100  RTT::TaskContext* peer = this->getPeer(tc);
101  if (!peer) {
102  log(Error)<<"No such peer: "<< tc <<endlog();
103  return false;
104  }
105  if ( ::RTT::corba::TaskContextServer::Create(peer, use_naming) != 0 )
106  return true;
107  return false;
108  }
109 
110  bool CorbaDeploymentComponent::createAliasServer(const std::string& tc, const std::string& alias, bool use_naming)
111  {
112  RTT::TaskContext* peer = this->getPeer(tc);
113  if (!peer) {
114  log(Error)<<"No such peer: "<< tc <<endlog();
115  return false;
116  }
117  if ( ::RTT::corba::TaskContextServer::Create(peer, alias, use_naming) != 0 )
118  return true;
119  return false;
120  }
121 
123  {
124  if ( dynamic_cast<RTT::corba::TaskContextProxy*>(c) ) {
125  // is a proxy.
126  for ( CompMap::iterator cit = compmap.begin(); cit != compmap.end(); ++cit) {
127  if (cit->second.instance == c) {
128  cit->second.proxy = true;
129  return true;
130  }
131  }
132  // impossible: proxy not found
133  assert(false);
134  return false;
135  }
136  bool use_naming = compmap[c->getName()].use_naming;
137  bool server = compmap[c->getName()].server;
138  log(Info) << "Name:"<< c->getName() << " Server: " << server << " Naming: " << use_naming <<endlog();
139  // create a server, use naming.
140  if (server)
141  ::RTT::corba::TaskContextServer::Create(c, use_naming);
142  return true;
143  }
144 
146  {
147  ::RTT::corba::TaskContextServer::CleanupServer( c );
148  }
149 }
virtual bool componentLoaded(RTT::TaskContext *c)
Check if c is a proxy or a local object.
bool createServer(const std::string &tc, bool use_naming)
Creates a ControlTask CORBA server for a given peer TaskContext.
This file contains the macros and definitions to create dynamically loadable components.
RTT::TaskContext * createTaskContextProxyIOR(std::string ior)
This helper function looks up a server using an IOR file and creates a proxy for that object...
The Orocos Component Library.
Definition: Component.hpp:43
bool createAliasServer(const std::string &tc, const std::string &alias, bool use_naming)
Creates a ControlTask CORBA server for a given peer TaskContext.
RTT::TaskContext * createTaskContextProxyIORFile(std::string iorfilename)
This helper function looks up a server using an IOR file and creates a proxy for that object...
A Component for deploying (configuring) other components in an application.
virtual void componentUnloaded(RTT::TaskContext *c)
Removes the CORBA server for this component.
RTT::TaskContext * createTaskContextProxy(std::string name)
This helper function looks up a server using the Naming Service and creates a proxy for that object...