00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 import copy
00017 import threading
00018 import OpenRTM_aist
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 class SdoServiceAdmin:
00091 """
00092 """
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 def __init__(self, rtobj):
00108 self._rtobj = rtobj
00109 self._consumerTypes = []
00110 self._providers = []
00111 self._allConsumerEnabled = True
00112
00113
00114
00115
00116
00117
00118
00119
00120 self._providerProfiles = []
00121 self._provider_mutex = threading.RLock()
00122
00123
00124
00125
00126
00127
00128
00129 self._consumers = []
00130 self._consumer_mutex = threading.RLock()
00131
00132
00133
00134
00135
00136
00137
00138 self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf("SdoServiceAdmin")
00139
00140 self._rtcout.RTC_TRACE("SdoServiceAdmin::SdoServiceAdmin(%s)",
00141 rtobj.getProperties().getProperty("instance_name"))
00142
00143 prop = self._rtobj.getProperties()
00144
00145
00146
00147 enabledProviderTypes = [s.strip() for s in prop.getProperty("sdo.service.provider.enabled_services").split(",")]
00148 self._rtcout.RTC_DEBUG("sdo.service.provider.enabled_services: %s",
00149 prop.getProperty("sdo.service.provider.enabled_services"))
00150
00151 availableProviderTypes = OpenRTM_aist.SdoServiceProviderFactory.instance().getIdentifiers()
00152 prop.setProperty("sdo.service.provider.available_services",
00153 str(OpenRTM_aist.flatten(availableProviderTypes)))
00154 self._rtcout.RTC_DEBUG("sdo.service.provider.available_services: %s",
00155 prop.getProperty("sdo.service.provider.available_services"))
00156
00157
00158
00159 activeProviderTypes = []
00160 for i in range(len(enabledProviderTypes)):
00161 tmp = enabledProviderTypes[i].lower()
00162 if tmp == "all":
00163 activeProviderTypes = availableProviderTypes
00164 self._rtcout.RTC_DEBUG("sdo.service.provider.enabled_services: ALL")
00165 break
00166
00167 for j in range(len(availableProviderTypes)):
00168 if availableProviderTypes[j] == enabledProviderTypes[i]:
00169 activeProviderTypes.append(availableProviderTypes[j])
00170
00171 factory = OpenRTM_aist.SdoServiceProviderFactory.instance()
00172 for i in range(len(activeProviderTypes)):
00173 svc = factory.createObject(activeProviderTypes[i])
00174 propkey = self.ifrToKey(activeProviderTypes[i])
00175 properties = []
00176 OpenRTM_aist.NVUtil.copyFromProperties(properties,
00177 prop.getNode(str(propkey)))
00178 prof = SDOPackage.ServiceProfile(str(activeProviderTypes[i]),
00179 str(activeProviderTypes[i]),
00180 properties,
00181 svc._this())
00182
00183 svc.init(rtobj, prof)
00184 self._providers.append(svc)
00185
00186
00187
00188
00189 constypes = prop.getProperty("sdo.service.consumer.enabled_services")
00190 self._consumerTypes = [s.strip() for s in constypes.split(",")]
00191 self._rtcout.RTC_DEBUG("sdo.service.consumer.enabled_services: %s",
00192 str(constypes))
00193
00194 prop.setProperty("sdo.service.consumer.available_services",
00195 str(OpenRTM_aist.flatten(OpenRTM_aist.SdoServiceConsumerFactory.instance().getIdentifiers())))
00196 self._rtcout.RTC_DEBUG("sdo.service.consumer.available_services: %s",
00197 prop.getProperty("sdo.service.consumer.available_services"))
00198
00199
00200 for ctype in self._consumerTypes:
00201 tmp = ctype.lower()
00202 if tmp == "all":
00203 self._allConsumerEnabled = True
00204 self._rtcout.RTC_DEBUG("sdo_service.consumer_types: ALL")
00205
00206 return
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 def __del__(self):
00219 len_ = len(self._proiders)
00220 for i in range(len_):
00221 idx = (len_ - 1) - i
00222 self._providers[idx].finalize()
00223 del self._providers[idx]
00224
00225 self._providers = []
00226
00227 len_ = len(self._consumers)
00228 for i in range(len_):
00229 idx = (len_ - 1) - i
00230 self._consumers[idx].finalize()
00231 del self._consumers[idx]
00232
00233 self._consumers = []
00234 return
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 def getServiceProviderProfiles(self):
00246 prof = []
00247 guard = OpenRTM_aist.ScopedLock(self._provider_mutex)
00248 for i in range(len(self._providers)):
00249 prof.append(self._providers[i].getProfile())
00250 return prof
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 def getServiceProviderProfile(self, id):
00263 idstr = id
00264 guard = OpenRTM_aist.ScopedLock(self._provider_mutex)
00265 for i in range(len(self._providers)):
00266 if idstr == str(self._providers[i].getProfile().id):
00267 return self._providers[i].getProfile()
00268
00269 raise SDOPackage.InvalidParameter()
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 def getServiceProvider(self, id):
00281 prof = self.getServiceProviderProfile(id)
00282 return prof.service
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 def addSdoServiceProvider(self, prof, provider):
00296 self._rtcout.RTC_TRACE("SdoServiceAdmin::addSdoServiceProvider(if=%s)",
00297 prof.interface_type)
00298 guard = OpenRTM_aist.ScopedLock(self._provider_mutex)
00299 id = prof.id
00300 for i in range(len(self._providers)):
00301 if id == str(self._providers[i].getProfile().id):
00302 self._rtcout.RTC_ERROR("SDO service(id=%s, ifr=%s) already exists",
00303 str(prof.id), str(prof.interface_type))
00304 return False
00305
00306 self._providers.append(provider)
00307 return True
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 def removeSdoServiceProvider(self, id):
00319 self._rtcout.RTC_TRACE("removeSdoServiceProvider(%d)", id)
00320 guard = OpenRTM_aist.ScopedLock(self._provider_mutex)
00321
00322 strid = id
00323 len_ = len(self._providers)
00324 for i in range(len_):
00325 idx = (len_ - 1) - i
00326 if strid == str(self._providers[idx].getProfile().id):
00327 self._providers[idx].finalize()
00328 factory = OpenRTM_aist.SdoServiceProviderFactory.instance()
00329 factory.deleteObject(self._providers[idx])
00330 del self._providers[idx]
00331 self._rtcout.RTC_INFO("SDO service provider has been deleted: %s", id)
00332 return True
00333 self._rtcout.RTC_WARN("Specified SDO service provider not found: %s", id)
00334 return False
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 def addSdoServiceConsumer(self, sProfile):
00346 self._rtcout.RTC_TRACE("addSdoServiceConsumer(IFR = %s)",
00347 sProfile.interface_type)
00348 profile = copy.deepcopy(sProfile)
00349
00350
00351 if not self.isEnabledConsumerType(sProfile):
00352 self._rtcout.RTC_ERROR("Not supported consumer type. %s", profile.id)
00353 return False
00354
00355 if not self.isExistingConsumerType(sProfile):
00356 self._rtcout.RTC_ERROR("type %s already exists.", profile.id)
00357 return False
00358 if str(profile.id) == "":
00359 self._rtcout.RTC_WARN("No id specified. It should be given by clients.")
00360 return False
00361
00362
00363 guard = OpenRTM_aist.ScopedLock(self._consumer_mutex)
00364 id = str(sProfile.id)
00365 for i in range(len(self._consumers)):
00366 if id == str(self._consumers[i].getProfile().id):
00367 self._rtcout.RTC_INFO("Existing consumer is reinitilized.")
00368 self._rtcout.RTC_DEBUG("Propeteis are: %s",
00369 NVUtil.toString(sProfile.properties))
00370 return self._consumers[i].reinit(sProfile)
00371 del guard
00372
00373
00374 factory = OpenRTM_aist.SdoServiceConsumerFactory.instance()
00375 ctype = str(profile.interface_type)
00376 consumer = factory.createObject(ctype)
00377 if consumer == None:
00378 self._rtcout.RTC_ERROR("Hmm... consumer must be created.")
00379 return False
00380
00381
00382 if not consumer.init(self._rtobj, sProfile):
00383 self._rtcout.RTC_WARN("SDO service initialization was failed.")
00384 self._rtcout.RTC_DEBUG("id: %s", str(sProfile.id))
00385 self._rtcout.RTC_DEBUG("IFR: %s", str(sProfile.interface_type))
00386 self._rtcout.RTC_DEBUG("properties: %s", OpenRTM_aist.NVUtil.toString(sProfile.properties))
00387 factory.deleteObject(consumer)
00388 self._rtcout.RTC_INFO("SDO consumer was deleted by initialization failure")
00389 return False
00390
00391
00392 guard = OpenRTM_aist.ScopedLock(self._consumer_mutex)
00393 self._consumers.append(consumer)
00394 del guard
00395
00396 return True
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 def removeSdoServiceConsumer(self, id):
00408 if id == None or id[0] == '\0':
00409 self._rtcout.RTC_ERROR("removeSdoServiceConsumer(): id is invalid.")
00410 return False
00411
00412 self._rtcout.RTC_TRACE("removeSdoServiceConsumer(id = %s)", id)
00413
00414 guard = OpenRTM_aist.ScopedLock(self._consumer_mutex)
00415 strid = id
00416
00417 for (idx,cons) in enumerate(self._consumers):
00418 if strid == str(cons.getProfile().id):
00419 cons.finalize()
00420 del self._consumers[idx]
00421 factory = OpenRTM_aist.SdoServiceConsumerFactory.instance()
00422 factory.deleteObject(cons)
00423 self._rtcout.RTC_INFO("SDO service has been deleted: %s", id)
00424 return True
00425
00426 self._rtcout.RTC_WARN(("Specified SDO consumer not found: %s", id))
00427 return False
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439 def isEnabledConsumerType(self, sProfile):
00440 if self._allConsumerEnabled:
00441 return True
00442
00443 for i in range(len(self._consumerTypes)):
00444 if self._consumerTypes[i] == str(sProfile.interface_type):
00445 self._rtcout.RTC_DEBUG("%s is supported SDO service.",
00446 str(sProfile.interface_type))
00447 return True
00448
00449 self._rtcout.RTC_WARN("Consumer type is not supported: %s",
00450 str(sProfile.interface_type))
00451 return False
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 def isExistingConsumerType(self, sProfile):
00463 factory = OpenRTM_aist.SdoServiceConsumerFactory.instance()
00464 consumerTypes = factory.getIdentifiers()
00465 for i in range(len(consumerTypes)):
00466 if consumerTypes[i] == str(sProfile.interface_type):
00467 self._rtcout.RTC_DEBUG("%s exists in the SDO service factory.", str(sProfile.interface_type))
00468 self._rtcout.RTC_PARANOID("Available SDO serices in the factory: %s", str(OpenRTM_aist.flatten(consumerTypes)))
00469 return True
00470 self._rtcout.RTC_WARN("No available SDO service in the factory: %s",
00471 str(sProfile.interface_type))
00472 return False
00473
00474
00475
00476 def getUUID(self):
00477 return str(OpenRTM_aist.uuid1())
00478
00479
00480 def ifrToKey(self, ifr):
00481 ifrvstr = ifr.split(":")
00482 ifrvstr[1] = ifrvstr[1].lower()
00483 ifrvstr[1] = ifrvstr[1].replace(".", "_")
00484 ifrvstr[1] = ifrvstr[1].replace("/", ".")
00485 return ifrvstr[1]