CORBA_IORUtil.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
18 #include <iostream>
19 #include <sstream>
20 #include <rtm/CORBA_IORUtil.h>
21 
22 #define POA_NAME_SEP '\xff'
23 #define TRANSIENT_SUFFIX_SEP '\xfe'
24 #define TRANSIENT_SUFFIX_SIZE 8
25 
26 namespace CORBA_IORUtil
27 {
28 
29 #ifdef ORB_IS_OMNIORB
30  typedef _CORBA_Unbounded_Sequence_Octet OctetUSequence;
31  typedef _CORBA_Unbounded_Sequence_String StringUSequence;
32 #endif
33 
34 #ifndef ORB_IS_RTORB
35  // prototype of static functions
36  static void print_key(std::stringstream& s, OctetUSequence& key);
37 
38  static void print_omni_key(std::stringstream& sstr, OctetUSequence& key);
39 
40  static int get_poa_info(OctetUSequence& key, StringUSequence& poas_out,
41  int& transient_out, OctetUSequence& id_out);
42 
43  static void print_tagged_components(std::stringstream& sstr,
44  IOP::MultipleComponentProfile& comps);
45 #endif // ORB_IS_RTORB
46 
47 
55  bool toIOR(const char* iorstr, IOP::IOR& ior)
56  {
57 #ifndef ORB_IS_RTORB
58  if (iorstr == 0) { return false; }
59  size_t size = strlen(iorstr);
60 
61  if (size < 4)
62  {
63  return false;
64  throw CORBA::MARSHAL(0,CORBA::COMPLETED_NO);
65  }
66 
67  const char* p = iorstr;
68 
69  if (p[0] != 'I' || p[1] != 'O' || p[2] != 'R' || p[3] != ':')
70  {
71  return false;
72  throw CORBA::MARSHAL(0,CORBA::COMPLETED_NO);
73  }
74 
75  // IOR:xxyyzz......
76  // "IOR:" occupies 4 digits.
77  // two digits express one byte, and all byte sequence express IOR profile
78  size = (size - 4) / 2; // how many octets are there in the string
79  p += 4;
80 
81  cdrMemoryStream buf((CORBA::ULong)size, 0);
82  for (int i(0); i < (int)size; ++i)
83  {
84  CORBA::Octet v;
85  // upper digit
86  int j(i * 2);
87  if (p[j] >= '0' && p[j] <= '9') { v = ((p[j] - '0') << 4); }
88  else if (p[j] >= 'a' && p[j] <= 'f') { v = ((p[j] - 'a' + 10) << 4); }
89  else if (p[j] >= 'A' && p[j] <= 'F') { v = ((p[j] - 'A' + 10) << 4); }
90  else { return false; }
91  // lower digit
92  int k(j + 1);
93  if (p[k] >= '0' && p[k] <= '9') { v += (p[k] - '0'); }
94  else if (p[k] >= 'a' && p[k] <= 'f') { v += (p[k] - 'a' + 10); }
95  else if (p[k] >= 'A' && p[k] <= 'F') { v += (p[k] - 'A' + 10); }
96  else { return false; }
97  // push_back to buffer
98  buf.marshalOctet(v);
99  }
100 
101  buf.rewindInputPtr();
102  CORBA::Boolean b = buf.unmarshalBoolean();
103  buf.setByteSwapFlag(b);
104 
105  ior.type_id = IOP::IOR::unmarshaltype_id(buf);
106  ior.profiles <<= buf;
107  return true;
108 #else // ORB_IS_RTORB
109  // RtORB does not supports this function
110  return false;
111 #endif // ORB_IS_RTORB
112  }
113 
121  bool toString(IOP::IOR& ior, std::string& iorstr)
122  {
123 #ifndef ORB_IS_RTORB
124  cdrMemoryStream buf(CORBA::ULong(0),CORBA::Boolean(1));
125  buf.marshalBoolean(omni::myByteOrder);
126  buf.marshalRawString(ior.type_id);
127  ior.profiles >>= buf;
128 
129  // turn the encapsulation into a hex string with "IOR:" prepended
130  buf.rewindInputPtr();
131  size_t s = buf.bufSize();
132  CORBA::Char* data = (CORBA::Char *)buf.bufPtr();
133 
134  char *result = new char[4 + s * 2 + 1];
135  result[4 + s * 2] = '\0';
136  result[0] = 'I';
137  result[1] = 'O';
138  result[2] = 'R';
139  result[3] = ':';
140 
141  for (int i(0); i < (int)s; ++i)
142  {
143  int j = 4 + i * 2;
144  int v = (data[i] & 0xf0);
145 
146  v = v >> 4;
147  if (v < 10)
148  {
149  result[j] = '0' + v;
150  }
151  else
152  {
153  result[j] = 'a' + (v - 10);
154  }
155  v = ((data[i] & 0xf));
156  if (v < 10)
157  {
158  result[j+1] = '0' + v;
159  }
160  else
161  {
162  result[j+1] = 'a' + (v - 10);
163  }
164  }
165  iorstr = result;
166  delete result;
167  return true;
168 #else // ORB_IS_RTORB
169  // RtORB does not this function.
170  return false;
171 #endif // ORB_IS_RTORB
172  }
173 
181  bool replaceEndpoint(std::string& iorstr, const std::string& endpoint)
182  {
183 #ifndef ORB_IS_RTORB
184  try
185  {
186  IOP::IOR ior;
187  toIOR(iorstr.c_str(), ior);
188 
189  for (unsigned long count(0); count < ior.profiles.length(); ++count)
190  {
191 
192  if (ior.profiles[count].tag == IOP::TAG_INTERNET_IOP)
193  {
194  IIOP::ProfileBody pBody;
195  IIOP::unmarshalProfile(ior.profiles[count], pBody);
196  pBody.address.host = endpoint.c_str();
197 
198  IOP::TaggedProfile profile;
199  // profile_data is cctet sequence
200  IIOP::encodeProfile(pBody, profile);
201  CORBA::ULong max = profile.profile_data.maximum();
202  CORBA::ULong len = profile.profile_data.length();
203  CORBA::Octet* buf = profile.profile_data.get_buffer(1);
204  // replace is not standard function
205  ior.profiles[count].profile_data.replace(max, len, buf, 1);
206  }
207  }
208  return toString(ior, iorstr);
209 
210  }
211  catch(...)
212  {
213  return false;
214  }
215 #endif // ORB_IS_RTORB
216  return false;
217  }
218 
226  std::string formatIORinfo(const char* iorstr)
227  {
228  std::stringstream retstr;
229 #ifndef ORB_IS_RTORB
230  IOP::IOR ior;
231  toIOR(iorstr, ior);
232 
233  if (ior.profiles.length() == 0 && strlen(ior.type_id) == 0)
234  {
235  retstr << "IOR is a nil object reference." << std::endl;
236  if (iorstr) { retstr << iorstr << std::endl; }
237  return retstr.str();
238  }
239 
240  retstr << "IOR information" << std::endl;
241  retstr << " Type ID: \"" << (const char*) ior.type_id
242  << "\"" << std::endl;;
243  retstr << " Profiles:" << std::endl;;
244  for (unsigned long count=0; count < ior.profiles.length(); ++count)
245  {
246  retstr << " " << count + 1 << ". ";
247  if (ior.profiles[count].tag == IOP::TAG_INTERNET_IOP)
248  {
249  IIOP::ProfileBody pBody;
250  IIOP::unmarshalProfile(ior.profiles[count], pBody);
251 
252  retstr << "IIOP " << (int) pBody.version.major << "."
253  << (int) pBody.version.minor << " ";
254  retstr << (const char*) pBody.address.host
255  << " " << pBody.address.port << std::endl;
256 
257  print_omni_key(retstr, pBody.object_key);
258  print_key(retstr, pBody.object_key);
259  print_tagged_components(retstr, pBody.components);
260 
261  retstr << std::endl;
262  }
263  else if (ior.profiles[count].tag == IOP::TAG_MULTIPLE_COMPONENTS)
264  {
265 
266  retstr << "Multiple Component Profile ";
267  IIOP::ProfileBody pBody;
268  IIOP::unmarshalMultiComponentProfile(ior.profiles[count],
269  pBody.components);
270  print_tagged_components(retstr, pBody.components);
271 
272  retstr << std::endl;
273 
274  }
275  else
276  {
277  retstr << "Unrecognised profile tag: 0x"
278  << std::hex
279  << (unsigned)(ior.profiles[count].tag)
280  << std::dec
281  << std::endl;
282  }
283  }
284 #else // ORB_IS_RTORB
285  retstr << "RtORB does't support formatIORinfo() function." << std::endl;
286 #endif // ORB_IS_RTORB
287  return retstr.str();
288  }
289 
290 
291 #ifndef ORB_IS_RTORB
292  //------------------------------------------------------------
293  // static functions
294 
295  static void print_key(std::stringstream& sstr, OctetUSequence& key)
296  {
297  // Output key as text
298  sstr << " Object Key: \"";
299  for(unsigned int j = 0; j < key.length(); ++j)
300  {
301  if( (char) key[j] >= ' ' && (char) key[j] <= '~')
302  {
303  sstr << (char) key[j];
304  }
305  else
306  {
307  sstr << ".";
308  }
309  }
310  sstr << "\"";
311 
312  // Output key in hexadecimal form.
313  sstr << " = 0x";
314  for(unsigned int j(0); j < key.length(); ++j)
315  {
316  int v = (key[j] & 0xf0) >> 4;
317  if (v < 10) { sstr << (char)('0' + v); }
318  else { sstr << (char)('a' + (v - 10)); }
319  v = key[j] & 0xf;
320  if (v < 10) { sstr << (char)('0' + v); }
321  else { sstr << (char)('a' + (v - 10)); }
322  }
323  sstr << " (" << key.length() << " bytes)" << std::endl;
324  }
325 
326  static void print_omni_key(std::stringstream& sstr, OctetUSequence& key)
327  {
328  StringUSequence poas;
329  int is_transient;
330  OctetUSequence id;
331 
332  if(get_poa_info(key, poas, is_transient, id))
333  {
334  sstr << " POA(" << (char*)poas[0];
335  for(unsigned i(1); i < poas.length(); ++i)
336  {
337  sstr << '/' << (char*)poas[i];
338  }
339  sstr << ") ";
340  }
341  else
342  {
343  if(key.length() != sizeof(omniOrbBoaKey))
344  {
345  return;
346  }
347  sstr << "BOA ";
348  }
349  print_key(sstr, id);
350  }
351 
352  static int get_poa_info(OctetUSequence& key, StringUSequence& poas_out,
353  int& transient_out, OctetUSequence& id_out)
354  {
355  const char* k = (const char*) key.NP_data();
356  int len = key.length();
357  const char* kend = k + len;
358 
359  poas_out.length(1);
360  poas_out[0] = CORBA::string_dup("root");
361 
362  if(*k != TRANSIENT_SUFFIX_SEP && *k != POA_NAME_SEP) { return 0; }
363 
364  while(k < kend && *k == POA_NAME_SEP)
365  {
366  ++k;
367  const char* name = k;
368 
369  while(k < kend && *k && *k != POA_NAME_SEP
370  && *k != TRANSIENT_SUFFIX_SEP)
371  {
372  ++k;
373  }
374  if(k == kend) { return 0; }
375 
376  char* nm = new char[k - name + 1];
377  memcpy(nm, name, k - name);
378  nm[k - name] = '\0';
379  poas_out.length(poas_out.length() + 1);
380  poas_out[poas_out.length() - 1] = nm;
381  }
382  if(k == kend) { return 0; }
383 
384  transient_out = 0;
385  if(*k == TRANSIENT_SUFFIX_SEP)
386  {
387  transient_out = 1;
388  k += TRANSIENT_SUFFIX_SIZE + 1;
389  }
390  if(k >= kend || *k) { return 0; }
391  k++;
392 
393  id_out.length(kend - k);
394  memcpy(id_out.NP_data(), k, kend - k);
395 
396  return 1;
397  }
398 
399  static void print_tagged_components(std::stringstream& sstr,
400  IOP::MultipleComponentProfile& components)
401  {
402 #if defined(RTM_OMNIORB_40) || defined(RTM_OMNIORB_41)
403  CORBA::ULong total(components.length());
404 
405  for (CORBA::ULong index(0); index < total; ++index)
406  {
407  try
408  {
409  CORBA::String_var content;
410  content = IOP::dumpComponent(components[index]);
411  char* p = content;
412  char* q;
413  do
414  {
415  q = strchr(p,'\n');
416  if (q)
417  {
418  *q++ = '\0';
419  }
420  sstr << " " << (const char*) p << std::endl;
421  p = q;
422  } while (q);
423  }
424  catch (CORBA::MARSHAL& ex)
425  {
426  sstr << " Broken component" << std::endl;
427  }
428  }
429 #endif // defined(RTM_OMNIORB_40) || defined(RTM_OMNIORB_41)
430  }
431 #endif // ORB_IS_RTORB
432 };
#define POA_NAME_SEP
bool toString(IOP::IOR &ior, std::string &iorstr)
Convert from IOR structure to IOR string.
#define TRANSIENT_SUFFIX_SEP
bool replaceEndpoint(std::string &iorstr, const std::string &endpoint)
Replace endpoint address in IOR entry.
static void print_key(std::stringstream &s, OctetUSequence &key)
std::string formatIORinfo(const char *iorstr)
Extracts information from IOR string and returns formatted string.
static void print_tagged_components(std::stringstream &sstr, IOP::MultipleComponentProfile &comps)
bool toIOR(const char *iorstr, IOP::IOR &ior)
Convert from IOR string to IOR structure.
list index
Definition: rtimages.py:10
#define TRANSIENT_SUFFIX_SIZE
CORBA IOR manipulation utility functions.
static int get_poa_info(OctetUSequence &key, StringUSequence &poas_out, int &transient_out, OctetUSequence &id_out)
static void print_omni_key(std::stringstream &sstr, OctetUSequence &key)


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:42