CorbaNaming.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
20 #ifdef WIN32
21 #define ACE_HAS_WINSOCK2 0
22 #endif //WIN32
23 
24 #include <assert.h>
25 #include <rtm/CorbaNaming.h>
26 #include <iostream>
27 
28 namespace RTC
29 {
37  CorbaNaming::CorbaNaming(CORBA::ORB_ptr orb)
38  : m_varORB(orb), m_nameServer(""),
39  m_rootContext(CosNaming::NamingContextExt::_nil()),
40  m_blLength(100)
41  {
42  }
43 
51  CorbaNaming::CorbaNaming(CORBA::ORB_ptr orb, const char* name_server)
52  : m_varORB(CORBA::ORB::_duplicate(orb)), m_nameServer(name_server),
53  m_rootContext(CosNaming::NamingContextExt::_nil()),
54  m_blLength(100)
55  {
56  CORBA::Object_var obj;
57  m_nameServer = "corbaloc::" + m_nameServer + "/NameService";
58  try
59  {
60  obj = m_varORB->string_to_object(m_nameServer.c_str());
61  m_rootContext = CosNaming::NamingContextExt::_narrow(obj);
62  if (CORBA::is_nil(m_rootContext)) throw std::bad_alloc();
63  }
64  catch(...)
65  {
66  throw std::bad_alloc();
67  }
68  }
69 
77  void CorbaNaming::init(const char* name_server)
78  {
79  m_nameServer = name_server;
80  m_nameServer = "corbaloc::" + m_nameServer + "/NameService";
81  CORBA::Object_var obj;
82  obj = m_varORB->string_to_object(m_nameServer.c_str());
83  m_rootContext = CosNaming::NamingContextExt::_narrow(obj);
84  if (CORBA::is_nil(m_rootContext)) throw std::bad_alloc();
85  }
86 
88  {
89  try
90  {
91  if (m_rootContext->_non_existent()) { return false; }
92  return true;
93  }
94  catch (...)
95  {
96  return false;
97  }
98  return false;
99  }
100 
108  void CorbaNaming::bind(const CosNaming::Name& name, CORBA::Object_ptr obj,
109  const bool force)
111  {
112  try
113  {
114  m_rootContext->bind(name, obj);
115  }
116  catch (NotFound& e)
117  {
118  force ? bindRecursive(m_rootContext, name, obj) : throw e;
119  }
120  catch (CannotProceed& e)
121  {
122 #ifndef ORB_IS_RTORB
123  force ? bindRecursive(e.cxt, e.rest_of_name, obj) : throw e;
124 #else // ORB_IS_RTORB
125  force ? bindRecursive(e.cxt(), e.rest_of_name(), obj) : throw e;
126 #endif // ORB_IS_RTORB
127  }
128  }
129 
137  void CorbaNaming::bindByString(const char* string_name, CORBA::Object_ptr obj,
138  const bool force)
140  {
141  this->bind(toName(string_name), obj, force);
142  }
143 
151  void CorbaNaming::bindRecursive(CosNaming::NamingContext_ptr context,
152  const CosNaming::Name& name,
153  CORBA::Object_ptr obj)
155  {
156  CORBA::ULong len(name.length());
157  CosNaming::NamingContext_var cxt;
158  cxt = CosNaming::NamingContext::_duplicate(context);
159 
160  for (CORBA::ULong i = 0; i < len; ++i)
161  {
162  if (i == (len - 1))
163  { // this operation may throw AlreadyBound,
164  cxt->bind(subName(name, i, i), obj);
165  return;
166  }
167  else
168  { // If the context is not a NamingContext, CannotProceed is thrown
169  if (isNamingContext(cxt))
170  cxt = bindOrResolveContext(cxt, subName(name, i, i));
171  else
172  throw CannotProceed(cxt, subName(name, i));
173  }
174  }
175  return;
176  }
177 
185  void CorbaNaming::rebind(const CosNaming::Name& name,
186  CORBA::Object_ptr obj,
187  const bool force)
189  {
190  try
191  {
192  m_rootContext->rebind(name, obj);
193  }
194  catch (NotFound& e)
195  {
196  force ? rebindRecursive(m_rootContext, name, obj) : throw e;
197  }
198  catch (CannotProceed& e)
199  {
200 #ifndef ORB_IS_RTORB
201  force ? rebindRecursive(e.cxt, e.rest_of_name, obj) : throw e;
202 #else // ORB_IS_RTORB
203  force ? rebindRecursive(e.cxt(), e.rest_of_name(), obj) : throw e;
204 #endif // ORB_IS_RTORB
205  }
206  }
207 
215  void CorbaNaming::rebindByString(const char* string_name,
216  CORBA::Object_ptr obj,
217  const bool force)
219  {
220  rebind(toName(string_name), obj, force);
221  }
222 
230  void CorbaNaming::rebindRecursive(CosNaming::NamingContext_ptr context,
231  const CosNaming::Name& name,
232  CORBA::Object_ptr obj)
234  {
235  CORBA::ULong len(name.length());
236  CosNaming::NamingContext_var cxt;
237  cxt = CosNaming::NamingContext::_duplicate(context);
238 
239  for (CORBA::ULong i = 0; i < len; ++i)
240  {
241  if (i == (len - 1))
242  {
243  cxt->rebind(subName(name, i, i), obj);
244  return;
245  }
246  else
247  { // If the context is not a NamingContext, CannotProceed is thrown
248  if (isNamingContext(cxt))
249  {
250  try
251  {
252  cxt = cxt->bind_new_context(subName(name, i, i));
253  }
254  catch (AlreadyBound& e)
255  {
256  (void)(e);
257  cxt = CosNaming::
258  NamingContextExt::
259  _narrow(cxt->resolve(subName(name, i, i)));
260  }
261  }
262  else
263  throw CannotProceed(cxt, subName(name, i));
264  }
265  }
266  return;
267  }
268 
276  void CorbaNaming::bindContext(const CosNaming::Name& name,
277  CosNaming::NamingContext_ptr name_cxt,
278  const bool force)
280  {
281  bind(name, name_cxt, force);
282  }
283 
291  void CorbaNaming::bindContext(const char* string_name,
292  CosNaming::NamingContext_ptr name_cxt,
293  const bool force)
295  {
296  bindContext(toName(string_name), name_cxt, force);
297  }
298 
306  void
307  CorbaNaming::bindContextRecursive(CosNaming::NamingContext_ptr context,
308  const CosNaming::Name& name,
309  CosNaming::NamingContext_ptr name_cxt)
310  {
311  bindRecursive(context, name, name_cxt);
312  return;
313  }
314 
322  void CorbaNaming::rebindContext(const CosNaming::Name& name,
323  CosNaming::NamingContext_ptr name_cxt,
324  const bool force)
326  {
327  rebind(name, name_cxt, force);
328  return;
329  }
330 
338  void CorbaNaming::rebindContext(const char* string_name,
339  CosNaming::NamingContext_ptr name_cxt,
340  const bool force)
342  {
343  rebindContext(toName(string_name), name_cxt, force);
344  }
345 
353  void
354  CorbaNaming::rebindContextRecursive(CosNaming::NamingContext_ptr context,
355  const CosNaming::Name& name,
356  CosNaming::NamingContext_ptr name_cxt)
357  {
358  rebindRecursive(context, name, name_cxt);
359  return;
360  }
361 
369  CORBA::Object_ptr CorbaNaming::resolve(const CosNaming::Name& name)
371  {
372  return m_rootContext->resolve(name);
373  }
374 
382  CORBA::Object_ptr CorbaNaming::resolve(const char* string_name)
384  {
385  return resolve(toName(string_name));
386  }
387 
395  void CorbaNaming::unbind(const CosNaming::Name& name)
397  {
398  m_rootContext->unbind(name);
399  }
400 
408  void CorbaNaming::unbind(const char* string_name)
410  {
411  unbind(toName(string_name));
412  }
413 
421  CosNaming::NamingContext_ptr CorbaNaming::newContext()
422  {
423  return m_rootContext->new_context();
424  }
425 
433  CosNaming::NamingContext_ptr
434  CorbaNaming::bindNewContext(const CosNaming::Name& name, bool force)
436  {
437  try
438  {
439  return m_rootContext->bind_new_context(name);
440  }
441  catch (NotFound& e)
442  {
443  force ? bindRecursive(m_rootContext, name, newContext()) : throw e;
444  }
445  catch (CannotProceed& e)
446  {
447 #ifndef ORB_IS_RTORB
448  force ? bindRecursive(e.cxt, e.rest_of_name, newContext()) : throw e;
449 #else // ORB_IS_RTORB
450  force ?
451  bindRecursive(e.cxt(), e.rest_of_name(), newContext()) : throw e;
452 #endif // ORB_IS_RTORB
453  }
454  return CosNaming::NamingContext::_nil();
455  }
456 
464  CosNaming::NamingContext_ptr
465  CorbaNaming::bindNewContext(const char* string_name, bool force)
467  {
468  return bindNewContext(toName(string_name));
469  }
470 
478  void CorbaNaming::destroy(CosNaming::NamingContext_ptr context)
479  throw (SystemException, NotEmpty)
480  {
481  context->destroy();
482  }
483 
491  void CorbaNaming::destroyRecursive(CosNaming::NamingContext_ptr context)
493  {
494  CosNaming::BindingList_var bl;
495  CosNaming::BindingIterator_var bi;
496  CORBA::Boolean cont(true);
497 
498 #ifndef ORB_IS_RTORB
499  context->list(m_blLength, bl.out(), bi.out());
500 #else // ORB_IS_RTORB
501  // context->list(m_blLength, bl, bi);
502  context->list(m_blLength, (CosNaming::BindingList_out)bl,
503  (CosNaming::BindingIterator_ptr)bi);
504 #endif // ORB_IS_RTORB
505 
506  while (cont)
507  {
508  CORBA::ULong len(bl->length());
509 
510  for (CORBA::ULong i = 0; i < len; ++i)
511  {
512  if (bl[i].binding_type == CosNaming::ncontext)
513  { // If Object is context, destroy recursive.
514  CosNaming::NamingContext_var next_context;
515  next_context = CosNaming::NamingContext::
516  _narrow(context->resolve(bl[i].binding_name));
517 
518  // Recursive function call
519  destroyRecursive(next_context); // +++ Recursive call +++
520  context->unbind(bl[i].binding_name);
521  next_context->destroy();
522  }
523  else if (bl[i].binding_type == CosNaming::nobject)
524  { // If Object is object, unbind it.
525  context->unbind(bl[i].binding_name);
526  }
527  else assert(0); // never comes here
528  }
529 
530  // no more binding -> do-while loop will be finished
531  if (CORBA::is_nil(bi)) cont = false;
532  else bi->next_n(m_blLength, bl);
533  }
534 
535  if (!CORBA::is_nil(bi)) bi->destroy();
536  return;
537  }
538 
547  {
549  }
550 
558  void CorbaNaming::list(CosNaming::NamingContext_ptr name_cxt,
559  CORBA::ULong how_many,
560  CosNaming::BindingList_var& bl,
561  CosNaming::BindingIterator_var& bi)
562  {
563 #ifndef ORB_IS_RTORB
564  name_cxt->list(how_many, bl.out(), bi.out());
565 #else // ORB_IS_RTORB
566  name_cxt->list(how_many, (CosNaming::BindingList_out)bl,
567  (CosNaming::BindingIterator_ptr)bi);
568 #endif // ORB_IS_RTORB
569 
570  }
571 
579  char* CorbaNaming::toString(const CosNaming::Name& name)
581  {
582  if (name.length() == 0)
583  throw InvalidName();
584 
585  CORBA::ULong slen = 0;
586  slen = getNameLength(name);
587 
588  CORBA::String_var string_name = CORBA::string_alloc(slen);
589  nameToString(name, (char*)string_name, slen);
590 
591  return string_name._retn();
592  }
593 
601  CosNaming::Name CorbaNaming::toName(const char* sname)
603  {
604  if (!sname) throw InvalidName();
605  if (*sname == '\0') throw InvalidName();
606 
607  std::string string_name(sname);
608  std::vector<std::string> name_comps;
609 
610  // String name should include 1 or more names
611  CORBA::ULong nc_length = 0;
612  nc_length = split(string_name, std::string("/"), name_comps);
613  if (!(nc_length > 0)) throw InvalidName();
614 
615  // Name components are allocated
616  CosNaming::Name_var name = new CosNaming::Name();
617  name->length(nc_length);
618 
619  // Insert id and kind to name components
620  for (CORBA::ULong i = 0; i < nc_length; ++i)
621  {
622  std::string::size_type pos;
623  pos = name_comps[i].find_last_of(".");
624  if (pos != name_comps[i].npos)
625  {
626  name[i].id =
627  CORBA::string_dup(name_comps[i].substr(0, pos).c_str());
628  name[i].kind =
629  CORBA::string_dup(name_comps[i].substr(pos + 1).c_str());
630  }
631  else
632  {
633  name[i].id = CORBA::string_dup(name_comps[i].c_str());
634 #ifndef ORB_IS_RTORB
635  name[i].kind = "";
636 #else // ORB_IS_RTORB
637  name[i].kind = (char*)"";
638 #endif // ORB_IS_RTORB
639  }
640  }
641  return name;
642  }
643 
651  char* CorbaNaming::toUrl(char* addr, char* string_name)
653  {
654  return m_rootContext->to_url(addr, string_name);
655  }
656 
664  CORBA::Object_ptr CorbaNaming::resolveStr(const char* string_name)
666  {
667  return resolve(string_name);
668  }
669 
670  //======================================================================
671  // Util functions
672  //======================================================================
680  CORBA::Object_ptr
681  CorbaNaming::bindOrResolve(CosNaming::NamingContext_ptr context,
682  const CosNaming::Name& name,
683  CORBA::Object_ptr obj)
684  {
685  try
686  {
687  context->bind(name, obj);
688  return obj;
689  }
690  catch (AlreadyBound& e)
691  {
692  (void)(e);
693  return context->resolve(name);
694  }
695  return CORBA::Object::_nil();
696  }
697 
705  CosNaming::NamingContext_ptr
706  CorbaNaming::bindOrResolveContext(CosNaming::NamingContext_ptr context,
707  const CosNaming::Name& name,
708  CosNaming::NamingContext_ptr new_context)
709  {
710  return CosNaming::NamingContext
711  ::_narrow(bindOrResolve(context, name, new_context));
712  }
713 
721  CosNaming::NamingContext_ptr
722  CorbaNaming::bindOrResolveContext(CosNaming::NamingContext_ptr context,
723  const CosNaming::Name& name)
724  {
725  return bindOrResolveContext(context, name, newContext());
726  }
727 
736  {
737  return m_nameServer.c_str();
738  }
739 
747  CosNaming::NamingContext_ptr CorbaNaming::getRootContext()
748  {
749  return m_rootContext;
750  }
751 
759  bool CorbaNaming::isNamingContext(CORBA::Object_ptr obj)
760  {
761  CosNaming::NamingContext_var nc;
762  nc = CosNaming::NamingContext::_narrow(obj);
763  return CORBA::is_nil(nc) ? false : true;
764  }
765 
773  bool CorbaNaming::isNamingContext(const CosNaming::Name& name)
774  {
775  return isNamingContext(resolve(name));
776  }
777 
785  bool CorbaNaming::isNamingContext(const char* string_name)
786  {
787  return isNamingContext(resolve(string_name));
788  }
789 
797  CosNaming::Name CorbaNaming::subName(const CosNaming::Name& name,
798  CORBA::Long begin,
799  CORBA::Long end)
800  {
801  if (end < 0) end = name.length() - 1;
802 
803  CosNaming::Name sub_name;
804  CORBA::ULong sub_len(end - (begin - 1));
805  if (sub_len > 0)
806  {
807  sub_name.length(sub_len);
808  }
809  else
810  {
811  sub_name.length(0);
812  return sub_name;
813  }
814 
815  for (CORBA::ULong i = 0; i < sub_len; ++i)
816  {
817  sub_name[i] = name[begin + i];
818  }
819  return sub_name;
820  }
821 
822  //------------------------------------------------------------
823  // Protected member functions
824  //------------------------------------------------------------
832  void CorbaNaming::nameToString(const CosNaming::Name& name,
833  char* string_name,
834  CORBA::ULong slen)
835  {
836  char* s = string_name;
837  for (CORBA::ULong i = 0; i < name.length(); ++i)
838  {
839  // Copy id to string_name
840  for (const char* id = name[i].id; *id != '\0'; ++id)
841  {
842  if (*id == '/' || *id == '.' || *id == '\\') *s++ = '\\';
843  *s++ = *id;
844  }
845  // '.' if there is a kind, or no id
846  if (((const char*)(name[i].id ))[0] == '\0' ||
847  ((const char*)(name[i].kind))[0] != '\0')
848  *s++ = '.';
849  // Copy kind to string_name
850  for (const char* kind = name[i].kind; *kind != '\0'; ++kind)
851  {
852  if (*kind == '/' || *kind == '.' || *kind == '\\')
853  *s++ = '\\';
854  *s++ = *kind;
855  }
856  // The end of string_name will be overwritten by '\0'
857  *s++ = '/';
858  }
859  string_name[slen-1] = '\0';
860  }
861 
869  CORBA::ULong CorbaNaming::getNameLength(const CosNaming::Name& name)
870  {
871  CORBA::ULong slen = 0;
872 
873  for (CORBA::ULong i = 0; i < name.length(); ++i)
874  {
875  // Count string length of id(s)
876  for (const char* id = name[i].id; *id; ++id)
877  {
878  // Escape character '/', '.', '\' will convert to "\/", "\.", "\\".
879  if (*id == '/' || *id == '.' || *id == '\\') slen++;
880  slen++;
881  }
882  // If kind exists, space for '.' is counted
883  if (((const char*)(name[i].id ))[0] == '\0' ||
884  ((const char*)(name[i].kind))[0] != '\0')
885  {
886  slen++;
887  }
888  // Count string length of kind(s)
889  for (const char* kind = name[i].kind; *kind; kind++)
890  {
891  if (*kind == '/' || *kind == '.' || *kind == '\\') slen++;
892  slen++;
893  }
894  // Space for '/' or '\0'
895  slen++;
896  }
897  return slen;
898  }
899 
907  unsigned int CorbaNaming::split(const std::string& input,
908  const std::string& delimiter,
909  std::vector<std::string>& results)
910  {
911  typedef std::string::size_type size;
912  size delim_size = delimiter.size();
913  size found_pos(0), begin_pos(0), pre_pos(0), substr_size(0);
914 
915  if (input.substr(0, delim_size) == delimiter)
916  begin_pos = pre_pos = delim_size;
917 
918  while (1)
919  {
920  REFIND:
921  found_pos = input.find(delimiter, begin_pos);
922  if (found_pos == std::string::npos)
923  {
924  results.push_back(input.substr(pre_pos));
925  break;
926  }
927  if ('\\' == input.at(found_pos - 1))
928  {
929  begin_pos = found_pos + delim_size;
930  goto REFIND;
931  }
932 
933  substr_size = found_pos - pre_pos;
934 
935  if (substr_size > 0)
936  {
937  results.push_back(input.substr(pre_pos, substr_size));
938  }
939  begin_pos = found_pos + delim_size;
940  pre_pos = found_pos + delim_size;
941  }
942  return results.size();
943  }
944 }; // namespace RTC
bool isNamingContext(CORBA::Object_ptr obj)
Determine whether the object is NamingContext.
CosNaming::NamingContext::CannotProceed CannotProceed
Definition: CorbaNaming.h:166
void destroyRecursive(CosNaming::NamingContext_ptr context)
Destroy the naming context recursively.
RT-Component.
CORBA::Object_ptr resolveStr(const char *string_name)
Resolve from name of string representation and get object.
void rebindRecursive(CosNaming::NamingContext_ptr context, const CosNaming::Name &name, CORBA::Object_ptr obj)
Bind intermediate context recursively and rebind object.
void nameToString(const CosNaming::Name &name, char *string_name, CORBA::ULong slen)
Get string representation of name component.
CORBA::SystemException SystemException
Definition: CorbaNaming.h:164
CosNaming::NamingContext_ptr bindNewContext(const CosNaming::Name &name, bool force=true)
Bind new NamingContext.
CORBA::ORB_var m_varORB
ORB.
Definition: CorbaNaming.h:1596
const char * getNameServer()
Get the name of name server.
void rebind(const CosNaming::Name &name, CORBA::Object_ptr obj, const bool force=1)
Rebind object.
unsigned int split(const std::string &input, const std::string &delimiter, std::vector< std::string > &results)
Split of string.
CosNaming::NamingContext_ptr getRootContext()
Get the root context.
CORBA::Object_ptr resolve(const CosNaming::Name &name)
Return object bound on the specified NameComponent.
std::string m_nameServer
Name of the name server.
Definition: CorbaNaming.h:1605
CorbaNaming(CORBA::ORB_ptr orb)
Consructor.
Definition: CorbaNaming.cpp:37
void bindContextRecursive(CosNaming::NamingContext_ptr context, const CosNaming::Name &name, CosNaming::NamingContext_ptr name_cxt)
Bind intermediate context recursively and bind NamingContext.
void bindByString(const char *string_name, CORBA::Object_ptr obj, const bool force=1)
Bind object on specified string name position.
CORBA::ULong m_blLength
Definition: CorbaNaming.h:1616
void list(CosNaming::NamingContext_ptr name_cxt, CORBA::ULong how_many, CosNaming::BindingList_var &bl, CosNaming::BindingIterator_var &bi)
Get Binding of the given NamingContext.
CORBA::ULong getNameLength(const CosNaming::Name &name)
Get string length of the name component&#39;s string representation.
void unbind(const CosNaming::Name &name)
Unbind a binding specified by NameComponent.
CosNaming::Name subName(const CosNaming::Name &name, CORBA::Long begin, CORBA::Long end=-1)
Get subset of given name component.
CosNaming::NamingContext::NotFound NotFound
Definition: CorbaNaming.h:165
CORBA naming service helper class.
void rebindContextRecursive(CosNaming::NamingContext_ptr context, const CosNaming::Name &name, CosNaming::NamingContext_ptr name_cxt)
Rebind intermediate context recursively and rebind NamingContext.
void init(const char *name_server)
Initialize the Naming Service.
Definition: CorbaNaming.cpp:77
void bindContext(const CosNaming::Name &name, CosNaming::NamingContext_ptr name_cxt, const bool force=1)
Bind NamingContext.
CosNaming::NamingContext_ptr newContext()
Create new NamingContext.
void bind(const CosNaming::Name &name, CORBA::Object_ptr obj, const bool force=1)
Bind object on specified name component position.
CosNaming::NamingContextExt_var m_rootContext
The root context of specified name server.
Definition: CorbaNaming.h:1613
CosNaming::NamingContext::NotEmpty NotEmpty
Definition: CorbaNaming.h:169
CosNaming::Name toName(const char *string_name)
Resolve given string representation to NameComponent.
CosNaming::NamingContext::AlreadyBound AlreadyBound
Definition: CorbaNaming.h:168
CosNaming::NamingContext_ptr bindOrResolveContext(CosNaming::NamingContext_ptr context, const CosNaming::Name &name, CosNaming::NamingContext_ptr new_context)
Bind or resolve the given name component.
CosNaming::NamingContextExt::InvalidAddress InvalidAddress
Definition: CorbaNaming.h:170
CosNaming::NamingContext::InvalidName InvalidName
Definition: CorbaNaming.h:167
char * toUrl(char *addr, char *string_name)
Get URL representation from given addr and string_name.
void bindRecursive(CosNaming::NamingContext_ptr context, const CosNaming::Name &name, CORBA::Object_ptr obj)
Bind intermediate context recursively and bind object.
CORBA::Object_ptr bindOrResolve(CosNaming::NamingContext_ptr context, const CosNaming::Name &name, CORBA::Object_ptr obj)
Bind or resolve the given name component.
char * toString(const CosNaming::Name &name)
Get string representation of given NameComponent.
void destroy(CosNaming::NamingContext_ptr context)
Destroy the naming context.
void rebindContext(const CosNaming::Name &name, CosNaming::NamingContext_ptr name_cxt, const bool force=1)
Rebind NamingContext.
void clearAll()
Destroy all bindings.
void rebindByString(const char *string_name, CORBA::Object_ptr obj, const bool force=1)
Rebind Object.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:58