SdoServiceAdmin.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: euc-jp -*-
3 
4 
15 
16 import copy
17 import threading
18 import OpenRTM_aist
19 
20 
21 
91  """
92  """
93 
94 
95 
107  def __init__(self, rtobj):
108  self._rtobj = rtobj
109  self._consumerTypes = []
110  self._providers = []
112 
113 
114 
121  self._provider_mutex = threading.RLock()
122 
123 
129  self._consumers = []
130  self._consumer_mutex = threading.RLock()
131 
132 
138  self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf("SdoServiceAdmin")
139 
140  self._rtcout.RTC_TRACE("SdoServiceAdmin::SdoServiceAdmin(%s)",
141  rtobj.getProperties().getProperty("instance_name"))
142 
143  prop = self._rtobj.getProperties()
144 
145  # ------------------------------------------------------------
146  # SDO service provider
147  enabledProviderTypes = [s.strip() for s in prop.getProperty("sdo.service.provider.enabled_services").split(",")]
148  self._rtcout.RTC_DEBUG("sdo.service.provider.enabled_services: %s",
149  prop.getProperty("sdo.service.provider.enabled_services"))
150 
151  availableProviderTypes = OpenRTM_aist.SdoServiceProviderFactory.instance().getIdentifiers()
152  prop.setProperty("sdo.service.provider.available_services",
153  str(OpenRTM_aist.flatten(availableProviderTypes)))
154  self._rtcout.RTC_DEBUG("sdo.service.provider.available_services: %s",
155  prop.getProperty("sdo.service.provider.available_services"))
156 
157 
158  # If types include '[Aa][Ll][Ll]', all types enabled in this RTC
159  activeProviderTypes = []
160  for i in range(len(enabledProviderTypes)):
161  tmp = enabledProviderTypes[i].lower()
162  if tmp == "all":
163  activeProviderTypes = availableProviderTypes
164  self._rtcout.RTC_DEBUG("sdo.service.provider.enabled_services: ALL")
165  break
166 
167  for j in range(len(availableProviderTypes)):
168  if availableProviderTypes[j] == enabledProviderTypes[i]:
169  activeProviderTypes.append(availableProviderTypes[j])
170 
171  factory = OpenRTM_aist.SdoServiceProviderFactory.instance()
172  for i in range(len(activeProviderTypes)):
173  svc = factory.createObject(activeProviderTypes[i])
174  propkey = self.ifrToKey(activeProviderTypes[i])
175  properties = []
177  prop.getNode(str(propkey)))
178  prof = SDOPackage.ServiceProfile(str(activeProviderTypes[i]),
179  str(activeProviderTypes[i]),
180  properties,
181  svc._this())
182 
183  svc.init(rtobj, prof)
184  self._providers.append(svc)
185 
186  # ------------------------------------------------------------
187  # SDO service consumer
188  # getting consumer types from RTC's properties
189  constypes = prop.getProperty("sdo.service.consumer.enabled_services")
190  self._consumerTypes = [s.strip() for s in constypes.split(",")]
191  self._rtcout.RTC_DEBUG("sdo.service.consumer.enabled_services: %s",
192  str(constypes))
193 
194  prop.setProperty("sdo.service.consumer.available_services",
195  str(OpenRTM_aist.flatten(OpenRTM_aist.SdoServiceConsumerFactory.instance().getIdentifiers())))
196  self._rtcout.RTC_DEBUG("sdo.service.consumer.available_services: %s",
197  prop.getProperty("sdo.service.consumer.available_services"))
198 
199  # If types include '[Aa][Ll][Ll]', all types allowed in this RTC
200  for ctype in self._consumerTypes:
201  tmp = ctype.lower()
202  if tmp == "all":
203  self._allConsumerEnabled = True
204  self._rtcout.RTC_DEBUG("sdo_service.consumer_types: ALL")
205 
206  return
207 
208 
209 
218  def __del__(self):
219  len_ = len(self._proiders)
220  for i in range(len_):
221  idx = (len_ - 1) - i
222  self._providers[idx].finalize()
223  del self._providers[idx]
224 
225  self._providers = []
226 
227  len_ = len(self._consumers)
228  for i in range(len_):
229  idx = (len_ - 1) - i
230  self._consumers[idx].finalize()
231  del self._consumers[idx]
232 
233  self._consumers = []
234  return
235 
236 
237 
246  prof = []
248  for i in range(len(self._providers)):
249  prof.append(self._providers[i].getProfile())
250  return prof
251 
252 
253 
263  idstr = id
265  for i in range(len(self._providers)):
266  if idstr == str(self._providers[i].getProfile().id):
267  return self._providers[i].getProfile()
268 
269  raise SDOPackage.InvalidParameter()
270 
271 
272 
280  def getServiceProvider(self, id):
281  prof = self.getServiceProviderProfile(id)
282  return prof.service
283 
284 
285 
295  def addSdoServiceProvider(self, prof, provider):
296  self._rtcout.RTC_TRACE("SdoServiceAdmin::addSdoServiceProvider(if=%s)",
297  prof.interface_type)
299  id = prof.id
300  for i in range(len(self._providers)):
301  if id == str(self._providers[i].getProfile().id):
302  self._rtcout.RTC_ERROR("SDO service(id=%s, ifr=%s) already exists",
303  str(prof.id), str(prof.interface_type))
304  return False
305 
306  self._providers.append(provider)
307  return True
308 
309 
310 
319  self._rtcout.RTC_TRACE("removeSdoServiceProvider(%d)", id)
321 
322  strid = id
323  len_ = len(self._providers)
324  for i in range(len_):
325  idx = (len_ - 1) - i
326  if strid == str(self._providers[idx].getProfile().id):
327  self._providers[idx].finalize()
328  factory = OpenRTM_aist.SdoServiceProviderFactory.instance()
329  factory.deleteObject(self._providers[idx])
330  del self._providers[idx]
331  self._rtcout.RTC_INFO("SDO service provider has been deleted: %s", id)
332  return True
333  self._rtcout.RTC_WARN("Specified SDO service provider not found: %s", id)
334  return False
335 
336 
337 
345  def addSdoServiceConsumer(self, sProfile):
346  self._rtcout.RTC_TRACE("addSdoServiceConsumer(IFR = %s)",
347  sProfile.interface_type)
348  profile = copy.deepcopy(sProfile)
349 
350  # Not supported consumer type -> error return
351  if not self.isEnabledConsumerType(sProfile):
352  self._rtcout.RTC_ERROR("Not supported consumer type. %s", profile.id)
353  return False
354 
355  if not self.isExistingConsumerType(sProfile):
356  self._rtcout.RTC_ERROR("type %s already exists.", profile.id)
357  return False
358  if str(profile.id) == "":
359  self._rtcout.RTC_WARN("No id specified. It should be given by clients.")
360  return False
361 
362  # re-initialization
364  id = str(sProfile.id)
365  for i in range(len(self._consumers)):
366  if id == str(self._consumers[i].getProfile().id):
367  self._rtcout.RTC_INFO("Existing consumer is reinitilized.")
368  self._rtcout.RTC_DEBUG("Propeteis are: %s",
369  NVUtil.toString(sProfile.properties))
370  return self._consumers[i].reinit(sProfile)
371  del guard
372 
373  # new pofile
374  factory = OpenRTM_aist.SdoServiceConsumerFactory.instance()
375  ctype = str(profile.interface_type)
376  consumer = factory.createObject(ctype)
377  if consumer == None:
378  self._rtcout.RTC_ERROR("Hmm... consumer must be created.")
379  return False
380 
381  # initialize
382  if not consumer.init(self._rtobj, sProfile):
383  self._rtcout.RTC_WARN("SDO service initialization was failed.")
384  self._rtcout.RTC_DEBUG("id: %s", str(sProfile.id))
385  self._rtcout.RTC_DEBUG("IFR: %s", str(sProfile.interface_type))
386  self._rtcout.RTC_DEBUG("properties: %s", OpenRTM_aist.NVUtil.toString(sProfile.properties))
387  factory.deleteObject(consumer)
388  self._rtcout.RTC_INFO("SDO consumer was deleted by initialization failure")
389  return False
390 
391  # store consumer
393  self._consumers.append(consumer)
394  del guard
395 
396  return True
397 
398 
399 
408  if id == None or id[0] == '\0':
409  self._rtcout.RTC_ERROR("removeSdoServiceConsumer(): id is invalid.")
410  return False
411 
412  self._rtcout.RTC_TRACE("removeSdoServiceConsumer(id = %s)", id)
413 
415  strid = id
416 
417  for (idx,cons) in enumerate(self._consumers):
418  if strid == str(cons.getProfile().id):
419  cons.finalize()
420  del self._consumers[idx]
421  factory = OpenRTM_aist.SdoServiceConsumerFactory.instance()
422  factory.deleteObject(cons)
423  self._rtcout.RTC_INFO("SDO service has been deleted: %s", id)
424  return True
425 
426  self._rtcout.RTC_WARN(("Specified SDO consumer not found: %s", id))
427  return False
428 
429 
430 
439  def isEnabledConsumerType(self, sProfile):
440  if self._allConsumerEnabled:
441  return True
442 
443  for i in range(len(self._consumerTypes)):
444  if self._consumerTypes[i] == str(sProfile.interface_type):
445  self._rtcout.RTC_DEBUG("%s is supported SDO service.",
446  str(sProfile.interface_type))
447  return True
448 
449  self._rtcout.RTC_WARN("Consumer type is not supported: %s",
450  str(sProfile.interface_type))
451  return False
452 
453 
454 
462  def isExistingConsumerType(self, sProfile):
463  factory = OpenRTM_aist.SdoServiceConsumerFactory.instance()
464  consumerTypes = factory.getIdentifiers()
465  for i in range(len(consumerTypes)):
466  if consumerTypes[i] == str(sProfile.interface_type):
467  self._rtcout.RTC_DEBUG("%s exists in the SDO service factory.", str(sProfile.interface_type))
468  self._rtcout.RTC_PARANOID("Available SDO serices in the factory: %s", str(OpenRTM_aist.flatten(consumerTypes)))
469  return True
470  self._rtcout.RTC_WARN("No available SDO service in the factory: %s",
471  str(sProfile.interface_type))
472  return False
473 
474 
475  # const std::string getUUID() const;
476  def getUUID(self):
477  return str(OpenRTM_aist.uuid1())
478 
479  # std::string SdoServiceAdmin::ifrToKey(std::string& ifr)
480  def ifrToKey(self, ifr):
481  ifrvstr = ifr.split(":")
482  ifrvstr[1] = ifrvstr[1].lower()
483  ifrvstr[1] = ifrvstr[1].replace(".", "_")
484  ifrvstr[1] = ifrvstr[1].replace("/", ".")
485  return ifrvstr[1]
def addSdoServiceConsumer(self, sProfile)
Add Service Consumer bool addSdoServiceConsumer(const SDOPackage::ServiceProfile& sProfile);...
def getServiceProviderProfiles(self)
Get ServiceProfileList of SDO Service ProviderSDOPackage::ServiceProfileList* SdoServiceAdmin::getSer...
def split(input, delimiter)
Split string by delimiter.
Definition: StringUtil.py:323
def isEnabledConsumerType(self, sProfile)
If it is enabled service typebool SdoServiceAdmin:: isEnabledConsumerType(const SDOPackage::ServicePr...
def addSdoServiceProvider(self, prof, provider)
Set a SDO service providerbool SdoServiceAdmin:: addSdoServiceProvider(const SDOPackage::ServiceProfi...
def __del__(self)
Virtual destractor Virtual destractor.
def removeSdoServiceConsumer(self, id)
Remove Service Consumer bool removeSdoServiceConsumer(const char* id);.
def getServiceProviderProfile(self, id)
Get ServiceProfile of an SDO Service ProviderSDOPackage::ServiceProfile* SdoServiceAdmin::getServiceP...
def toString(nv, name=None)
Get string value in NVList specified by name.
Definition: NVUtil.py:311
def isExistingConsumerType(self, sProfile)
If it is existing service type bool isExistingConsumerType(const SDOPackage::ServiceProfile& sProfile...
def getServiceProvider(self, id)
Get ServiceProfile of an SDO ServiceSDOPackage::SDOService_ptr SdoServiceAdmin::getServiceProvider(co...
def append(dest, src)
Definition: NVUtil.py:386
def copyFromProperties(nv, prop)
Copy to NVList from Proeprties.
Definition: NVUtil.py:85
def __init__(self, rtobj)
Constructor Constructor.
def removeSdoServiceProvider(self, id)
Remove a SDO service providerbool SdoServiceAdmin::removeSdoServiceProvider(const char* id) ...


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Mon Feb 28 2022 23:01:06