CorbaPort.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
20 #include <rtm/SystemLogger.h>
21 #include <rtm/CorbaPort.h>
22 #include <rtm/CORBA_SeqUtil.h>
23 #include <rtm/NVUtil.h>
24 #include <rtm/Manager.h>
25 #include <string>
26 
27 namespace RTC
28 {
36  CorbaPort::CorbaPort(const char* name)
37  : PortBase(name)
38  {
39  addProperty("port.port_type", "CorbaPort");
40  }
41 
50  {
51  }
52 
61  {
62  RTC_TRACE(("init()"));
63  RTC_PARANOID(("given properties:"));
64  RTC_DEBUG_STR((prop));
65 
66  m_properties << prop;
67 
68  RTC_PARANOID(("updated properties:"));
70 
71  int num(-1);
72  if (!coil::stringTo(num, m_properties.getProperty("connection_limit",
73  "-1").c_str()))
74  {
75  RTC_ERROR(("invalid connection_limit value: %s",
76  m_properties.getProperty("connection_limit").c_str()));
77  }
78 
79  setConnectionLimit(num);
80  }
81 
89  bool
90  CorbaPort::registerProvider(const char* instance_name,
91  const char* type_name,
92  PortableServer::RefCountServantBase& provider)
93  {
94  RTC_TRACE(("registerProvider(instance=%s, type_name=%s)",
95  instance_name, type_name));
96 
97  try
98  {
99  CorbaProviderHolder providerholder(type_name, instance_name, &provider);
100  m_providers.push_back(providerholder);
101  }
102  catch (...)
103  {
104  RTC_ERROR(("appending provider interface failed"));
105  return false;
106  }
107 
108  if (!appendInterface(instance_name, type_name, RTC::PROVIDED))
109  {
110  RTC_ERROR(("appending provider interface failed"));
111  return false;
112  }
113 
114  return true;
115  };
116 
124  bool
125  CorbaPort::registerConsumer(const char* instance_name,
126  const char* type_name,
127  CorbaConsumerBase& consumer)
128  {
129  RTC_TRACE(("registerConsumer()"));
130 
131  if (!appendInterface(instance_name, type_name, RTC::REQUIRED))
132  {
133  return false;
134  }
135 
136  m_consumers.push_back(CorbaConsumerHolder(type_name,
137  instance_name,
138  &consumer));
139 
140  return true;
141  }
142 
143  //============================================================
144  // Local operations
145  //============================================================
146 
155  {
156  CorbaProviderList::iterator it(m_providers.begin());
157  while(it != m_providers.end())
158  {
159  it->activate();
160  ++it;
161  }
162  }
163 
172  {
173  CorbaProviderList::iterator it(m_providers.begin());
174  while(it != m_providers.end())
175  {
176  it->deactivate();
177  ++it;
178  }
179  }
180 
181  //============================================================
182  // protected functions
183  //============================================================
192  CorbaPort::publishInterfaces(ConnectorProfile& connector_profile)
193  {
194  RTC_TRACE(("publishInterfaces()"));
195 
196  ReturnCode_t returnvalue = _publishInterfaces();
197  if(returnvalue != RTC::RTC_OK)
198  {
199  return returnvalue;
200  }
201 
202  NVList properties;
203  CorbaProviderList::iterator it(m_providers.begin());
204  while (it != m_providers.end())
205  {
206  //------------------------------------------------------------
207  // new version descriptor
208  // <comp_iname>.port.<port_name>.provided.<type_name>.<instance_name>
209  std::string newdesc((const char*)m_profile.name);
210  newdesc.insert(m_ownerInstanceName.size(), ".port");
211  newdesc += ".provided." + it->descriptor();
213  push_back(properties,
214  NVUtil::newNV(newdesc.c_str(), it->ior().c_str()));
215 
216  //------------------------------------------------------------
217  // old version descriptor
218  // port.<type_name>.<instance_name>
219  std::string olddesc;
220  olddesc += "port." + it->descriptor();
222  push_back(properties,
223  NVUtil::newNV(olddesc.c_str(), it->ior().c_str()));
224  ++it;
225  }
226 
227 #ifdef ORB_IS_RTORB
228  {
229  CORBA::ULong len1(connector_profile.properties.length());
230  CORBA::ULong len2(properties.length());
231  CORBA::ULong len(len1 + len2);
232  connector_profile.properties.length(len);
233 
234  for (CORBA::ULong i = 0; i < len2; ++i)
235  {
236  connector_profile.properties[len1 + i] = properties[i];
237  }
238  }
239 #else // ORB_IS_RTORB
240  CORBA_SeqUtil::push_back_list(connector_profile.properties, properties);
241 #endif
242 
243  RTC_DEBUG_STR((NVUtil::toString(properties)));
244 
245  return RTC::RTC_OK;
246  }
247 
256  CorbaPort::subscribeInterfaces(const ConnectorProfile& connector_profile)
257  {
258  RTC_TRACE(("subscribeInterfaces()"));
259 
260  const NVList& nv(connector_profile.properties);
262 
263  bool strict(false); // default is "best_effort"
264  CORBA::Long index(NVUtil::find_index(nv, "port.connection.strictness"));
265  if (index >= 0)
266  {
267  const char* strictness;
268  nv[index].value >>= strictness;
269  if (std::string("best_effort") == strictness) { strict = false; }
270  else if (std::string("strict") == strictness) { strict = true; }
271  RTC_DEBUG(("Connetion strictness is: %s",
272  strict ? "strict" : "best_effort"))
273  }
274 
275  for (CorbaConsumerList::iterator it(m_consumers.begin());
276  it != m_consumers.end(); ++it)
277  {
278  std::string ior;
279  if (findProvider(nv, *it, ior))
280  {
281  setObject(ior, *it);
282  continue;
283  }
284  if (findProviderOld(nv, *it, ior))
285  {
286  setObject(ior, *it);
287  continue;
288  }
289 
290  // never come here without error
291  // if strict connection option is set, error is returned.
292  if (strict)
293  {
294  RTC_ERROR(("subscribeInterfaces() failed."));
295  return RTC::RTC_ERROR;
296  }
297  }
298 
299  RTC_TRACE(("subscribeInterfaces() successfully finished."));
300 
301  return RTC::RTC_OK;
302  }
303 
311  void
312  CorbaPort::unsubscribeInterfaces(const ConnectorProfile& connector_profile)
313  {
314  RTC_TRACE(("unsubscribeInterfaces()"));
315 
316  const NVList& nv(connector_profile.properties);
318 
319  for (CorbaConsumerList::iterator it(m_consumers.begin());
320  it != m_consumers.end(); ++it)
321  {
322  std::string ior;
323  if (findProvider(nv, *it, ior))
324  {
325  RTC_DEBUG(("Correspoinding consumer found."));
326  releaseObject(ior, *it);
327  continue;
328  }
329  if (findProviderOld(nv, *it, ior))
330  {
331  RTC_DEBUG(("Correspoinding consumer found."));
332  releaseObject(ior, *it);
333  continue;
334  }
335  }
336  }
337 
346  std::string& iorstr)
347  {
348  // new consumer interface descriptor
349  std::string newdesc((const char*)m_profile.name);
350  newdesc.insert(m_ownerInstanceName.size(), ".port");
351  newdesc += ".required." + cons.descriptor();
352 
353  // find a NameValue of the consumer
354  CORBA::Long cons_index(NVUtil::find_index(nv, newdesc.c_str()));
355  if (cons_index < 0) { return false; }
356 
357  const char* provider;
358  if (!(nv[cons_index].value >>= provider))
359  {
360  RTC_WARN(("Cannot extract Provider interface descriptor"));
361  return false;
362  }
363 
364  // find a NameValue of the provider
365  CORBA::Long prov_index(NVUtil::find_index(nv, provider));
366  if (prov_index < 0) { return false; }
367 
368  const char* ior;
369  if (!(nv[prov_index].value >>= ior))
370  {
371  RTC_WARN(("Cannot extract Provider IOR string"));
372  return false;
373  }
374  iorstr = ior;
375  RTC_DEBUG(("interface matched with new descriptor: %s", newdesc.c_str()));
376  return true;
377  }
378 
387  std::string& iorstr)
388  {
389  // old consumer interface descriptor
390  std::string olddesc("port."); olddesc += cons.descriptor();
391 
392  // find a NameValue of the provider same as olddesc
393  CORBA::Long index(NVUtil::find_index(nv, olddesc.c_str()));
394  if (index < 0) { return false; }
395 
396  const char* ior;
397  if (!(nv[index].value >>= ior))
398  {
399  RTC_WARN(("Cannot extract Provider IOR string"));
400  return false;
401  }
402  iorstr = ior;
403  RTC_INFO(("interface matched with old descriptor: %s", olddesc.c_str()));
404  return true;
405  }
406 
414  bool CorbaPort::setObject(const std::string& ior, CorbaConsumerHolder& cons)
415  {
416  // if ior string is "null" or "nil", ignore it.
417  if (std::string("null") == ior) { return true; }
418  if (std::string("nil") == ior) { return true; }
419  // IOR should be started by "IOR:"
420  if (std::string("IOR:").compare(0, 4, ior.c_str(), 4) != 0)
421  {
422  return false;
423  }
424 
425  // set IOR to the consumer
426  if (!cons.setObject(ior.c_str()))
427  {
428  RTC_ERROR(("Cannot narrow reference"));
429  return false;
430  }
431  RTC_TRACE(("setObject() done"));
432  return true;
433  }
434 
442  bool CorbaPort::releaseObject(const std::string& ior,
443  CorbaConsumerHolder& cons)
444  {
445  if (ior == cons.getIor())
446  {
447  cons.releaseObject();
448  RTC_DEBUG(("Consumer %s released.", cons.descriptor().c_str()));
449  return true;
450  }
451  RTC_WARN(("IORs between Consumer and Connector are different."));
452  return false;
453  }
454 
455 };
SDOPackage::NameValue newNV(const char *name, Value value)
Create NameValue.
Definition: NVUtil.h:79
#define RTC_ERROR(fmt)
Error log output macro.
Definition: SystemLogger.h:422
RT-Component.
virtual ReturnCode_t publishInterfaces(ConnectorProfile &connector_profile)
Publish information about interfaces.
Definition: CorbaPort.cpp:192
coil::Properties m_properties
Properties.
Definition: CorbaPort.h:1070
std::string m_ownerInstanceName
Instance name.
Definition: PortBase.h:2098
CorbaPort(const char *name)
Constructor.
Definition: CorbaPort.cpp:36
bool stringTo(To &val, const char *str)
Convert the given std::string to object.
Definition: stringutil.h:597
RT component logger class.
ReturnCode_t
Definition: doil.h:53
std::vector< std::pair< std::string, std::string > > NVList
Definition: IRTC.h:67
void init(coil::Properties &prop)
Initializing properties.
Definition: CorbaPort.cpp:60
CorbaProviderList m_providers
Definition: CorbaPort.h:1308
CorbaPort class.
#define RTC_WARN(fmt)
Warning log output macro.
Definition: SystemLogger.h:444
virtual ~CorbaPort(void)
Virtual destructor.
Definition: CorbaPort.cpp:49
RTComponent manager class.
#define RTC_DEBUG_STR(str)
Definition: SystemLogger.h:489
bool setObject(const char *ior)
Definition: CorbaPort.h:1333
#define RTC_PARANOID(fmt)
Paranoid level log output macro.
Definition: SystemLogger.h:555
const CORBA::Long find_index(const SDOPackage::NVList &nv, const char *name)
Return the index of element specified by name from NVList.
Definition: NVUtil.cpp:227
The structure to be stored Consumer information.
Definition: CorbaPort.h:1317
#define RTC_DEBUG(fmt)
Debug level log output macro.
Definition: SystemLogger.h:488
std::string toString(const SDOPackage::NVList &nv, const char *name)
Get NVList of specifid name as string.
Definition: NVUtil.cpp:282
#define RTC_TRACE(fmt)
list index
Definition: rtimages.py:10
NameValue and NVList utility functions.
CORBA sequence utility template functions.
The structure to be stored Provider information.
Definition: CorbaPort.h:1217
const std::string & getIor()
Definition: CorbaPort.h:1349
virtual void unsubscribeInterfaces(const ConnectorProfile &connector_profile)
Unsubscribe interfaces.
Definition: CorbaPort.cpp:312
virtual void deactivateInterfaces()
Deactivate all Port interfaces.
Definition: CorbaPort.cpp:171
prop
Organization::get_organization_property ();.
bool setObject(const std::string &ior, CorbaConsumerHolder &cons)
Setting IOR to Consumer.
Definition: CorbaPort.cpp:414
PortProfile m_profile
PortProfile of the Port.
Definition: PortBase.h:2070
virtual ReturnCode_t subscribeInterfaces(const ConnectorProfile &connector_profile)
Subscribe to interface.
Definition: CorbaPort.cpp:256
Class represents a set of properties.
Definition: Properties.h:101
bool appendInterface(const char *name, const char *type_name, PortInterfacePolarity pol)
Append an interface to the PortInterfaceProfile.
Definition: PortBase.cpp:841
virtual bool findProvider(const NVList &nv, CorbaConsumerHolder &cons, std::string &iorstr)
Find out a provider corresponding to the consumer from NVList.
Definition: CorbaPort.cpp:345
#define RTC_INFO(fmt)
Information level log output macro.
Definition: SystemLogger.h:466
void push_back_list(CorbaSequence &seq1, const CorbaSequence &seq2)
Merge the elements of the CORBA sequence.
void push_back(CorbaSequence &seq, SequenceElement elem)
Push the new element back to the CORBA sequence.
CorbaConsumerList m_consumers
Definition: CorbaPort.h:1360
virtual bool findProviderOld(const NVList &nv, CorbaConsumerHolder &cons, std::string &iorstr)
Find out a provider corresponding to the consumer from NVList.
Definition: CorbaPort.cpp:386
bool releaseObject(const std::string &ior, CorbaConsumerHolder &cons)
Releasing Consumer Object.
Definition: CorbaPort.cpp:442
void addProperty(const char *key, ValueType value)
Add NameValue data to PortProfile&#39;s properties.
Definition: PortBase.h:1876
bool registerConsumer(const char *instance_name, const char *type_name, CorbaConsumerBase &consumer)
Register the consumer.
Definition: CorbaPort.cpp:125
virtual ReturnCode_t _publishInterfaces(void)
Publish interface information.
Definition: PortBase.cpp:315
virtual void activateInterfaces()
Activate all Port interfaces.
Definition: CorbaPort.cpp:154
virtual void setConnectionLimit(int limit_value)
Set the maximum number of connections.
Definition: PortBase.cpp:700
const std::string & getProperty(const std::string &key) const
Search for the property with the specified key in this property.
Definition: Properties.cpp:156
bool registerProvider(const char *instance_name, const char *type_name, PortableServer::RefCountServantBase &provider)
Register the provider.
Definition: CorbaPort.cpp:90


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