OrocosComponentLibrary  2.8.3
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  }
90 
91  CorbaDeploymentComponent::~CorbaDeploymentComponent()
92  {
93  // removes our own server, before removing peer's.
94  ::RTT::corba::TaskContextServer::CleanupServer(this);
95  }
96 
97  bool CorbaDeploymentComponent::createServer(const std::string& tc, bool use_naming)
98  {
99  RTT::TaskContext* peer = this->getPeer(tc);
100  if (!peer) {
101  log(Error)<<"No such peer: "<< tc <<endlog();
102  return false;
103  }
104  if ( ::RTT::corba::TaskContextServer::Create(peer, use_naming) != 0 )
105  return true;
106  return false;
107  }
108 
109 
111  {
112  if ( dynamic_cast<RTT::corba::TaskContextProxy*>(c) ) {
113  // is a proxy.
114  for ( CompMap::iterator cit = compmap.begin(); cit != compmap.end(); ++cit) {
115  if (cit->second.instance == c) {
116  cit->second.proxy = true;
117  return true;
118  }
119  }
120  // impossible: proxy not found
121  assert(false);
122  return false;
123  }
124  bool use_naming = compmap[c->getName()].use_naming;
125  bool server = compmap[c->getName()].server;
126  log(Info) << "Name:"<< c->getName() << " Server: " << server << " Naming: " << use_naming <<endlog();
127  // create a server, use naming.
128  if (server)
129  ::RTT::corba::TaskContextServer::Create(c, use_naming);
130  return true;
131  }
132 
134  {
135  ::RTT::corba::TaskContextServer::CleanupServer( c );
136  }
137 }
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
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...