jython/rtm.py
Go to the documentation of this file.
1 
5 from org.omg.CORBA import *
6 from org.omg.CosNaming import *
8 from com.sun.corba.se.impl.encoding import EncapsOutputStream
9 
10 from java.lang import System, Class
11 
12 from RTC import *
13 from RTM import *
14 from OpenRTM import *
15 from _SDOPackage import *
16 
17 import string, math, socket, time, sys
18 
19 
22 rootnc = None
23 
24 
27 nshost = None
28 
29 
33 
38  def __init__(self, ref):
39  self.ref = ref
40  self.owned_ecs = ref.get_owned_contexts()
41  self.ec = self.owned_ecs[0]
42  self.ports = {}
43  ports = self.ref.get_ports()
44  for p in ports:
45  prof = p.get_port_profile()
46  name = prof.name.split('.')[1]
47  self.ports[name] = p
48 
49 
54  def port(self, name):
55  try:
56  p = self.ports[unicode(name)]
57  except KeyError:
58  p = findPort(self.ref, name)
59  self.ports[unicode(name)] = p
60  return p
61 
62 
69  def service(self, instance_name, type_name="", port_name=""):
70  return findService(self, port_name, type_name, instance_name)
71 
72 
76  def setConfiguration(self, nvlist):
77  setConfiguration(self.ref, nvlist)
78 
79 
84  def setProperty(self, name, value):
85  self.setConfiguration([[name, value]])
86 
87 
92  def getProperty(self, name):
93  cfg = self.ref.get_configuration()
94  cfgsets = cfg.get_configuration_sets()
95  if len(cfgsets) == 0:
96  print "configuration set is not found"
97  return None
98  cfgset = cfgsets[0]
99  for d in cfgset.configuration_data:
100  if d.name == name:
101  return d.value.extract_string()
102  return None
103 
104 
107  def properties(self):
108  cfg = self.ref.get_configuration()
109  cfgsets = cfg.get_configuration_sets()
110  if len(cfgsets) == 0:
111  print "configuration set is not found"
112  return
113  cfgset = cfgsets[0]
114  for d in cfgset.configuration_data:
115  print d.name,":",d.value.extract_string()
116 
117 
118 
122  def start(self, ec=None):
123  if ec == None:
124  ec = self.ec
125  if ec != None:
126  ec.activate_component(self.ref)
127  while self.isInactive(ec):
128  time.sleep(0.01)
129 
130 
134  def stop(self, ec=None):
135  if ec == None:
136  ec = self.ec
137  if ec != None:
138  ec.deactivate_component(self.ref)
139  while self.isActive(ec):
140  time.sleep(0.01)
141 
142 
147  def getLifeCycleState(self, ec=None):
148  if ec == None:
149  ec = self.ec
150  if ec != None:
151  return ec.get_component_state(self.ref)
152  else:
153  return None
154 
155 
160  def isActive(self, ec=None):
161  return LifeCycleState.ACTIVE_STATE == self.getLifeCycleState(ec)
162 
163 
168  def isInactive(self, ec=None):
169  return LifeCycleState.INACTIVE_STATE == self.getLifeCycleState(ec)
170 
171 
174  def name(self):
175  cprof = self.ref.get_component_profile()
176  return cprof.instance_name
177 
178 
182 
187  def __init__(self, ref):
188  self.ref = ref
189  osname = System.getProperty("os.name")
190  # assuming jython and rtcd are running on the same OS
191  if osname == "Mac OS X":
192  self.soext = ".dylib"
193  else:
194  self.soext = ".so"
195 
196 
201  def load(self, basename):
202  path = basename+self.soext
203  initfunc = basename+"Init"
204  try:
205  self.ref.load_module(path, initfunc)
206  except:
207  print "failed to load",path
208 
209 
215  def create(self, module,name=None):
216  if name != None:
217  rtc = findRTC(name)
218  if rtc != None:
219  print 'RTC named "',name,'" already exists.'
220  return rtc
221  args = module
222  if name != None:
223  args += '?instance_name=' + name
224  ref = self.ref.create_component(args)
225  if ref == None:
226  return None
227  else:
228  return RTcomponent(ref)
229 
230 
233  def get_factory_names(self):
234  fs = []
235  fps = self.ref.get_factory_profiles()
236  for afp in fps:
237  for p in afp.properties:
238  if p.name == "implementation_id":
239  fs.append(p.value.extract_string())
240  return fs
241 
242 
245  def get_components(self):
246  cs = []
247  crefs = self.ref.get_components()
248  for cref in crefs:
249  c = RTcomponent(cref)
250  cs.append(c)
251  return cs
252 
253 
255  def restart(self):
256  self.ref.shutdown()
257  time.sleep(1)
258 
263 def unbindObject(name, kind):
264  nc = NameComponent(name, kind)
265  path = [nc]
266  rootnc.unbind(path)
267  return None
268 
269 
272 def initCORBA():
273  global rootnc, nshost, orb
274  props = System.getProperties()
275 
276  args = string.split(System.getProperty("NS_OPT"))
277  nshost = System.getProperty("NS_OPT").split(':')[2]
278  if nshost == "localhost" or nshost == "127.0.0.1":
279  nshost = socket.gethostname()
280  print 'nshost =',nshost
281  orb = ORB.init(args, props)
282 
283  nameserver = orb.resolve_initial_references("NameService");
284  rootnc = NamingContextHelper.narrow(nameserver);
285  return None
286 
287 
292 def getRootNamingContext(corbaloc):
293  props = System.getProperties()
294 
295  args = ["-ORBInitRef", corbaloc]
296  orb = ORB.init(args, props)
297 
298  nameserver = orb.resolve_initial_references("NameService");
299  return NamingContextHelper.narrow(nameserver);
300 
301 
308 def findObject(name, kind="", rnc=None):
309  nc = NameComponent(name,kind)
310  path = [nc]
311  if not rnc:
312  rnc = rootnc
313  return rnc.resolve(path)
314 
315 
321 def findRTCmanager(hostname=None, rnc=None):
322  if not hostname:
323  hostname = nshost
324  try:
325  try:
326  cxt = findObject(hostname, "host_cxt", rnc)
327  except:
328  hostname = socket.gethostbyaddr(hostname)[0]
329  cxt = findObject(hostname, "host_cxt", rnc)
330  obj = findObject("manager","mgr",cxt)
331  return RTCmanager(ManagerHelper.narrow(obj))
332  except:
333  print "exception in findRTCmanager("+hostname+")"
334 
335 
341 def findRTC(name, rnc=None):
342  try:
343  obj = findObject(name, "rtc", rnc)
344  rtc = RTcomponent(RTObjectHelper.narrow(obj))
345  cxts = rtc.ref.get_participating_contexts()
346  if len(cxts) > 0:
347  rtc.ec = cxts[0]
348  return rtc
349  except:
350  return None
351 
352 
358 def findPort(rtc, name):
359  ports = rtc.get_ports()
360  cprof = rtc.get_component_profile()
361  portname = cprof.instance_name+"."+name
362  for p in ports:
363  prof = p.get_port_profile()
364  if prof.name == portname:
365  return p
366  return None
367 
368 
373 def serializeComponents(rtcs, stopEC=True):
374  if len(rtcs) < 2:
375  return
376  ec = rtcs[0].ec
377  for rtc in rtcs[1:]:
378  if ec != rtc.ec:
379  if stopEC:
380  rtc.ec.stop()
381  if ec.add_component(rtc.ref) == ReturnCode_t.RTC_OK:
382  rtc.ec = ec
383  else:
384  print 'error in add_component()'
385  else:
386  print 'already serialized'
387 
388 
392 def isConnected(outP, inP):
393  op = outP.get_port_profile()
394  for con_prof in op.connector_profiles:
395  ports = con_prof.ports
396  if len(ports) == 2 and outP == ports[0] and inP == ports[1]:
397  return True
398  return False
399 
400 
404 def disconnectPorts(outP, inP):
405  op = outP.get_port_profile()
406  for con_prof in op.connector_profiles:
407  ports = con_prof.ports
408  if len(ports) == 2 and outP == ports[0] and inP == ports[1]:
409  outP.disconnect(con_prof.connector_id)
410  return
411 
412 
416 def dataTypeOfPort(port):
417  prof = port.get_port_profile()
418  prop = prof.properties
419  for p in prop:
420  if p.name == "dataport.data_type":
421  return p.value.extract_string()
422  return None
423 
424 
433 def connectPorts(outP, inPs, subscription="flush", dataflow="Push", bufferlength=1, rate=1000):
434  if not isinstance(inPs, list):
435  inPs = [inPs]
436  for inP in inPs:
437  if isConnected(outP, inP) == True:
438  print outP.get_port_profile().name,'and',inP.get_port_profile().name,'are already connected'
439  continue
440  if dataTypeOfPort(outP) != dataTypeOfPort(inP):
441  print outP.get_port_profile().name,'and',inP.get_port_profile().name,'have different data types'
442  continue
443 
444  con_prof = ConnectorProfile()
445  con_prof.connector_id = ""
446  con_prof.name = "connector0"
447  con_prof.ports = [outP, inP]
448  #
449  nv1 = NameValue()
450  nv1.name = "dataport.interface_type";
451  a1 = orb.create_any()
452  a1.insert_string("corba_cdr")
453  nv1.value = a1
454  #
455  nv2 = NameValue()
456  nv2.name = "dataport.dataflow_type"
457  a2 = orb.create_any()
458  a2.insert_string(dataflow)
459  nv2.value = a2
460  #
461  nv3 = NameValue()
462  nv3.name = "dataport.subscription_type"
463  a3 = orb.create_any()
464  a3.insert_string(subscription)
465  nv3.value = a3
466  #
467  nv4 = NameValue()
468  nv4.name = "dataport.buffer.length"
469  a4 = orb.create_any()
470  a4.insert_string(str(bufferlength))
471  nv4.value = a4
472  #
473  nv5 = NameValue()
474  nv5.name = "dataport.publisher.push_rate"
475  a5 = orb.create_any()
476  a5.insert_string(str(rate))
477  nv5.value = a5
478  #
479  con_prof.properties = [nv1, nv2, nv3, nv4, nv5]
480  con_prof_holder = ConnectorProfileHolder()
481  con_prof_holder.value = con_prof
482  if inP.connect(con_prof_holder) != ReturnCode_t.RTC_OK:
483  print "failed to connect(",outP.get_port_profile().name,'<->',inP.get_port_profile().name,")"
484  continue
485  # confirm connection
486  if isConnected(outP, inP) == False:
487  print "connet() returned RTC_OK, but not connected"
488 
489 
494 def data2cdr(data):
495  holder = Class.forName(data.getClass().getCanonicalName()+"Holder",
496  True, data.getClass().getClassLoader())
497  streamable = holder.newInstance()
498  #field = holder.getField("value")
499  #field.set(streamable, data)
500  streamable.value = data;
501  strm = EncapsOutputStream(orb, True)
502  streamable._write(strm)
503  return strm.toByteArray()
504 
505 
511 def cdr2data(cdr, classname):
512  ostrm = EncapsOutputStream(orb, True)
513  ostrm.write_octet_array(cdr, 0, len(cdr))
514  istrm = ostrm.create_input_stream()
515  holder = Class.forName("RTC."+classname+"Holder", True, Manager.getClassLoader())
516  streamable = holder.newInstance()
517  streamable._read(istrm)
518  return streamable.value
519 
520 
526 def writeDataPort(port, data, tm=1.0):
527  con_prof = ConnectorProfile()
528  con_prof.connector_id = ""
529  con_prof.name = "connector0"
530  con_prof.ports = [port]
531  #
532  nv1 = NameValue()
533  nv1.name = "dataport.interface_type";
534  a1 = orb.create_any()
535  a1.insert_string("corba_cdr")
536  nv1.value = a1
537  #
538  nv2 = NameValue()
539  nv2.name = "dataport.dataflow_type"
540  a2 = orb.create_any()
541  a2.insert_string("Push")
542  nv2.value = a2
543  #
544  nv3 = NameValue()
545  nv3.name = "dataport.subscription_type"
546  a3 = orb.create_any()
547  a3.insert_string("flush")
548  nv3.value = a3
549  #
550  con_prof.properties = [nv1, nv2, nv3]
551  con_prof_holder = ConnectorProfileHolder()
552  con_prof_holder.value = con_prof
553  if port.connect(con_prof_holder) != ReturnCode_t.RTC_OK:
554  print "failed to connect"
555  return None
556  for p in con_prof_holder.value.properties:
557  if p.name == 'dataport.corba_cdr.inport_ior':
558  ior = p.value.extract_string()
559  obj = orb.string_to_object(ior)
560  inport = InPortCdrHelper.narrow(obj)
561  cdr = data2cdr(data)
562  if inport.put(cdr) != PortStatus.PORT_OK:
563  print "failed to put"
564  time.sleep(tm)
565  port.disconnect(con_prof_holder.value.connector_id)
566  return None
567 
568 
569 
574 def readDataPort(port, timeout = 1.0):
575  pprof = port.get_port_profile()
576  for prop in pprof.properties:
577  if prop.name == "dataport.data_type":
578  classname = prop.value.extract_string()
579  break;
580  con_prof = ConnectorProfile()
581  con_prof.connector_id = ""
582  con_prof.name = "connector0"
583  con_prof.ports = [port]
584  #
585  nv1 = NameValue()
586  nv1.name = "dataport.interface_type";
587  a1 = orb.create_any()
588  a1.insert_string("corba_cdr")
589  nv1.value = a1
590  #
591  nv2 = NameValue()
592  nv2.name = "dataport.dataflow_type"
593  a2 = orb.create_any()
594  a2.insert_string("Pull")
595  nv2.value = a2
596  #
597  nv3 = NameValue()
598  nv3.name = "dataport.subscription_type"
599  a3 = orb.create_any()
600  a3.insert_string("flush")
601  nv3.value = a3
602  #
603  con_prof.properties = [nv1, nv2, nv3]
604  con_prof_holder = ConnectorProfileHolder()
605  con_prof_holder.value = con_prof
606  if port.connect(con_prof_holder) != ReturnCode_t.RTC_OK:
607  print "failed to connect"
608  return None
609  for p in con_prof_holder.value.properties:
610  #print p.name
611  if p.name == 'dataport.corba_cdr.outport_ior':
612  ior = p.value.extract_string()
613  obj = orb.string_to_object(ior)
614  outport = OutPortCdrHelper.narrow(obj)
615  cdr = CdrDataHolder()
616  tm = 0
617  while tm < timeout:
618  try:
619  outport.get(cdr)
620  if len(cdr.value) > 0:
621  port.disconnect(con_prof_holder.value.connector_id)
622  return cdr2data(cdr.value, classname)
623  except:
624  pass
625  time.sleep(0.1)
626  tm = tm + 0.1
627 
628  port.disconnect(con_prof_holder.value.connector_id)
629  return None
630 
631 
632 
633 
641 def findService(rtc, port_name, type_name, instance_name):
642  if port_name == "":
643  prof = rtc.ref.get_component_profile()
644  #print "RTC name:",prof.instance_name
645  port_prof = prof.port_profiles
646  else:
647  p = rtc.port(port_name)
648  if p == None:
649  print "can't find a port named",port_name
650  return None
651  else:
652  port_prof = [p.get_port_profile()]
653  port = None
654  for pp in port_prof:
655  #print "name:",pp.name
656  ifs = pp.interfaces
657  for aif in ifs:
658  #print "IF name:",aif.instance_name
659  #print "IF type:",aif.type_name
660  if aif.instance_name == instance_name and (type_name == "" or aif.type_name == type_name):
661  port = pp.port_ref
662  if port == None:
663  print "can't find a service named",instance_name
664  return None
665  #print port
666  con_prof = ConnectorProfile()
667  con_prof.name = "noname"
668  con_prof.connector_id = ""
669  con_prof.ports = [port]
670  con_prof.properties = []
671  con_prof_holder = ConnectorProfileHolder()
672  con_prof_holder.value = con_prof
673  port.connect(con_prof_holder)
674  ior = con_prof_holder.value.properties[0].value.extract_string()
675  port.disconnect(con_prof_holder.value.connector_id)
676  return orb.string_to_object(ior)
677 
678 
683 def setConfiguration(rtc, nvlist):
684  cfg = rtc.get_configuration()
685  cfgsets = cfg.get_configuration_sets()
686  if len(cfgsets) == 0:
687  print "configuration set is not found"
688  return
689  cfgset = cfgsets[0]
690  for nv in nvlist:
691  name = nv[0]
692  value = nv[1]
693  found = False
694  for d in cfgset.configuration_data:
695  if d.name == name:
696  d.value.insert_string(value)
697  cfg.set_configuration_set_values(cfgset)
698  found = True
699  break;
700  if not found:
701  print "no such property(",name,")"
702  cfg.activate_configuration_set('default')
703 
704 
709 def narrow(ior, klass, package="OpenHRP"):
710  return getattr(sys.modules[package], klass+"Helper").narrow(ior)
711 
712 
715 def isJython():
716  return sys.version.count("GCC") == 0
717 
718 initCORBA()
def isJython()
check if jython or python
Definition: jython/rtm.py:715
def start(self, ec=None)
activate this component
Definition: jython/rtm.py:122
def get_components(self)
get list of components
Definition: jython/rtm.py:245
def getLifeCycleState(self, ec=None)
get life cycle state of the main execution context
Definition: jython/rtm.py:147
def isActive(self, ec=None)
check the main execution context is active or not
Definition: jython/rtm.py:160
def findRTC(name, rnc=None)
get RT component
Definition: jython/rtm.py:341
def __init__(self, ref)
constructor
Definition: jython/rtm.py:187
def getRootNamingContext(corbaloc)
get root naming context
Definition: jython/rtm.py:292
def narrow(ior, klass, package="OpenHRP")
narrow ior
Definition: jython/rtm.py:709
wrapper class of RTCmanager
Definition: jython/rtm.py:181
def writeDataPort(port, data, tm=1.0)
write data to a data port
Definition: jython/rtm.py:526
def findService(rtc, port_name, type_name, instance_name)
get a service of RT component
Definition: jython/rtm.py:641
def cdr2data(cdr, classname)
convert data from CDR format
Definition: jython/rtm.py:511
def name(self)
get instance name
Definition: jython/rtm.py:174
def isInactive(self, ec=None)
check the main execution context is inactive or not
Definition: jython/rtm.py:168
def unbindObject(name, kind)
unbind an object reference
Definition: jython/rtm.py:263
def properties(self)
show list of property names and values
Definition: jython/rtm.py:107
def setConfiguration(rtc, nvlist)
update default configuration set
Definition: jython/rtm.py:683
def stop(self, ec=None)
deactivate this component
Definition: jython/rtm.py:134
def readDataPort(port, timeout=1.0)
read data from a data port
Definition: jython/rtm.py:574
def serializeComponents(rtcs, stopEC=True)
set up execution context of the first RTC so that RTCs are executed sequentially
Definition: jython/rtm.py:373
def get_factory_names(self)
get list of factory names
Definition: jython/rtm.py:233
def isConnected(outP, inP)
check two ports are connected or not
Definition: jython/rtm.py:392
def data2cdr(data)
convert data into CDR format
Definition: jython/rtm.py:494
def findRTCmanager(hostname=None, rnc=None)
get RTCmanager
Definition: jython/rtm.py:321
def connectPorts(outP, inPs, subscription="flush", dataflow="Push", bufferlength=1, rate=1000)
connect ports
Definition: jython/rtm.py:433
wrapper class of RT component
Definition: jython/rtm.py:32
def findObject(name, kind="", rnc=None)
get IOR of the object
Definition: jython/rtm.py:308
def restart(self)
restart Manager
Definition: jython/rtm.py:255
def findPort(rtc, name)
get a port of RT component
Definition: jython/rtm.py:358
def setProperty(self, name, value)
update value of the default configuration set
Definition: jython/rtm.py:84
def create(self, module, name=None)
create an instance of RT component
Definition: jython/rtm.py:215
def port(self, name)
get IOR of port
Definition: jython/rtm.py:54
def load(self, basename)
load RT component factory
Definition: jython/rtm.py:201
def __init__(self, ref)
constructor
Definition: jython/rtm.py:38
def service(self, instance_name, type_name="", port_name="")
get IOR of the service
Definition: jython/rtm.py:69
def disconnectPorts(outP, inP)
disconnect ports
Definition: jython/rtm.py:404
def dataTypeOfPort(port)
get data type of a port
Definition: jython/rtm.py:416
def getProperty(self, name)
get value of the property in the default configuration set
Definition: jython/rtm.py:92
def setConfiguration(self, nvlist)
update default configuration set
Definition: jython/rtm.py:76
def initCORBA()
initialize ORB
Definition: jython/rtm.py:272


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:51