00001 from omniORB import CORBA, any, cdrUnmarshal, cdrMarshal
00002 import CosNaming
00003
00004 import OpenRTM_aist
00005 import RTC, OpenRTM, SDOPackage, RTM
00006 from OpenRTM import CdrData, OutPortCdr, InPortCdr
00007 from RTC import *
00008
00009 import sys
00010 import string, math, socket
00011 import os
00012 import time
00013 import re
00014
00015
00016
00017
00018 rootnc = None
00019 nshost = None
00020 nsport = None
00021 mgrhost = None
00022 mgrport = None
00023
00024
00025
00026
00027 class RTcomponent:
00028
00029
00030
00031
00032 def __init__(self, ref):
00033 self.ref = ref
00034 self.owned_ecs = ref.get_owned_contexts()
00035 self.ec = self.owned_ecs[0]
00036 self.ports = {}
00037 ports = self.ref.get_ports()
00038 for p in ports:
00039 prof = p.get_port_profile()
00040 name = prof.name.split('.')[1]
00041 self.ports[name] = p
00042
00043
00044
00045
00046
00047
00048 def port(self, name):
00049 try:
00050 p = self.ports[unicode(name)]
00051 except KeyError:
00052 p = findPort(self.ref, name)
00053 self.ports[unicode(name)] = p
00054 return p
00055
00056
00057
00058
00059
00060
00061
00062
00063 def service(self, instance_name, type_name="", port_name=""):
00064 return findService(self, port_name, type_name, instance_name)
00065
00066
00067
00068
00069
00070
00071 def setConfiguration(self, nvlist):
00072 return setConfiguration(self.ref, nvlist)
00073
00074
00075
00076
00077
00078
00079
00080 def setProperty(self, name, value):
00081 return self.setConfiguration([[name, value]])
00082
00083
00084
00085
00086
00087 def getProperties(self):
00088 return getConfiguration(self.ref)
00089
00090
00091
00092
00093
00094
00095 def getProperty(self, name):
00096 cfg = self.ref.get_configuration()
00097 cfgsets = cfg.get_configuration_sets()
00098 if len(cfgsets) == 0:
00099 print("configuration set is not found")
00100 return None
00101 cfgset = cfgsets[0]
00102 for d in cfgset.configuration_data:
00103 if d.name == name:
00104 return any.from_any(d.value)
00105 return None
00106
00107
00108
00109
00110
00111
00112
00113 def start(self, ec=None, timeout=3.0):
00114 if ec == None:
00115 ec = self.ec
00116 if ec != None:
00117 if self.isActive(ec):
00118 return True
00119 ret = ec.activate_component(self.ref)
00120 if ret != RTC.RTC_OK:
00121 print ('[rtm.py] \033[31m Failed to start %s(%s)\033[0m' % \
00122 (self.name(), ret))
00123 return False
00124 tm = 0
00125 while tm < timeout:
00126 if self.isActive(ec):
00127 return True
00128 time.sleep(0.01)
00129 tm += 0.01
00130 print ('[rtm.py] \033[31m Failed to start %s(timeout)\033[0m' % \
00131 self.name())
00132 return False
00133
00134
00135
00136
00137
00138
00139
00140 def stop(self, ec=None, timeout=3.0):
00141 if ec == None:
00142 ec = self.ec
00143 if ec != None:
00144 if self.isInactive(ec):
00145 return True
00146 ret = ec.deactivate_component(self.ref)
00147 if ret != RTC.RTC_OK:
00148 print ('[rtm.py] \033[31m Failed to stop %s(%s)\033[0m' % \
00149 (self.name(), ret))
00150 return False
00151 tm = 0
00152 while tm < timeout:
00153 if self.isInactive(ec):
00154 return True
00155 time.sleep(0.01)
00156 tm += 0.01
00157 print ('[rtm.py] \033[31m Failed to stop %s(timeout)\033[0m' % \
00158 self.name())
00159 return False
00160
00161
00162
00163
00164
00165
00166 def reset(self, ec=None, timeout=3.0):
00167 if self.getLifeCycleState(ec) != RTC.ERROR_STATE:
00168 return True
00169 if ec == None:
00170 ec = self.ec
00171 return ec.reset_component(self.ref) == RTC.RTC_OK
00172
00173
00174
00175
00176
00177 def getLifeCycleState(self, ec=None):
00178 if ec == None:
00179 ec = self.ec
00180 if ec != None:
00181 return ec.get_component_state(self.ref)
00182 else:
00183 return None
00184
00185
00186
00187
00188
00189
00190 def isActive(self, ec=None):
00191 return RTC.ACTIVE_STATE == self.getLifeCycleState(ec)
00192
00193
00194
00195
00196
00197
00198 def isInactive(self, ec=None):
00199 return RTC.INACTIVE_STATE == self.getLifeCycleState(ec)
00200
00201
00202
00203
00204 def name(self):
00205 cprof = self.ref.get_component_profile()
00206 return cprof.instance_name
00207
00208
00209
00210
00211 class RTCmanager:
00212
00213
00214
00215
00216 def __init__(self, ref):
00217 self.ref = ref
00218 uname = os.uname()[0]
00219 if uname == "Darwin":
00220 self.soext = ".dylib"
00221 else:
00222 self.soext = ".so"
00223
00224
00225
00226
00227
00228
00229
00230 def load(self, basename, initfunc=""):
00231 path = basename + self.soext
00232 if initfunc == "":
00233 basename + "Init"
00234 try:
00235 self.ref.load_module(path, initfunc)
00236 except:
00237 print("failed to load", path)
00238
00239
00240
00241
00242
00243
00244
00245 def create(self, module, name=None):
00246 if name != None:
00247 rtc = findRTC(name)
00248 if rtc != None:
00249 print('RTC named "' + name + '" already exists.')
00250 return rtc
00251 args = module
00252 if name != None:
00253 args += '?instance_name=' + name
00254 ref = self.ref.create_component(args)
00255 if ref == None:
00256 return None
00257 else:
00258 return RTcomponent(ref)
00259
00260
00261
00262
00263
00264 def delete(self, name):
00265
00266 ref = findRTC(name).ref.exit()
00267 if ref == RTC_OK:
00268 return True
00269 else:
00270 return False
00271
00272
00273
00274
00275 def get_factory_names(self):
00276 fs = []
00277 fps = self.ref.get_factory_profiles()
00278 for afp in fps:
00279 for p in afp.properties:
00280 if p.name == "implementation_id":
00281 fs.append(any.from_any(p.value))
00282 return fs
00283
00284
00285
00286
00287 def get_components(self):
00288 cs = []
00289 crefs = self.ref.get_components()
00290 for cref in crefs:
00291 c = RTcomponent(cref)
00292 cs.append(c)
00293 return cs
00294
00295
00296
00297 def restart(self):
00298 self.ref.shutdown()
00299 time.sleep(1)
00300
00301
00302
00303
00304
00305
00306 def unbindObject(name, kind):
00307 nc = NameComponent(name, kind)
00308 path = [nc]
00309 rootnc.unbind(path)
00310 return None
00311
00312
00313
00314
00315 def initCORBA():
00316 global rootnc, orb, nshost, nsport, mgrhost, mgrport
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326 rtm_argv = [sys.argv[0]]
00327 for i in range(len(sys.argv)):
00328 if sys.argv[i] == '-o':
00329 rtm_argv.extend(['-o', sys.argv[i+1]])
00330
00331 mc = OpenRTM_aist.ManagerConfig();
00332 mc.parseArgs(rtm_argv)
00333
00334 if nshost != None:
00335 print("\033[34m[rtm.py] nshost already set as " + str(nshost) + "\033[0m")
00336 else:
00337 try:
00338 nshost = mc._argprop.getProperty("corba.nameservers").split(":")[0]
00339 if not nshost:
00340 raise
00341 except:
00342 nshost = socket.gethostname()
00343 print("\033[34m[rtm.py] Failed to parse corba.nameservers, use " + str(nshost) + " as nshost \033[0m")
00344
00345 if nsport != None:
00346 print("\033[34m[rtm.py] nsport already set as " + str(nsport) + "\033[0m")
00347 else:
00348 try:
00349 nsport = int(mc._argprop.getProperty("corba.nameservers").split(":")[1])
00350 if not nsport:
00351 raise
00352 except:
00353 nsport = 15005
00354 print("\033[34m[rtm.py] Failed to parse corba.nameservers, use " + str(nsport) + " as nsport \033[0m")
00355
00356 if mgrhost != None:
00357 print("\033[34m[rtm.py] mgrhost already set as " + str(mgrhost) + "\033[0m")
00358 else:
00359 mgrhost = nshost
00360
00361 if mgrport != None:
00362 print("\033[34m[rtm.py] mgrport already set as " + str(mgrport) + "\033[0m")
00363 else:
00364 try:
00365 mgrport = int(mc._argprop.getProperty("corba.master_manager").split(":")[1])
00366 if not mgrport:
00367 raise
00368 except:
00369 mgrport = 2810
00370 print("\033[34m[rtm.py] Failed to parse corba.master_manager, use " + str(mgrport) + "\033[0m")
00371
00372 print("\033[34m[rtm.py] configuration ORB with %s:%s\033[0m"%(nshost, nsport))
00373 print("\033[34m[rtm.py] configuration RTCManager with %s:%s\033[0m"%(mgrhost, mgrport))
00374 os.environ['ORBInitRef'] = 'NameService=corbaloc:iiop:%s:%s/NameService' % \
00375 (nshost, nsport)
00376
00377 try:
00378 orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
00379 nameserver = orb.resolve_initial_references("NameService")
00380 rootnc = nameserver._narrow(CosNaming.NamingContext)
00381 except omniORB.CORBA.ORB.InvalidName:
00382 _, e, _ = sys.exc_info()
00383 sys.exit('[ERROR] Invalide Name (hostname=%s).\n' % (nshost) +
00384 'Make sure the hostname is correct.\n' + str(e))
00385 except omniORB.CORBA.TRANSIENT:
00386 try:
00387 nameserver = orb.string_to_object('corbaloc:iiop:%s:%s/NameService'%(nshost, nsport))
00388 rootnc = nameserver._narrow(CosNaming.NamingContext)
00389 except:
00390 _, e, _ = sys.exc_info()
00391 sys.exit('[ERROR] Connection Failed with the Nameserver (hostname=%s port=%s).\n' % (nshost, nsport) +
00392 'Make sure the hostname is correct and the Nameserver is running.\n' + str(e))
00393 except Exception:
00394 _, e, _ = sys.exc_info()
00395 print(str(e))
00396
00397 return None
00398
00399
00400
00401
00402
00403
00404 def getRootNamingContext(corbaloc):
00405 props = System.getProperties()
00406
00407 args = ["-ORBInitRef", corbaloc]
00408 orb = ORB.init(args, props)
00409
00410 nameserver = orb.resolve_initial_references("NameService")
00411 return NamingContextHelper.narrow(nameserver)
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421 def findObject(name, kind="", rnc=None):
00422 nc = CosNaming.NameComponent(name, kind)
00423 path = [nc]
00424 if not rnc:
00425 rnc = rootnc
00426 if not rnc:
00427 print("[ERROR] findObject(%r,kind=%r,rnc=%r) rootnc is not found" % (name, kind, rnc))
00428 return rnc.resolve(path)
00429
00430
00431
00432
00433
00434
00435
00436
00437 def findRTCmanager(hostname=None, rnc=None):
00438 if not rootnc:
00439 print("[ERROR] findRTCmanager(hostname=%r,rnc=%r) rootnc is not defined, need to call initCORBA()" % \
00440 (hostname, rnc))
00441 if not hostname:
00442 hostname = nshost
00443 cxt = None
00444 if not hostname:
00445 hostname = socket.gethostname()
00446 try:
00447 socket.gethostbyaddr(hostname)
00448 except Exception:
00449 _, e, _ = sys.exc_info()
00450 sys.exit('[ERROR] %s\n' % (str(e)) + '[ERROR] Could not get hostname for %s\n' % (hostname) +
00451 '[ERROR] Make sure that you set the target hostname and address in DNS or /etc/hosts in linux/unix.')
00452
00453
00454 def getManagerFromNS(hostname, mgr=None):
00455 try:
00456 obj = findObject("manager", "mgr", findObject(hostname, "host_cxt", rnc))
00457 mgr = RTCmanager(obj._narrow(RTM.Manager))
00458 except:
00459 mgr = None
00460 return mgr
00461
00462 def getManagerDirectly(hostname, mgr=None):
00463 global orb, mgrport
00464 corbaloc = "corbaloc:iiop:" + hostname + ":" + str(mgrport) + "/manager"
00465 print("\033[34m[rtm.py] trying to findRTCManager on port" + str(mgrport) + "\033[0m")
00466 try:
00467 obj = orb.string_to_object(corbaloc)
00468 mgr = RTCmanager(obj._narrow(RTM.Manager))
00469 except:
00470 mgr = None
00471 return mgr
00472
00473 try:
00474 import CORBA
00475 except:
00476 print('import CORBA failed in findRTCmanager and neglect it for old python environment.')
00477
00478 mgr = None
00479 hostnames = [hostname, hostname.split(".")[0],
00480 socket.gethostbyaddr(hostname)[0],
00481 socket.gethostbyaddr(hostname)[0].split(".")[0]]
00482 for h in hostnames:
00483 mgr = getManagerDirectly(h) or getManagerFromNS(h)
00484 if mgr:
00485 return mgr
00486 print("Manager not found")
00487 return None
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497 def findRTC(name, rnc=None):
00498 try:
00499 obj = findObject(name, "rtc", rnc)
00500 try:
00501 rtc = RTcomponent(obj._narrow(RTC.RTObject))
00502 except TypeError:
00503 rtc = RTcomponent(obj._narrow(RTC.DataFlowComponent))
00504 cxts = rtc.ref.get_participating_contexts()
00505 if len(cxts) > 0:
00506 rtc.ec = cxts[0]
00507 return rtc
00508 except:
00509 return None
00510
00511
00512
00513
00514
00515
00516
00517 def findPort(rtc, name):
00518 ports = rtc.get_ports()
00519 cprof = rtc.get_component_profile()
00520 portname = cprof.instance_name + "." + name
00521 for p in ports:
00522 prof = p.get_port_profile()
00523 if prof.name == portname:
00524 return p
00525 return None
00526
00527
00528
00529
00530
00531
00532
00533 def serializeComponents(rtcs, stopEC=True):
00534 if len(rtcs) < 2:
00535 return
00536 ec = rtcs[0].ec
00537 for rtc in rtcs[1:]:
00538 try:
00539 if not ec._is_equivalent(rtc.ec):
00540 if stopEC:
00541 rtc.ec.stop()
00542 if ec.add_component(rtc.ref) == RTC.RTC_OK:
00543 rtc.ec = ec
00544 else:
00545 print('error in add_component()')
00546 else:
00547 print(rtc.name() + 'is already serialized')
00548 except Exception:
00549 _, e, _ = sys.exc_info()
00550 print("[rtm.py] \033[31m error in serialize %s of %s %s\033[0m" % (rtc.name(), [[r, r.name()] for r in rtcs], str(e)))
00551 raise e
00552
00553
00554
00555
00556
00557
00558 def isConnected(outP, inP):
00559 op = outP.get_port_profile()
00560 for con_prof in op.connector_profiles:
00561 ports = con_prof.ports
00562 if len(ports) == 2 and outP._is_equivalent(ports[0]) and \
00563 inP._is_equivalent(ports[1]):
00564 return True
00565 return False
00566
00567
00568
00569
00570
00571
00572
00573 def disconnectPorts(outP, inP):
00574 op = outP.get_port_profile()
00575 iname = inP.get_port_profile().name
00576 for con_prof in op.connector_profiles:
00577 ports = con_prof.ports
00578 if len(ports) == 2:
00579 pname = ports[1].get_port_profile().name
00580 if pname == iname:
00581 print('[rtm.py] Disconnect %s - %s' %(op.name, iname))
00582 outP.disconnect(con_prof.connector_id)
00583 return True
00584 return False
00585
00586
00587
00588
00589
00590
00591 def dataTypeOfPort(port):
00592 prof = port.get_port_profile()
00593 prop = prof.properties
00594 for p in prop:
00595 if p.name == "dataport.data_type":
00596 return any.from_any(p.value)
00597 return None
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608 def connectPorts(outP, inPs, subscription="flush", dataflow="Push", bufferlength=1, rate=1000, pushpolicy="new", interfaceType="corba_cdr"):
00609 if not isinstance(inPs, list):
00610 inPs = [inPs]
00611 if not outP:
00612 print('[rtm.py] \033[31m Failed to connect %s to %s(%s)\033[0m' % \
00613 (outP, [inP.get_port_profile().name if inP else inP for inP in inPs], inPs))
00614 return
00615 for inP in inPs:
00616 if not inP:
00617 print('[rtm.py] \033[31m Failed to connect %s to %s(%s)\033[0m' % \
00618 (outP.get_port_profile().name, inP, inPs))
00619 continue
00620 if isConnected(outP, inP) == True:
00621 print('[rtm.py] %s and %s are already connected' % \
00622 (outP.get_port_profile().name, inP.get_port_profile().name))
00623 continue
00624 if dataTypeOfPort(outP) != dataTypeOfPort(inP):
00625 print('[rtm.py] \033[31m %s and %s have different data types\033[0m' % \
00626 (outP.get_port_profile().name, inP.get_port_profile().name))
00627 continue
00628 nv1 = SDOPackage.NameValue("dataport.interface_type", any.to_any(interfaceType))
00629 nv2 = SDOPackage.NameValue("dataport.dataflow_type", any.to_any(dataflow))
00630 nv3 = SDOPackage.NameValue("dataport.subscription_type", any.to_any(subscription))
00631 nv4 = SDOPackage.NameValue("dataport.buffer.length", any.to_any(str(bufferlength)))
00632 nv5 = SDOPackage.NameValue("dataport.publisher.push_rate", any.to_any(str(rate)))
00633 nv6 = SDOPackage.NameValue("dataport.publisher.push_policy", any.to_any(pushpolicy))
00634 nv7 = SDOPackage.NameValue("dataport.data_type", any.to_any(dataTypeOfPort(outP)))
00635 con_prof = RTC.ConnectorProfile("connector0", "", [outP, inP],
00636 [nv1, nv2, nv3, nv4, nv5, nv6, nv7])
00637 print('[rtm.py] Connect ' + outP.get_port_profile().name + ' - ' + \
00638 inP.get_port_profile().name+' (dataflow_type='+dataflow+', subscription_type='+ subscription+', bufferlength='+str(bufferlength)+', push_rate='+str(rate)+', push_policy='+pushpolicy+')')
00639 ret, prof = inP.connect(con_prof)
00640 if ret != RTC.RTC_OK:
00641 print("failed to connect")
00642 continue
00643
00644 if isConnected(outP, inP) == False:
00645 print("connet() returned RTC_OK, but not connected")
00646
00647
00648
00649
00650
00651
00652 def data2cdr(data):
00653 return cdrMarshal(any.to_any(data).typecode(), data, True)
00654
00655
00656
00657
00658
00659
00660 def classFromString(fullname):
00661 component_path = fullname.split('.')
00662 package_name = component_path[0]
00663 component_path = component_path[1:]
00664 attr = None
00665 while component_path:
00666 class_name = component_path[0]
00667 component_path = component_path[1:]
00668 if attr:
00669 attr = getattr(attr, class_name)
00670 else:
00671 __import__(str(package_name))
00672 attr = getattr(sys.modules[package_name], class_name)
00673 return attr
00674
00675
00676
00677
00678
00679
00680
00681 def cdr2data(cdr, classname):
00682 return cdrUnmarshal(any.to_any(classFromString(classname)).typecode(), cdr, True)
00683
00684
00685
00686 connector_list = []
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697 def writeDataPort(port, data, tm=1.0, disconnect=True):
00698 global connector_list, orb
00699
00700 connector_name = "writeDataPort"
00701
00702
00703
00704 prof = None
00705
00706 for p in connector_list:
00707 if p["port"]._is_equivalent(port):
00708 if port.get_connector_profile(p["prof"].connector_id).name == connector_name:
00709 prof = p["prof"]
00710 else:
00711 connector_list.remove(p)
00712
00713
00714
00715
00716 if prof is None:
00717 nv1 = SDOPackage.NameValue("dataport.interface_type", any.to_any("corba_cdr"))
00718 nv2 = SDOPackage.NameValue("dataport.dataflow_type", any.to_any("Push"))
00719 nv3 = SDOPackage.NameValue("dataport.subscription_type", any.to_any("flush"))
00720 con_prof = RTC.ConnectorProfile(connector_name, "", [port], [nv1, nv2, nv3])
00721
00722 ret, prof = port.connect(con_prof)
00723
00724 if ret != RTC.RTC_OK:
00725 print("failed to connect")
00726 return None
00727 connector_list.append({"port":port,"prof":prof})
00728
00729
00730 for p in prof.properties:
00731 if p.name == 'dataport.corba_cdr.inport_ior':
00732 ior = any.from_any(p.value)
00733 obj = orb.string_to_object(ior)
00734 inport = obj._narrow(InPortCdr)
00735 cdr = data2cdr(data)
00736 if inport.put(cdr) != OpenRTM.PORT_OK:
00737 print("failed to put")
00738 if disconnect:
00739 time.sleep(tm)
00740 port.disconnect(prof.connector_id)
00741 for p in connector_list:
00742 if prof.connector_id == p["prof"].connector_id:
00743 connector_list.remove(p)
00744 else:
00745 return prof.connector_id
00746 return None
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756 def readDataPort(port, timeout=1.0, disconnect=True):
00757 global connector_list, orb
00758
00759
00760 connector_name = "readDataPort"
00761 prof = None
00762 for p in connector_list:
00763 if p["port"]._is_equivalent(port):
00764 if port.get_connector_profile(p["prof"].connector_id).name == connector_name:
00765 prof = p["prof"]
00766 else:
00767 connector_list.remove(p)
00768
00769
00770
00771
00772
00773
00774 pprof = port.get_port_profile()
00775 for prop in pprof.properties:
00776 if prop.name == "dataport.data_type":
00777 classname = any.from_any(prop.value)
00778
00779 if prof is None:
00780
00781 nv1 = SDOPackage.NameValue("dataport.interface_type", any.to_any("corba_cdr"))
00782 nv2 = SDOPackage.NameValue("dataport.dataflow_type", any.to_any("Pull"))
00783 nv3 = SDOPackage.NameValue("dataport.subscription_type", any.to_any("flush"))
00784 con_prof = RTC.ConnectorProfile(connector_name, "", [port], [nv1, nv2, nv3])
00785
00786 ret, prof = port.connect(con_prof)
00787
00788 if ret != RTC.RTC_OK:
00789 print("failed to connect")
00790 return None
00791
00792 connector_list.append({"port":port,"prof":prof})
00793
00794 for p in prof.properties:
00795 if p.name == 'dataport.corba_cdr.outport_ior':
00796 ior = any.from_any(p.value)
00797 obj = orb.string_to_object(ior)
00798 outport = obj._narrow(OutPortCdr)
00799 tm = 0
00800 while tm < timeout:
00801 try:
00802 ret, data = outport.get()
00803 if ret == OpenRTM.PORT_OK:
00804 if disconnect:
00805 port.disconnect(prof.connector_id)
00806 for p in connector_list:
00807 if prof.connector_id == p["prof"].connector_id:
00808 connector_list.remove(p)
00809
00810 tokens = classname.split(':')
00811 if len(tokens) == 3:
00812 classname = tokens[1].replace('/', '.')
00813 return cdr2data(data, classname)
00814 except:
00815 pass
00816 time.sleep(0.1)
00817 tm = tm + 0.1
00818
00819
00820
00821 return None
00822
00823
00824
00825
00826 def deleteAllConnector():
00827 global connector_list
00828 for port in connector_list:
00829 port["port"].disconnect(port["prof"].connector_id)
00830 del connector_list[:]
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840 def findService(rtc, port_name, type_name, instance_name):
00841 if port_name == "":
00842 prof = rtc.ref.get_component_profile()
00843
00844 port_prof = prof.port_profiles
00845 else:
00846 p = rtc.port(port_name)
00847 if p == None:
00848 print("can't find a port named" + port_name)
00849 return None
00850 else:
00851 port_prof = [p.get_port_profile()]
00852 port = None
00853 for pp in port_prof:
00854
00855 ifs = pp.interfaces
00856 for aif in ifs:
00857
00858
00859 if aif.instance_name == instance_name and \
00860 (type_name == "" or aif.type_name == type_name) and \
00861 aif.polarity == PROVIDED:
00862 port = pp.port_ref
00863 if port == None:
00864 print("can't find a service named", instance_name)
00865 return None
00866 con_prof = RTC.ConnectorProfile("noname", "", [port], [])
00867 ret, con_prof = port.connect(con_prof)
00868 ior = any.from_any(con_prof.properties[0].value)
00869 return orb.string_to_object(ior)
00870
00871
00872
00873
00874
00875
00876
00877 def setConfiguration(rtc, nvlist):
00878 ret = True
00879 cfg = rtc.get_configuration()
00880 cfgsets = cfg.get_configuration_sets()
00881 if len(cfgsets) == 0:
00882 print("configuration set is not found")
00883 return
00884 cfgset = cfgsets[0]
00885 for nv in nvlist:
00886 name = nv[0]
00887 value = nv[1]
00888 found = False
00889 for d in cfgset.configuration_data:
00890 if d.name == name:
00891 d.value = any.to_any(value)
00892 cfg.set_configuration_set_values(cfgset)
00893 found = True
00894 break
00895 if not found:
00896 ret = False
00897 cfg.activate_configuration_set('default')
00898 return ret
00899
00900
00901
00902
00903
00904
00905 def getConfiguration(rtc):
00906 cfg = rtc.get_configuration()
00907 cfgsets = cfg.get_configuration_sets()
00908 if len(cfgsets) == 0:
00909 print("configuration set is not found")
00910 return None
00911 ret = {}
00912 for nv in cfgsets[0].configuration_data:
00913 ret[nv.name] = any.from_any(nv.value)
00914 return ret
00915
00916
00917
00918
00919
00920
00921
00922
00923 def narrow(ior, klass, package="OpenHRP"):
00924 return ior._narrow(getattr(sys.modules[package], klass))
00925
00926
00927
00928
00929
00930 def isJython():
00931 return sys.version.count("GCC") == 0
00932
00933
00934 if __name__ == '__main__':
00935 initCORBA()