SdoServiceAdmin.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
19 #include <memory>
20 #include <coil/UUID.h>
21 #include <coil/Guard.h>
22 #include <coil/stringutil.h>
23 #include <rtm/RTObject.h>
24 #include <rtm/CORBA_SeqUtil.h>
25 #include <rtm/SdoServiceAdmin.h>
28 
29 namespace RTC
30 {
32 
40  struct service_id
41  {
42  service_id(const char* id) : m_id(id) {};
43  bool operator()(const SDOPackage::ServiceProfile& s)
44  {
45  std::string id(s.id);
46  return m_id == id;
47  }
48  const std::string m_id;
49  };
50 
51 
60  : m_rtobj(rtobj), m_allConsumerEnabled(true),
61  rtclog("SdoServiceAdmin")
62  {
63  RTC_TRACE(("SdoServiceAdmin::SdoServiceAdmin(%s)",
64  rtobj.getProperties()["instance_name"].c_str()));
65 
67 
68  //------------------------------------------------------------
69  // SDO service provider
70  ::coil::vstring enabledProviderTypes
71  = ::coil::split(prop["sdo.service.provider.enabled_services"], ",", true);
72  RTC_DEBUG(("sdo.service.provider.enabled_services: %s",
73  prop["sdo.service.provider.enabled_services"].c_str()));
74 
75  ::coil::vstring availableProviderTypes
76  = SdoServiceProviderFactory::instance().getIdentifiers();
77  prop["sdo.service.provider.available_services"]
78  = coil::flatten(availableProviderTypes);
79  RTC_DEBUG(("sdo.service.provider.available_services: %s",
80  prop["sdo.service.provider.available_services"].c_str()));
81 
82 
83  // If types include '[Aa][Ll][Ll]', all types enabled in this RTC
84  ::coil::vstring activeProviderTypes;
85  for (size_t i(0); i < enabledProviderTypes.size(); ++i)
86  {
87  std::string tmp(enabledProviderTypes[i]);
88  coil::toLower(tmp);
89  if (tmp == "all")
90  {
91  activeProviderTypes = availableProviderTypes;
92  RTC_DEBUG(("sdo.service.provider.enabled_services: ALL"));
93  break;
94  }
95  for (size_t j(0); j < availableProviderTypes.size(); ++j)
96  {
97  if (availableProviderTypes[j] == enabledProviderTypes[i])
98  {
99  activeProviderTypes.push_back(availableProviderTypes[j]);
100  }
101  }
102  }
103 
105  for (size_t i(0); i < activeProviderTypes.size(); ++i)
106  {
108  = factory.createObject(activeProviderTypes[i]);
109 
110  SDOPackage::ServiceProfile prof;
111  prof.id = CORBA::string_dup(activeProviderTypes[i].c_str());
112  prof.interface_type = CORBA::string_dup(activeProviderTypes[i].c_str());
113  prof.service = svc->_this();
114  std::string propkey = ifrToKey(activeProviderTypes[i]);
115  NVUtil::copyFromProperties(prof.properties,
116  prop.getNode(propkey.c_str()));
117 
118  svc->init(rtobj, prof);
119  m_providers.push_back(svc);
120  }
121 
122  //------------------------------------------------------------
123  // SDO service consumer
124  // getting consumer types from RTC's properties
125 
126  ::std::string constypes = prop["sdo.service.consumer.enabled_services"];
127  m_consumerTypes = ::coil::split(constypes, ",", true);
128  RTC_DEBUG(("sdo.service.consumer.enabled_services: %s", constypes.c_str()));
129 
130  prop["sdo.service.consumer.available_services"]
132  RTC_DEBUG(("sdo.service.consumer.available_services: %s",
133  prop["sdo.service.consumer.available_services"].c_str()));
134 
135  // If types include '[Aa][Ll][Ll]', all types enabled in this RTC
136  for (size_t i(0); i < m_consumerTypes.size(); ++i)
137  {
138  std::string tmp(m_consumerTypes[i]);
139  coil::toLower(tmp);
140  if (tmp == "all")
141  {
142  m_allConsumerEnabled = true;
143  RTC_DEBUG(("sdo.service.consumer.enabled_services: ALL"));
144  }
145  }
146  }
147 
156  {
157  for (size_t i(0); i < m_providers.size(); ++i)
158  {
159  m_providers[i]->finalize();
160  delete m_providers[i];
161  }
162  m_providers.clear();
163 
164  for (size_t i(0); i < m_consumers.size(); ++i)
165  {
166  m_consumers[i]->finalize();
167  delete m_consumers[i];
168  }
169  m_consumers.clear();
170  }
171 
179  SDOPackage::ServiceProfileList* SdoServiceAdmin::getServiceProviderProfiles()
180  {
181  SDOPackage::ServiceProfileList_var prof
182  = new SDOPackage::ServiceProfileList();
183  SDOPackage::ServiceProfileList prof2;
184  Guard guard(m_provider_mutex);
185  for (size_t i(0); i < m_providers.size(); ++i)
186  {
187  CORBA_SeqUtil::push_back(prof2, m_providers[i]->getProfile());
188  }
189  return prof._retn();
190  }
191 
199  SDOPackage::ServiceProfile*
201  {
202  std::string idstr(id);
203  Guard guard(m_provider_mutex);
204  for (size_t i(0); i < m_providers.size(); ++i)
205  {
206  if (idstr == static_cast<const char*>(m_providers[i]->getProfile().id))
207  {
208  return new SDOPackage::ServiceProfile(m_providers[i]->getProfile());
209  }
210  }
211  throw new SDOPackage::InvalidParameter();
212  return new SDOPackage::ServiceProfile();
213  }
214 
222  SDOPackage::SDOService_ptr SdoServiceAdmin::getServiceProvider(const char* id)
223  {
224  SDOPackage::ServiceProfile_var prof;
225  prof = getServiceProviderProfile(id);
226  SDOPackage::SDOService_var sdo
227  = SDOPackage::SDOService::_duplicate(prof->service);
228  return sdo._retn();
229  }
230 
238  bool SdoServiceAdmin::
239  addSdoServiceProvider(const SDOPackage::ServiceProfile& prof,
241  {
242  RTC_TRACE(("SdoServiceAdmin::addSdoServiceProvider(if=%s)",
243  static_cast<const char*>(prof.interface_type)));
244  Guard guard(m_provider_mutex);
245 
246  std::string id(static_cast<const char*>(prof.id));
247  for (size_t i(0); i < m_providers.size(); ++i)
248  {
249  if (id == static_cast<const char*>(m_providers[i]->getProfile().id))
250  {
251  RTC_ERROR(("SDO service(id=%s, ifr=%s) already exists",
252  static_cast<const char*>(prof.id),
253  static_cast<const char*>(prof.interface_type)));
254  return false;
255  }
256  }
257  m_providers.push_back(provider);
258  return true;
259  }
260 
269  {
270  RTC_TRACE(("removeSdoServiceProvider(%d)", id));
271  Guard gurad(m_provider_mutex);
272 
273  std::string strid(id);
274  std::vector<SdoServiceProviderBase*>::iterator it = m_providers.begin();
275  std::vector<SdoServiceProviderBase*>::iterator it_end = m_providers.end();
276  while (it != it_end)
277  {
278  if (strid == static_cast<const char*>((*it)->getProfile().id))
279  {
280  (*it)->finalize();
283  factory.deleteObject(*it);
284  m_providers.erase(it);
285  RTC_INFO(("SDO service provider has been deleted: %s", id));
286  return true;
287  }
288  ++it;
289  }
290  RTC_WARN(("Specified SDO service provider not found: %s", id));
291  return false;
292  }
293 
301  bool SdoServiceAdmin::
302  addSdoServiceConsumer(const SDOPackage::ServiceProfile& sProfile)
303  {
304  Guard guard(m_consumer_mutex);
305  RTC_TRACE(("addSdoServiceConsumer(IFR = %s)",
306  static_cast<const char*>(sProfile.interface_type)));
307 
308  // Not supported consumer type -> error return
309  if (!isEnabledConsumerType(sProfile)) { return false; }
310  if (!isExistingConsumerType(sProfile)) { return false; }
311  RTC_DEBUG(("Valid SDO service required"));
312  if (strncmp(sProfile.id, "", 1) == 0)
313  {
314  RTC_WARN(("No id specified. It should be given by clients."));
315  return false;
316  }
317  RTC_DEBUG(("Valid ID specified"));
318  { // re-initialization
319  std::string id(sProfile.id);
320  for (size_t i(0); i < m_consumers.size(); ++i)
321  {
322  if (id == static_cast<const char*>(m_consumers[i]->getProfile().id))
323  {
324  RTC_INFO(("Existing consumer is reinitilized."));
325  RTC_DEBUG(("Propeteis are: %s",
326  NVUtil::toString(sProfile.properties).c_str()));
327  return m_consumers[i]->reinit(sProfile);
328  }
329  }
330  }
331  RTC_DEBUG(("SDO service properly initialized."));
332 
333  // new pofile
336  const char* ctype = static_cast<const char*>(sProfile.interface_type);
337  if (ctype == NULL) { return false; }
338  SdoServiceConsumerBase* consumer(factory.createObject(ctype));
339  if (consumer == NULL)
340  {
341  RTC_ERROR(("Hmm... consumer must be created."));
342  return false;
343  }
344  RTC_DEBUG(("An SDO service consumer created."));
345 
346  // initialize
347  if (!consumer->init(m_rtobj, sProfile))
348  {
349  RTC_WARN(("SDO service initialization was failed."));
350  RTC_DEBUG(("id: %s", static_cast<const char*>(sProfile.id)));
351  RTC_DEBUG(("IFR: %s",
352  static_cast<const char*>(sProfile.interface_type)));
353  RTC_DEBUG(("properties: %s",
354  NVUtil::toString(sProfile.properties).c_str()));
355  factory.deleteObject(consumer);
356  RTC_INFO(("SDO consumer was deleted by initialization failure"));
357  return false;
358  }
359  RTC_DEBUG(("An SDO service consumer initialized."));
360  RTC_DEBUG(("id: %s", static_cast<const char*>(sProfile.id)));
361  RTC_DEBUG(("IFR: %s",
362  static_cast<const char*>(sProfile.interface_type)));
363  RTC_DEBUG(("properties: %s",
364  NVUtil::toString(sProfile.properties).c_str()));
365 
366  // store consumer
367  m_consumers.push_back(consumer);
368 
369  return true;
370  }
371 
380  {
381  Guard guard(m_consumer_mutex);
382  if (id == NULL || id[0] == '\0')
383  {
384  RTC_ERROR(("removeSdoServiceConsumer(): id is invalid."));
385  return false;
386  }
387  RTC_TRACE(("removeSdoServiceConsumer(id = %s)", id));
388 
389  std::string strid(id);
390  std::vector<SdoServiceConsumerBase*>::iterator it = m_consumers.begin();
391  std::vector<SdoServiceConsumerBase*>::iterator it_end = m_consumers.end();
392  while (it != it_end)
393  {
394  if (strid == static_cast<const char*>((*it)->getProfile().id))
395  {
396  (*it)->finalize();
399  factory.deleteObject(*it);
400  m_consumers.erase(it);
401  RTC_INFO(("SDO service has been deleted: %s", id));
402  return true;
403  }
404  ++it;
405  }
406  RTC_WARN(("Specified SDO consumer not found: %s", id));
407  return false;
408  }
409 
410  //------------------------------------------------------------
411  // protected functios
412  //------------------------------------------------------------
413 
421  bool SdoServiceAdmin::
422  isEnabledConsumerType(const SDOPackage::ServiceProfile& sProfile)
423  {
424  if (m_allConsumerEnabled) { return true; }
425 
426  for (size_t i(0); i < m_consumerTypes.size(); ++i)
427  {
428  if (m_consumerTypes[i] ==
429  static_cast<const char*>(sProfile.interface_type))
430  {
431  RTC_DEBUG(("%s is supported SDO service.",
432  static_cast<const char*>(sProfile.interface_type)));
433  return true;
434  }
435  }
436  RTC_WARN(("Consumer type is not supported: %s",
437  static_cast<const char*>(sProfile.interface_type)));
438  return false;
439  }
440 
448  bool SdoServiceAdmin::
449  isExistingConsumerType(const SDOPackage::ServiceProfile& sProfile)
450  {
452  coil::vstring consumerTypes(factory.getIdentifiers());
453 
454  for (size_t i(0); i < consumerTypes.size(); ++i)
455  {
456  if (consumerTypes[i] ==
457  static_cast<const char*>(sProfile.interface_type))
458  {
459  RTC_DEBUG(("%s exists in the SDO service factory.",
460  static_cast<const char*>(sProfile.interface_type)));
461  RTC_PARANOID(("Available SDO serices in the factory: %s",
462  coil::flatten(consumerTypes).c_str()));
463  return true;
464  }
465  }
466  RTC_WARN(("No available SDO service in the factory: %s",
467  static_cast<const char*>(sProfile.interface_type)));
468  return false;
469  }
470 
471  const std::string SdoServiceAdmin::getUUID() const
472  {
473  coil::UUID_Generator uugen;
474  uugen.init();
475  std::auto_ptr<coil::UUID> uuid(uugen.generateUUID(2,0x01));
476 
477  return (const char*) uuid->to_string();
478  }
479 
480  std::string SdoServiceAdmin::ifrToKey(std::string& ifr)
481  {
482  ::coil::vstring ifrvstr = ::coil::split(ifr, ":");
483  ::coil::toLower(ifrvstr[1]);
484  ::coil::replaceString(ifrvstr[1], ".", "_");
485  ::coil::replaceString(ifrvstr[1], "/", ".");
486  return ifrvstr[1];
487  }
488 
489 
490 }; // end of namepsace RTC
#define RTC_ERROR(fmt)
Error log output macro.
Definition: SystemLogger.h:422
virtual bool init(RTObject_impl &rtobj, const SDOPackage::ServiceProfile &profile)=0
Initialization function of the consumer class.
Functor for ServiceProfile.
RT-Component.
bool operator()(const SDOPackage::ServiceProfile &s)
const std::string m_id
void init()
Initialization.
RT-Component class.
Definition: RTObject.h:89
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
Split string by delimiter.
Definition: stringutil.cpp:341
bool removeSdoServiceProvider(const char *id)
Remove a SDO service provider.
SDOPackage::SDOService_ptr getServiceProvider(const char *id)
Get ServiceProfile of an SDO Service.
coil::Properties & getProperties()
[local interface] Get RTC property
Definition: RTObject.cpp:1525
static GlobalFactory< AbstractClass, Identifier, Compare, Creator, Destructor > & instance()
Create instance.
Definition: Singleton.h:131
GlobalFactory template class.
bool isEnabledConsumerType(const SDOPackage::ServiceProfile &sProfile)
If it is enabled service type.
#define RTC_WARN(fmt)
Warning log output macro.
Definition: SystemLogger.h:444
SDO service provider base class and its factory.
SDO service administration class.
#define RTC_PARANOID(fmt)
Paranoid level log output macro.
Definition: SystemLogger.h:555
std::vector< Identifier > getIdentifiers()
Get factory ID list.
std::vector< std::string > vstring
Definition: stringutil.h:37
SdoServiceAdmin(::RTC::RTObject_impl &rtobj)
Constructor.
coil::UUID * generateUUID(ACE_UINT16 version=0x0001, u_char variant=0x80)
Definition: ace/coil/UUID.h:45
#define RTC_DEBUG(fmt)
Debug level log output macro.
Definition: SystemLogger.h:488
std::string flatten(vstring sv)
Create CSV file from the given string list.
Definition: stringutil.cpp:549
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)
def j(str, encoding="cp932")
Definition: RunAtFirst.py:198
coil::Guard< coil::Mutex > Guard
coil::vstring m_consumerTypes
void deleteObject(const Identifier &id, AbstractClass *&obj)
Delete factory object.
std::string ifrToKey(std::string &ifr)
CORBA sequence utility template functions.
AbstractClass * createObject(const Identifier &id)
Create factory object.
unsigned int replaceString(std::string &str, const std::string from, const std::string to)
Replace string.
Definition: stringutil.cpp:317
bool addSdoServiceProvider(const SDOPackage::ServiceProfile &prof, SdoServiceProviderBase *provider)
Set a SDO service provider.
prop
Organization::get_organization_property ();.
service_id(const char *id)
void toLower(std::string &str)
Lowercase String Transformation.
Definition: stringutil.cpp:81
Class represents a set of properties.
Definition: Properties.h:101
const std::string getUUID() const
bool addSdoServiceConsumer(const SDOPackage::ServiceProfile &sProfile)
Add Service Consumer.
SDOPackage::ServiceProfile * getServiceProviderProfile(const char *id)
Get ServiceProfile of an SDO Service Provider.
#define RTC_INFO(fmt)
Information level log output macro.
Definition: SystemLogger.h:466
void push_back(CorbaSequence &seq, SequenceElement elem)
Push the new element back to the CORBA sequence.
RTC::RTObject_impl & m_rtobj
void copyFromProperties(SDOPackage::NVList &nv, const coil::Properties &prop)
Copy the properties to NVList.
Definition: NVUtil.cpp:108
SDOPackage::ServiceProfileList * getServiceProviderProfiles()
Get ServiceProfileList of SDO Service Provider.
coil::Mutex m_consumer_mutex
coil::Mutex m_provider_mutex
bool isExistingConsumerType(const SDOPackage::ServiceProfile &sProfile)
If it is existing service type.
SDO service consumer base class and its factory.
virtual ~SdoServiceAdmin()
Virtual destractor.
bool removeSdoServiceConsumer(const char *id)
Remove Service Consumer.
std::vector< SdoServiceConsumerBase * > m_consumers
SDO ServiceProfileList with mutex lock.
std::vector< SdoServiceProviderBase * > m_providers
SDO ServiceProfileList with mutex lock.


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