NamingManager.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
20 #include <functional>
21 #include <algorithm>
22 #include <iostream>
23 
24 #include <coil/Routing.h>
25 #include <coil/stringutil.h>
26 
27 #include <rtm/NamingManager.h>
28 #include <rtm/Manager.h>
29 #include <rtm/CORBA_IORUtil.h>
30 
31 namespace RTC
32 {
40  NamingOnCorba::NamingOnCorba(CORBA::ORB_ptr orb, const char* names)
41  : m_cosnaming(orb, names), m_endpoint(""),
42  m_replaceEndpoint(false)
43  {
44  rtclog.setName("NamingOnCorba");
45  coil::Properties& prop(Manager::instance().getConfig());
47  coil::toBool(prop["corba.nameservice.replace_endpoint"].c_str(),
48  "YES", "NO", true);
49 
50 
51  coil::vstring host_port(coil::split(names, ":"));
52  if (coil::dest_to_endpoint(host_port[0], m_endpoint))
53  {
54  RTC_INFO(("Endpoint for the CORBA naming service (%s) is %s.",
55  host_port[0].c_str(),
56  m_endpoint.c_str()));
57  }
58  else
59  {
60  RTC_WARN(("No endpoint for the CORBA naming service (%s) was found.",
61  host_port[0].c_str()));
62  }
63  }
71  void NamingOnCorba::bindObject(const char* name,
72  const RTObject_impl* rtobj)
73  {
74  RTC_TRACE(("bindObject(name = %s, rtobj)", name));
75 #ifdef ORB_IS_OMNIORB
76  if (!m_endpoint.empty() && m_replaceEndpoint)
77  {
78  CORBA::Object_var obj(RTObject::_duplicate(rtobj->getObjRef()));
79  CORBA::String_var ior;
80  ior = RTC::Manager::instance().getORB()->object_to_string(obj.in());
81  std::string iorstr((const char*)ior);
82 
83  RTC_DEBUG(("Original IOR information:\n %s",
84  CORBA_IORUtil::formatIORinfo(iorstr.c_str()).c_str()));
86  CORBA::Object_var newobj = RTC::Manager::instance().
87  getORB()->string_to_object(iorstr.c_str());
88 
89  RTC_DEBUG(("Modified IOR information:\n %s",
90  CORBA_IORUtil::formatIORinfo(iorstr.c_str()).c_str()));
91  m_cosnaming.rebindByString(name, newobj.in(), true);
92  }
93  else
94  {
95 #endif // ORB_IS_OMNIORB
96  m_cosnaming.rebindByString(name, rtobj->getObjRef(), true);
97 #ifdef ORB_IS_OMNIORB
98  }
99 #endif // ORB_IS_OMNIORB
100  }
101 
102  void NamingOnCorba::bindObject(const char* name,
103  const RTM::ManagerServant* mgr)
104  {
105  RTC_TRACE(("bindObject(name = %s, mgr)", name));
106 #ifdef ORB_IS_OMNIORB
107  if (!m_endpoint.empty() && m_replaceEndpoint)
108  {
109  CORBA::Object_var obj(RTM::Manager::_duplicate(mgr->getObjRef()));
110  CORBA::String_var ior;
111  ior = RTC::Manager::instance().getORB()->object_to_string(obj.in());
112  std::string iorstr((const char*)ior);
113 
114  RTC_DEBUG(("Original IOR information:\n %s",
115  CORBA_IORUtil::formatIORinfo(iorstr.c_str()).c_str()));
117  CORBA::Object_var newobj = RTC::Manager::instance().
118  getORB()->string_to_object(iorstr.c_str());
119 
120  RTC_DEBUG(("Modified IOR information]\n %s",
121  CORBA_IORUtil::formatIORinfo(iorstr.c_str()).c_str()));
122  m_cosnaming.rebindByString(name, newobj.in(), true);
123  }
124  else
125  {
126 #endif // ORB_IS_OMNIORB
127  m_cosnaming.rebindByString(name, mgr->getObjRef(), true);
128 #ifdef ORB_IS_OMNIORB
129  }
130 #endif // ORB_IS_OMNIORB
131  }
132 
140  void NamingOnCorba::unbindObject(const char* name)
141  {
142  RTC_TRACE(("unbindObject(name = %s)", name));
143  m_cosnaming.unbind(name);
144  }
145 
147  {
148  RTC_TRACE(("isAlive()"));
149  return m_cosnaming.isAlive();
150  }
151 
152 
153  //============================================================
154  // NamingManager
155  //============================================================
164  :m_manager(manager), rtclog("NamingManager")
165  {
166  }
167 
176  {
177  }
178 
186  void NamingManager::registerNameServer(const char* method,
187  const char* name_server)
188  {
189  RTC_TRACE(("NamingManager::registerNameServer(%s, %s)",
190  method, name_server));
191  NamingBase* name;
192  name = createNamingObj(method, name_server);
193 
194  Guard guard(m_namesMutex);
195  m_names.push_back(new Names(method, name_server, name));
196  }
197 
205  void NamingManager::bindObject(const char* name,
206  const RTObject_impl* rtobj)
207  {
208  RTC_TRACE(("NamingManager::bindObject(%s)", name));
209 
210  Guard guard(m_namesMutex);
211  for (int i(0), len(m_names.size()); i < len; ++i)
212  {
213  if (m_names[i]->ns != 0)
214  {
215  try
216  {
217  m_names[i]->ns->bindObject(name, rtobj);
218  }
219  catch (...)
220  {
221  delete m_names[i]->ns;
222  m_names[i]->ns = 0;
223  }
224  }
225  }
226  registerCompName(name, rtobj);
227  }
228  void NamingManager::bindObject(const char* name,
229  const RTM::ManagerServant* mgr)
230  {
231  RTC_TRACE(("NamingManager::bindObject(%s)", name));
232 
233  Guard guard(m_namesMutex);
234  for (int i(0), len(m_names.size()); i < len; ++i)
235  {
236  if (m_names[i]->ns != 0)
237  {
238  try
239  {
240  m_names[i]->ns->bindObject(name, mgr);
241  }
242  catch (...)
243  {
244  delete m_names[i]->ns;
245  m_names[i]->ns = 0;
246  }
247  }
248  }
249  registerMgrName(name, mgr);
250  }
251 
260  {
261  RTC_TRACE(("NamingManager::update()"));
262 
263  Guard guard(m_namesMutex);
264  bool rebind(coil::toBool(m_manager->getConfig()["naming.update.rebind"],
265  "YES", "NO", false));
266  for (int i(0), len(m_names.size()); i < len; ++i)
267  {
268  if (m_names[i]->ns == 0) // if ns==NULL
269  {
270  RTC_DEBUG(("Retrying connection to %s/%s",
271  m_names[i]->method.c_str(),
272  m_names[i]->nsname.c_str()));
274  }
275  else
276  {
277  try
278  {
279  if (rebind) { bindCompsTo(m_names[i]->ns); }
280  if (!m_names[i]->ns->isAlive())
281  {
282  RTC_INFO(("Name server: %s (%s) disappeared.",
283  m_names[i]->nsname.c_str(),
284  m_names[i]->method.c_str()));
285  delete m_names[i]->ns;
286  m_names[i]->ns = 0;
287  }
288  }
289  catch (...)
290  {
291  RTC_INFO(("Name server: %s (%s) disappeared.",
292  m_names[i]->nsname.c_str(),
293  m_names[i]->method.c_str()));
294  delete m_names[i]->ns;
295  m_names[i]->ns = 0;
296  }
297  }
298  }
299  }
300 
308  void NamingManager::unbindObject(const char* name)
309  {
310  RTC_TRACE(("NamingManager::unbindObject(%s)", name));
311 
312  Guard guard(m_namesMutex);
313  for (int i(0), len(m_names.size()); i < len; ++i)
314  {
315  if (m_names[i]->ns != NULL)
316  {
317  m_names[i]->ns->unbindObject(name);
318  }
319  }
320  unregisterCompName(name);
321  unregisterMgrName(name);
322  }
323 
332  {
333  RTC_TRACE(("NamingManager::unbindAll(): %d names.", m_compNames.size()));
334  {
335  Guard guard(m_compNamesMutex);
336  coil::vstring names;
337  // unbindObject modifiy m_compNames
338  for (int i(0), len(m_compNames.size()); i < len; ++i)
339  {
340  names.push_back(m_compNames[i]->name);
341  }
342  for (size_t i(0); i < names.size(); ++i)
343  {
344  unbindObject(names[i].c_str());
345  }
346 
347  }
348  {
349  Guard guard(m_mgrNamesMutex);
350  coil::vstring names;
351  // unbindObject modifiy m_mgrNames
352  for (int i(0), len(m_mgrNames.size()); i < len; ++i)
353  {
354  names.push_back(m_mgrNames[i]->name);
355  }
356  for (size_t i(0); i < names.size(); ++i)
357  {
358  unbindObject(names[i].c_str());
359  }
360  }
361  }
362 
370  std::vector<RTObject_impl*> NamingManager::getObjects()
371  {
372  std::vector<RTObject_impl*> comps;
373  Guard guard(m_compNamesMutex);
374 
375  for (int i(0), len(m_compNames.size()); i < len; ++i)
376  {
377  comps.push_back(const_cast<RTObject_impl*>(m_compNames[i]->rtobj));
378  }
379  return comps;
380  }
381 
382  //============================================================
383  // Protected
384  //============================================================
393  const char* name_server)
394  {
395  RTC_TRACE(("createNamingObj(method = %s, nameserver = %s",
396  method, name_server));
397  std::string m(method);
398  if (m == "corba")
399  {
400  try
401  {
402  NamingBase* name;
403  CORBA::ORB_var orb;
404  orb = CORBA::ORB::_duplicate(m_manager->getORB());
405  name = new NamingOnCorba(orb.in(), name_server);
406  if (name == NULL) return NULL;
407  RTC_INFO(("NameServer connection succeeded: %s/%s", \
408  method, name_server));
409  return name;
410  }
411  catch (...)
412  {
413  RTC_INFO(("NameServer connection failed: %s/%s", \
414  method, name_server));
415  return NULL;
416  }
417  }
418  return NULL;
419  }
420 
429  {
430  for (int i(0), len(m_compNames.size()); i < len; ++i)
431  {
432  ns->bindObject(m_compNames[i]->name.c_str(), m_compNames[i]->rtobj);
433  }
434  }
435 
443  void NamingManager::registerCompName(const char* name,
444  const RTObject_impl* rtobj)
445  {
446  for (int i(0), len(m_compNames.size()); i < len; ++i)
447  {
448  if (m_compNames[i]->name == name)
449  {
450  m_compNames[i]->rtobj = rtobj;
451  return;
452  }
453  }
454  m_compNames.push_back(new Comps(name, rtobj));
455  return;
456  }
457  void NamingManager::registerMgrName(const char* name,
458  const RTM::ManagerServant* mgr)
459  {
460  for (int i(0), len(m_mgrNames.size()); i < len; ++i)
461  {
462  if (m_mgrNames[i]->name == name)
463  {
464  m_mgrNames[i]->mgr = mgr;
465  return;
466  }
467  }
468  m_mgrNames.push_back(new Mgr(name, mgr));
469  return;
470  }
471 
479  void NamingManager::unregisterCompName(const char* name)
480  {
481  std::vector<Comps*>::iterator it(m_compNames.begin());
482  for (int i(0), len(m_compNames.size()); i < len; ++i, ++it)
483  {
484  if (m_compNames[i]->name == name)
485  {
486  delete m_compNames[i];
487  m_compNames.erase(it);
488  return;
489  }
490  }
491  return;
492  }
493  void NamingManager::unregisterMgrName(const char* name)
494  {
495  std::vector<Mgr*>::iterator it(m_mgrNames.begin());
496  for (int i(0), len(m_mgrNames.size()); i < len; ++i, ++it)
497  {
498  if (m_mgrNames[i]->name == name)
499  {
500  delete m_mgrNames[i];
501  m_mgrNames.erase(it);
502  return;
503  }
504  }
505  return;
506  }
507 
509  {
510  // recreate NamingObj
511  NamingBase* nsobj(0);
512  try
513  {
514  nsobj = createNamingObj(ns->method.c_str(),
515  ns->nsname.c_str());
516  if (nsobj != 0) // if succeed
517  {
518  RTC_INFO(("Connected to a name server: %s/%s",
519  ns->method.c_str(), ns->nsname.c_str()));
520  ns->ns = nsobj;
521  bindCompsTo(nsobj); // rebind all comps to new NS
522  return;
523  }
524  else
525  {
526  RTC_DEBUG(("Name service: %s/%s still not available.",
527  ns->method.c_str(),
528  ns->nsname.c_str()));
529  }
530  }
531  catch (...)
532  {
533  RTC_DEBUG(("Name server: %s/%s disappeared again.",
534  ns->method.c_str(),
535  ns->nsname.c_str()));
536  if (nsobj != 0)
537  {
538  delete ns->ns;
539  ns->ns = 0;
540  }
541  }
542  }
543 }; // namespace RTC
RT-Component.
std::vector< Mgr * > m_mgrNames
ManagerServant list.
std::vector< Comps * > m_compNames
Component list.
Manager * m_manager
Manager object.
Manager CORBA class.
std::vector< RTObject_impl * > getObjects()
Get all bound objects.
bool replaceEndpoint(std::string &iorstr, const std::string &endpoint)
Replace endpoint address in IOR entry.
Mutex m_mgrNamesMutex
Mutex of ManagerServant list.
virtual void bindObject(const char *name, const RTObject_impl *rtobj)=0
Pure virtual function to bind the specified objects to the NamingService.
RT-Component class.
Definition: RTObject.h:89
std::string formatIORinfo(const char *iorstr)
Extracts information from IOR string and returns formatted string.
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
Split string by delimiter.
Definition: stringutil.cpp:341
Manager class.
Definition: Manager.h:80
void bindObject(const char *name, const RTObject_impl *rtobj)
Bind the specified objects to NamingService.
Mutex m_compNamesMutex
Mutex of Component list.
std::string m_endpoint
void setName(const char *name)
Set suffix of date/time string of header.
void registerNameServer(const char *method, const char *name_server)
Regster the NameServer.
CORBA::ORB_ptr getORB()
Get the pointer to ORB.
Definition: Manager.cpp:832
NamingOnCorba(CORBA::ORB_ptr orb, const char *names)
Constructor.
virtual void bindObject(const char *name, const RTObject_impl *rtobj)
Bind the specified CORBA objects to NamingService.
static Manager & instance()
Get instance of the manager.
Definition: Manager.cpp:140
void unregisterMgrName(const char *name)
Unregister the ManagerServants that will be registered to NameServer.
Structure for ManagerServant management.
#define RTC_WARN(fmt)
Warning log output macro.
Definition: SystemLogger.h:444
void registerMgrName(const char *name, const RTM::ManagerServant *mgr)
Configure the ManagerServants that will be registered to NameServer.
RTComponent manager class.
std::vector< std::string > vstring
Definition: stringutil.h:37
void unbind(const CosNaming::Name &name)
Unbind a binding specified by NameComponent.
NamingService management abstract class.
Definition: NamingManager.h:63
coil::Properties & getConfig()
Get the manager configuration.
Definition: Manager.h:305
#define RTC_DEBUG(fmt)
Debug level log output macro.
Definition: SystemLogger.h:488
Mutex m_namesMutex
Mutex of NameServer list.
#define RTC_TRACE(fmt)
NamingBase * createNamingObj(const char *method, const char *name_server)
Create objects for NameServer management.
Structure for NameServer management.
void retryConnection(Names *ns)
void registerCompName(const char *name, const RTObject_impl *rtobj)
Configure the components that will be registered to NameServer.
prop
Organization::get_organization_property ();.
naming Service helper class
CORBA IOR manipulation utility functions.
void bindCompsTo(NamingBase *ns)
Register the configured component to NameServer.
void unregisterCompName(const char *name)
Unregister the components that will be registered to NameServer.
Class represents a set of properties.
Definition: Properties.h:101
Structure for component management.
bool toBool(std::string str, std::string yes, std::string no, bool default_value)
Convert given string into bool value.
Definition: stringutil.cpp:410
void update()
Update information of NamingServer.
virtual ~NamingManager(void)
Destructor.
virtual void unbindObject(const char *name)
Unbind the specified CORBA objects from NamingService.
#define RTC_INFO(fmt)
Information level log output macro.
Definition: SystemLogger.h:466
RTM::Manager_ptr getObjRef() const
Get the reference of Manager.
CorbaNaming m_cosnaming
NamingServer management class for CORBA.
NamingManager(Manager *manager)
Constructor.
std::vector< Names * > m_names
NameServer list.
virtual bool isAlive()
Check if the name service is alive.
void unbindObject(const char *name)
Unbind the specified objects from NamingService.
RTObject_ptr getObjRef() const
[local interface] Get the object reference
Definition: RTObject.cpp:1483
bool dest_to_endpoint(std::string dest_addr, std::string &endpoint)
Getting network interface name from destination address.
void rebindByString(const char *string_name, CORBA::Object_ptr obj, const bool force=1)
Rebind Object.
void unbindAll()
Unbind all objects from NamingService.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:53