CorbaPort.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: euc-jp -*-
3 
4 
17 
18 from omniORB import CORBA
19 from omniORB import any
20 import traceback
21 import sys
22 
23 import OpenRTM_aist
24 import RTC
25 
26 
27 
28 
605  """
606  """
607 
608 
622  def __init__(self, name):
623  OpenRTM_aist.PortBase.__init__(self, name)
624  self.addProperty("port.port_type", "CorbaPort")
626  self._providers = []
627  self._consumers = []
628  return
629 
630 
631  def __del__(self, PortBase=OpenRTM_aist.PortBase):
632  PortBase.__del__(self)
633 
634 
635 
663  def init(self, prop):
664  self._rtcout.RTC_TRACE("init()")
665 
666  self._properties.mergeProperties(prop)
667 
668  num = [-1]
669  if not OpenRTM_aist.stringTo([num],
670  self._properties.getProperty("connection_limit","-1")):
671  self._rtcout.RTC_ERROR("invalid connection_limit value: %s",
672  self._properties.getProperty("connection_limit"))
673 
674  self.setConnectionLimit(num[0])
675 
676 
677 
717  def registerProvider(self, instance_name, type_name, provider):
718  self._rtcout.RTC_TRACE("registerProvider(instance=%s, type_name=%s)",
719  (instance_name, type_name))
720 
721  try:
722  self._providers.append(self.CorbaProviderHolder(type_name,
723  instance_name,
724  provider))
725  except:
726  self._rtcout.RTC_ERROR("appending provider interface failed")
727  self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
728  return False
729 
730 
731  if not self.appendInterface(instance_name, type_name, RTC.PROVIDED):
732  return False
733 
734  return True
735 
736 
737 
780  def registerConsumer(self, instance_name, type_name, consumer):
781  self._rtcout.RTC_TRACE("registerConsumer()")
782 
783  if not self.appendInterface(instance_name, type_name, RTC.REQUIRED):
784  return False
785 
786  self._consumers.append(self.CorbaConsumerHolder(type_name,
787  instance_name,
788  consumer,
789  self))
790  return True
791 
792 
793 
811  for provider in self._providers:
812  provider.activate()
813 
814  return
815 
816 
817 
835  for provider in self._providers:
836  provider.deactivate()
837 
838  return
839 
840 
841 
842 
925  def publishInterfaces(self, connector_profile):
926  self._rtcout.RTC_TRACE("publishInterfaces()")
927 
928  returnvalue = self._publishInterfaces()
929 
930  if returnvalue != RTC.RTC_OK:
931  return returnvalue
932 
933  properties = []
934  for provider in self._providers:
935  #------------------------------------------------------------
936  # new version descriptor
937  # <comp_iname>.port.<port_name>.provided.<type_name>.<instance_name>
938  newdesc = self._profile.name[:len(self._ownerInstanceName)] + \
939  ".port" + self._profile.name[len(self._ownerInstanceName):]
940  newdesc += ".provided." + provider.descriptor()
941 
942  properties.append(OpenRTM_aist.NVUtil.newNV(newdesc, provider.ior()))
943 
944  #------------------------------------------------------------
945  # old version descriptor
946  # port.<type_name>.<instance_name>
947  olddesc = "port." + provider.descriptor()
948  properties.append(OpenRTM_aist.NVUtil.newNV(olddesc, provider.ior()))
949 
950  OpenRTM_aist.CORBA_SeqUtil.push_back_list(connector_profile.properties, properties)
951 
952  return RTC.RTC_OK
953 
954 
955 
1089  def subscribeInterfaces(self, connector_profile):
1090  self._rtcout.RTC_TRACE("subscribeInterfaces()")
1091  nv = connector_profile.properties
1092 
1093  strict = False # default is "best_effort"
1094  index = OpenRTM_aist.NVUtil.find_index(nv, "port.connection.strictness")
1095  if index >= 0:
1096  strictness = str(any.from_any(nv[index].value, keep_structs=True))
1097  if "best_effort" == strictness:
1098  strict = False
1099  elif "strict" == strictness:
1100  strict = True
1101 
1102  self._rtcout.RTC_DEBUG("Connetion strictness is: %s",strictness)
1103 
1104  for consumer in self._consumers:
1105  ior = []
1106  if self.findProvider(nv, consumer, ior) and len(ior) > 0:
1107  self.setObject(ior[0], consumer)
1108  continue
1109 
1110  ior = []
1111  if self.findProviderOld(nv, consumer, ior) and len(ior) > 0:
1112  self.setObject(ior[0], consumer)
1113  continue
1114 
1115  # never come here without error
1116  # if strict connection option is set, error is returned.
1117  if strict:
1118  self._rtcout.RTC_ERROR("subscribeInterfaces() failed.")
1119  return RTC.RTC_ERROR
1120 
1121  self._rtcout.RTC_TRACE("subscribeInterfaces() successfully finished.")
1122 
1123  return RTC.RTC_OK
1124 
1125 
1126 
1149  def unsubscribeInterfaces(self, connector_profile):
1150  self._rtcout.RTC_TRACE("unsubscribeInterfaces()")
1151  nv = connector_profile.properties
1152 
1153  for consumer in self._consumers:
1154  ior = []
1155  if self.findProvider(nv, consumer, ior) and len(ior) > 0:
1156  self._rtcout.RTC_DEBUG("Correspoinding consumer found.")
1157  self.releaseObject(ior[0], consumer)
1158  continue
1159 
1160  ior = []
1161  if self.findProviderOld(nv, consumer, ior) and len(ior) > 0:
1162  self._rtcout.RTC_DEBUG("Correspoinding consumer found.")
1163  self.releaseObject(ior[0], consumer)
1164  continue
1165 
1166  return
1167 
1168 
1169 
1202  def findProvider(self, nv, cons, iorstr):
1203  # new consumer interface descriptor
1204  newdesc = self._profile.name[:len(self._ownerInstanceName)] + \
1205  ".port" + self._profile.name[len(self._ownerInstanceName):]
1206  newdesc += ".required." + cons.descriptor()
1207 
1208  # find a NameValue of the consumer
1209  cons_index = OpenRTM_aist.NVUtil.find_index(nv, newdesc)
1210  if cons_index < 0:
1211  return False
1212 
1213  provider = str(any.from_any(nv[cons_index].value, keep_structs=True))
1214  if not provider:
1215  self._rtcout.RTC_WARN("Cannot extract Provider interface descriptor")
1216  return False
1217 
1218  # find a NameValue of the provider
1219  prov_index = OpenRTM_aist.NVUtil.find_index(nv, provider)
1220  if prov_index < 0:
1221  return False
1222 
1223  ior_ = str(any.from_any(nv[prov_index].value, keep_structs=True))
1224  if not ior_:
1225  self._rtcout.RTC_WARN("Cannot extract Provider IOR string")
1226  return False
1227 
1228  if isinstance(iorstr, list):
1229  iorstr.append(ior_)
1230 
1231  self._rtcout.RTC_ERROR("interface matched with new descriptor: %s", newdesc)
1232 
1233  return True
1234 
1235 
1236 
1273  def findProviderOld(self, nv, cons, iorstr):
1274  # old consumer interface descriptor
1275  olddesc = "port." + cons.descriptor()
1276 
1277  # find a NameValue of the provider same as olddesc
1278  index = OpenRTM_aist.NVUtil.find_index(nv, olddesc)
1279  if index < 0:
1280  return False
1281 
1282  ior_ = str(any.from_any(nv[index].value, keep_structs=True))
1283  if not ior_:
1284  self._rtcout.RTC_WARN("Cannot extract Provider IOR string")
1285  return False
1286 
1287  if isinstance(iorstr, list):
1288  iorstr.append(ior_)
1289 
1290  self._rtcout.RTC_ERROR("interface matched with old descriptor: %s", olddesc)
1291 
1292  return True
1293 
1294 
1295 
1323  def setObject(self, ior, cons):
1324  # if ior string is "null" or "nil", ignore it.
1325  if "null" == ior:
1326  return True
1327 
1328  if "nil" == ior:
1329  return True
1330 
1331  # IOR should be started by "IOR:"
1332  if "IOR:" != ior[:4]:
1333  return False
1334 
1335  # set IOR to the consumer
1336  if not cons.setObject(ior):
1337  self._rtcout.RTC_ERROR("Cannot narrow reference")
1338  return False
1339 
1340  self._rtcout.RTC_TRACE("setObject() done")
1341  return True
1342 
1343 
1370  def releaseObject(self, ior, cons):
1371  if ior == cons.getIor():
1372  cons.releaseObject()
1373  self._rtcout.RTC_DEBUG("Consumer %s released.", cons.descriptor())
1374  return True
1375 
1376  self._rtcout.RTC_WARN("IORs between Consumer and Connector are different.")
1377  return False
1378 
1379 
1394  # CorbaProviderHolder(const char* type_name,
1395  # const char* instance_name,
1396  # PortableServer::RefCountServantBase* servant)
1397  def __init__(self, type_name, instance_name, servant):
1398  self._typeName = type_name
1399  self._instanceName = instance_name
1400  self._servant = servant
1401  _mgr = OpenRTM_aist.Manager.instance()
1402  self._oid = _mgr.getPOA().servant_to_id(self._servant)
1403 
1404  obj = _mgr.getPOA().id_to_reference(self._oid)
1405  self._ior = _mgr.getORB().object_to_string(obj)
1406  self.deactivate()
1407  return
1408 
1409  def __del__(self):
1410  self.deactivate()
1411 
1412  # std::string instanceName() { return m_instanceName; }
1413  def instanceName(self):
1414  return self._instanceName
1415 
1416  # std::string typeName() { return m_typeName; }
1417  def typeName(self):
1418  return self._typeName
1419 
1420  # std::string ior() { return m_ior; }
1421  def ior(self):
1422  return self._ior
1423 
1424  # std::string descriptor() { return m_typeName + "." + m_instanceName; }
1425  def descriptor(self):
1426  return self._typeName + "." + self._instanceName
1427 
1428  # void activate()
1429  def activate(self):
1430  try:
1431  OpenRTM_aist.Manager.instance().getPOA().activate_object_with_id(self._oid, self._servant)
1432  except:
1433  print OpenRTM_aist.Logger.print_exception()
1434  return
1435 
1436  # void deactivate()
1437  def deactivate(self):
1438  try:
1439  OpenRTM_aist.Manager.instance().getPOA().deactivate_object(self._oid)
1440  except:
1441  print OpenRTM_aist.Logger.print_exception()
1442  return
1443 
1444 
1445 
1453  # CorbaConsumerHolder(const char* type_name,
1454  # const char* instance_name,
1455  # CorbaConsumerBase* consumer,
1456  # string& owner)
1457  def __init__(self, type_name, instance_name, consumer, owner):
1458  self._typeName = type_name
1459  self._instanceName = instance_name
1460  self._consumer = consumer
1461  self._owner = owner
1462  self._ior = ""
1463  return
1464 
1465  # std::string instanceName() { return m_instanceName; }
1466  def instanceName(self):
1467  return self._instanceName
1468 
1469  # std::string typeName() { return m_typeName; }
1470  def typeName(self):
1471  return self._typeName
1472 
1473  # std::string descriptor() { return m_typeName + "." + m_instanceName; }
1474  def descriptor(self):
1475  return self._typeName + "." + self._instanceName
1476 
1477 
1485  def setObject(self, ior):
1486  self._ior = ior
1487  orb = OpenRTM_aist.Manager.instance().getORB()
1488  obj = orb.string_to_object(ior)
1489  if CORBA.is_nil(obj):
1490  return False
1491 
1492  return self._consumer.setObject(obj)
1493 
1494 
1502  def releaseObject(self):
1503  self._consumer.releaseObject()
1504  return
1505 
1506  # const std::string& getIor()
1507  def getIor(self):
1508  return self._ior
1509 
1510 
1511 
1518  class subscribe:
1519  def __init__(self, cons):
1520  self._cons = cons
1521  self._len = len(cons)
1522 
1523  def __call__(self, nv):
1524  for i in range(self._len):
1525  name_ = nv.name
1526  if self._cons[i].descriptor() == name_:
1527  try:
1528  obj = any.from_any(nv.value, keep_structs=True)
1529  self._cons[i].setObject(obj)
1530  except:
1531  print OpenRTM_aist.Logger.print_exception()
1532 
1533 
1534 
1535 
1542  def __init__(self, cons):
1543  self._cons = cons
1544  self._len = len(cons)
1545 
1546  def __call__(self, nv):
1547  for i in range(self._len):
1548  name_ = nv.name
1549  if self._cons[i].descriptor() == name_:
1550  self._cons[i].releaseObject()
1551  return
1552 
1553  # for 0.4.x
1554  if "port."+self._cons[i].descriptor() == name_:
1555  self._cons[i].releaseObject()
1556 
1557 
def activateInterfaces(self)
Activate all Port interfaces.
Definition: CorbaPort.py:810
def publishInterfaces(self, connector_profile)
Publish information about interfaces.
Definition: CorbaPort.py:925
def unsubscribeInterfaces(self, connector_profile)
Unsubscribe interfaces.
Definition: CorbaPort.py:1149
Subscription mutching functor for Consumer.
Definition: CorbaPort.py:1518
def push_back_list(seq1, seq2)
RT Conponent CORBA service/consumer Port.
Definition: CorbaPort.py:604
def setObject(self, ior, cons)
Setting IOR to Consumer.
Definition: CorbaPort.py:1323
def addProperty(self, key, value)
Add NameValue data to PortProfile&#39;s properties.
Definition: PortBase.py:2184
def findProvider(self, nv, cons, iorstr)
Find out a provider corresponding to the consumer from NVList.
Definition: CorbaPort.py:1202
def deactivateInterfaces(self)
Deactivate all Port interfaces.
Definition: CorbaPort.py:834
The structure to be stored Provider information.
Definition: CorbaPort.py:1393
def releaseObject(self)
Releasing Consumer Objectvoid releaseObject()
Definition: CorbaPort.py:1502
def setConnectionLimit(self, limit_value)
Set the maximum number of connections.
Definition: PortBase.py:1760
def releaseObject(self, ior, cons)
Releasing Consumer Object.
Definition: CorbaPort.py:1370
The Properties class represents a persistent set of properties.
Definition: Properties.py:83
def subscribeInterfaces(self, connector_profile)
Subscribe to interface.
Definition: CorbaPort.py:1089
def __init__(self, name)
Constructor.
Definition: CorbaPort.py:622
def __init__(self, type_name, instance_name, consumer, owner)
Definition: CorbaPort.py:1457
def registerConsumer(self, instance_name, type_name, consumer)
Register the consumer.
Definition: CorbaPort.py:780
def appendInterface(self, instance_name, type_name, pol)
Append an interface to the PortInterfaceProfile.
Definition: PortBase.py:2108
def __del__(self, PortBase=OpenRTM_aist.PortBase)
Definition: CorbaPort.py:631
def __init__(self, type_name, instance_name, servant)
Definition: CorbaPort.py:1397
def findProviderOld(self, nv, cons, iorstr)
Find out a provider corresponding to the consumer from NVList.
Definition: CorbaPort.py:1273
def find_index(nv, name)
Definition: NVUtil.py:229
The structure to be stored Consumer information.
Definition: CorbaPort.py:1452
def _publishInterfaces(self)
Publish interface information.
Definition: PortBase.py:1786
def registerProvider(self, instance_name, type_name, provider)
Register the provider.
Definition: CorbaPort.py:717
def init(self, prop)
Initializing properties.
Definition: CorbaPort.py:663
def newNV(name, value)
Create NameVale.
Definition: NVUtil.py:50
def setObject(self, ior)
Setting IOR to Consumerbool setObject(const char* ior)
Definition: CorbaPort.py:1485
Functor to release Consumer&#39;s object.
Definition: CorbaPort.py:1541


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Jun 6 2019 19:11:34