rtc_handle.py
Go to the documentation of this file.
00001 #/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 # -*- Python -*-
00004 
00005 import sys
00006 from omniORB import CORBA, URI
00007 from omniORB import any
00008 
00009 import OpenRTM_aist
00010 import RTC
00011 
00012 
00013 from CorbaNaming import *
00014 import SDOPackage
00015 
00016 # class RtmEnv :
00017 #       rtm environment manager
00018 #       orb, naming service, rtc proxy list
00019 #
00020 class RtmEnv : 
00021 
00022     def __init__(self, orb_args, nserver_names=["localhost"], 
00023                                      orb=None, naming=None):
00024         if not orb :
00025             orb = CORBA.ORB_init(orb_args)
00026         self.orb = orb
00027         self.name_space = {}
00028         if naming : # naming can specify only one naming service
00029             self.name_space['default']=NameSpace(orb, naming=naming)
00030         else :
00031             for ns in nserver_names :
00032               self.name_space[ns]=NameSpace(orb, server_name=ns)
00033 
00034     def __del__(self):
00035         self.orb.shutdown(wait_for_completion=CORBA.FALSE)
00036         self.orb.destroy()
00037 #
00038 # class NameSpace :
00039 #       rtc_handles and object list in naming service
00040 #
00041 class NameSpace :
00042     def __init__(self, orb, server_name=None, naming=None):
00043         self.orb = orb
00044         self.name = server_name
00045         if naming :
00046           self.naming = naming
00047         else :
00048           self.naming = CorbaNaming(self.orb, server_name) 
00049 
00050         self.b_len = 10 # iteration cut off no.
00051         self.rtc_handles = {}
00052         self.obj_list = {}
00053 
00054     def get_object_by_name(self, name, cl=RTC.RTObject):
00055         ref = self.naming.resolveStr(name)
00056         if ref is None: return None     # return CORBA.nil ?
00057         if cl :
00058             return ref._narrow(cl)
00059         else :
00060             return ref
00061 
00062     def list_obj(self) :
00063         self.rtc_handes = {}
00064         self.obj_list = {}
00065         return self.list_obj1(self.naming._rootContext, "")
00066 
00067     def list_obj1(self, name_context, parent) :
00068         if not name_context :
00069             name_context = self.naming._rootContext 
00070         rslt = []
00071         b_list = name_context.list(self.b_len)
00072         for bd in b_list[0] :
00073             rslt = rslt + self.proc_bd(bd, name_context, parent)
00074         if b_list[1] :  # iterator : there exists remaining.
00075             t_list = b_list[1].next_n(self.b_len)
00076             while t_list[0] :
00077                 for bd in t_list[1] :
00078                     rslt = rslt + self.proc_bd(bd, name_context, parent)
00079                 t_list = b_list[1].next_n(self.b_len)
00080         return rslt
00081 
00082     def proc_bd(self, bd, name_context, parent) :
00083 #        print '-------------------------------------------------------------------'
00084 #        print 'bd= ', bd
00085 #        print 'name_context= ', name_context
00086 #        print 'parent= ', parent
00087         rslt = []
00088         pre = ""
00089         if parent :
00090             pre = parent + "/"
00091         nam = pre + URI.nameToString(bd.binding_name)
00092         if bd.binding_type == CosNaming.nobject :
00093             tmp = name_context.resolve(bd.binding_name)
00094             self.obj_list[nam]=tmp
00095             print 'objcet '+nam+' was listed.'
00096             try :
00097                 tmp = tmp._narrow(RTC.RTObject)
00098             except :
00099                 print nam+' is not RTC.'
00100                 tmp = None
00101             try :
00102                 if tmp :
00103                    rslt = [[nam, tmp]]
00104                    self.rtc_handles[nam]=RtcHandle(nam,self,tmp)
00105                    print 'handle for '+nam+' was created.'
00106                 else :
00107                    pass
00108             except :
00109                 print nam+' is not alive.'
00110                 pass
00111         else :
00112             tmp = name_context.resolve(bd.binding_name)
00113             tmp = tmp._narrow(CosNaming.NamingContext)
00114             rslt = self.list_obj1(tmp, nam)
00115         return rslt
00116 
00117 #
00118 # data conversion
00119 #
00120 def nvlist2dict(nvlist) :
00121     rslt = {}
00122     for tmp in nvlist :
00123         rslt[tmp.name]=tmp.value.value()   # nv.value and any.value()
00124     return rslt
00125 def dict2nvlist(dict) :
00126     rslt = []
00127     for tmp in dict.keys() :
00128         rslt.append(SDOPackage.NameValue(tmp, any.to_any(dict[tmp])))
00129     return rslt
00130 #
00131 #  connector, port, inport, outport, service
00132 #                
00133 
00134 class Connector :
00135     def __init__(self, plist, name = None, id="", prop_dict={}) :
00136        self.plist = plist
00137        self.port_reflist = [tmp.port_profile.port_ref for tmp in plist]
00138        if name :
00139            self.name = name
00140        else :
00141            self.name = string.join([tmp.name for tmp in plist],'_')
00142        self.prop_dict = prop_dict
00143        self.prop_nvlist = dict2nvlist(self.prop_dict)
00144        self.profile = RTC.ConnectorProfile(self.name, id, self.port_reflist, self.prop_nvlist)
00145        self.nego_prop()
00146 
00147     def nego_prop(self) :
00148        self.possible = True
00149        for kk in self.def_prop :
00150            if kk in self.prop_dict :
00151                if not self.prop_dict[kk] :
00152                    self.prop_dict[kk]=self.def_prop[kk]
00153            else :
00154                 self.prop_dict[kk]=self.def_prop[kk]
00155            for pp in self.plist :  
00156                if not ((self.prop_dict[kk] in pp.prop[kk]) or 
00157                                  ('Any' in    pp.prop[kk])) :
00158                    self.prop_dict[kk] = ""
00159                    self.possible = False
00160        self.prop_nvlist = dict2nvlist(self.prop_dict)
00161        self.profile.properties = self.prop_nvlist
00162        return self.possible
00163 
00164     def connect(self) :
00165 #
00166 #   out and inout parameters are retuned as a tuple   
00167 #
00168        ret, self.profile = self.port_reflist[0].connect(self.profile)
00169        self.prop_nvlist = self.profile.properties
00170        self.prop_dict = nvlist2dict(self.prop_nvlist)
00171        return ret
00172 
00173     def disconnect(self) :
00174        ret = self.port_reflist[0].disconnect(self.profile.connector_id)
00175        return ret
00176 
00177 class IOConnector(Connector) :
00178     def __init__(self, plist, name = None, id="", prop_dict={}) :
00179 #      self.def_prop = {'dataport.dataflow_type':'Push' ,
00180 #                       'dataport.interface_type':'CORBA_Any' ,
00181 #                       'dataport.subscription_type':'Flush'}
00182        self.def_prop = {'dataport.dataflow_type':'push' ,
00183                         'dataport.interface_type':'corba_cdr' ,
00184                         'dataport.subscription_type':'flush'}
00185        Connector.__init__(self, plist, name, id, prop_dict)
00186 
00187 class ServiceConnector(Connector) :
00188     def __init__(self, plist, name = None, id="", prop_dict={}) :
00189        self.def_prop = {'port.port_type':'CorbaPort' }
00190        Connector.__init__(self, plist, name, id, prop_dict)
00191 
00192 
00193 class Port :
00194     def __init__(self, profile,nv_dict=None) :
00195         self.name=profile.name    
00196         self.port_profile = profile
00197         if not nv_dict :
00198             nv_dict = nvlist2dict(profile.properties)
00199         self.prop = nv_dict
00200         self.con = None           # this must be set in each subclasses
00201     def get_info(self) :
00202         self.con.connect()
00203         tmp1 = self.get_connections()
00204         tmp2 = [pp.connector_id for pp in tmp1]
00205         if self.con.profile.connector_id in tmp2 :
00206             self.con.disconnect()
00207 
00208     def get_connections(self) :
00209         return self.port_profile.port_ref.get_connector_profiles()
00210 
00211 class CorbaServer :
00212     def __init__(self, profile, port) :
00213         self.profile = profile
00214         self.port = port
00215         self.name = profile.instance_name
00216         self.type = profile.type_name
00217         self.ref = None
00218         ref_key = 'port.' + self.type + '.' + self.name
00219         self.ref=self.port.con.prop_dict[ref_key]
00220 #
00221 # if we import stubs before we create instances,
00222 # we rarely need to narrow the object references.
00223 # we need to specify global symbol table to evaluate class symbols.
00224 #
00225     def narrow_ref(self, gls) :
00226         if self.type.find('::') == -1 :
00227             self.narrow_sym = eval('_GlobalIDL.' + self.type, gls)
00228         else :
00229             self.narrow_sym = eval(self.type.replace('::','.'), gls)
00230         self.ref = self.ref._narrow(self.narrow_sym)
00231 
00232 class CorbaClient :
00233     def __init__(self, profile) :
00234         self.profile = profile
00235         self.name = profile.instance_name
00236         self.type = profile.type_name
00237 #
00238 # to connect to an outside corba client,
00239 # we need an implementation of the corresponding corba server.
00240 # but ....
00241 #
00242 
00243 class RtcService(Port) :
00244     def __init__(self, profile,nv_dict=None) :
00245         Port.__init__(self, profile, nv_dict)
00246         self.con = ServiceConnector([self])
00247         self.get_info()
00248         self.provided={}
00249         self.required={}
00250         tmp = self.port_profile.interfaces
00251         for itf in tmp :
00252             if itf.polarity == RTC.PROVIDED :
00253                 self.provided[itf.instance_name] = CorbaServer(itf,self)
00254             elif itf.polarity == RTC.REQUIRED :
00255                 self.required[itf.instance_name] = CorbaClient(itf)
00256 
00257 class RtcInport(Port) :
00258     def __init__(self, profile, nv_dict=None) :
00259         Port.__init__(self, profile, nv_dict)
00260         self.con = IOConnector([self])
00261         self.get_info() 
00262 #       self.ref = self.con.prop_dict['dataport.corba_any.inport_ref']
00263         self.ref = self.con.prop_dict['dataport.corba_cdr.inport_ref']
00264         self.data_class = eval('RTC.' + self.prop['dataport.data_type'])
00265         self.data_tc = eval('RTC._tc_' + self.prop['dataport.data_type'])
00266     def write(self,data) :
00267         self.ref.put(CORBA.Any(self.data_tc,
00268                          self.data_class(RTC.Time(0,0),data)))
00269 
00270 class RtcOutport(Port) :
00271     def __init__(self, profile,nv_dict=None) :
00272         Port.__init__(self, profile, nv_dict)
00273         self.con = IOConnector([self])
00274         self.get_info()
00275         if 'dataport.corba_any.outport_ref' in self.con.prop_dict :
00276            self.ref = self.con.prop_dict['dataport.corba_cdr.outport_ref']
00277 #          self.ref = self.con.prop_dict['dataport.corba_any.outport_ref']
00278         else :
00279            self.ref=None
00280         self.data_class = eval('RTC.' + self.prop['dataport.data_type'])
00281         self.data_tc = eval('RTC._tc_' + self.prop['dataport.data_type'])
00282 
00283     def read(self) :
00284         if self.ref :
00285            return self.ref.get().value()
00286         else :
00287            print "not supported"
00288            return None
00289 #
00290 # RtcHandle
00291 #
00292 class RtcHandle :
00293   def __init__(self, name, env, ref=None) :
00294     self.name = name
00295     self.env = env
00296     if ref :
00297         self.rtc_ref = ref
00298     else :
00299         self.rtc_ref = env.naming.resolve(name)._narrow(RTC.RTObject)
00300     self.conf_ref = None
00301     self.retrieve_info()
00302 
00303   def retrieve_info(self) :
00304     self.conf_set={}
00305     self.conf_set_data={}
00306     self.port_refs = []
00307     self.execution_contexts =[]
00308     if self.rtc_ref :
00309         self.conf_ref = self.rtc_ref.get_configuration()
00310         conf_set = self.conf_ref.get_configuration_sets()
00311         for cc in conf_set :
00312             self.conf_set[cc.id]=cc
00313             self.conf_set_data[cc.id]=nvlist2dict(cc.configuration_data)
00314         self.profile = self.rtc_ref.get_component_profile()
00315         self.prop = nvlist2dict(self.profile.properties)
00316         #self.execution_contexts = self.rtc_ref.get_contexts()
00317         self.execution_contexts = self.rtc_ref.get_owned_contexts()
00318         self.port_refs = self.rtc_ref.get_ports()  
00319     # this includes inports, outports and service ports
00320     self.ports = {}
00321     self.services = {}
00322     self.inports = {}
00323     self.outports = {}
00324     for pp in self.port_refs :
00325         tmp = pp.get_port_profile()
00326         tmp_prop = nvlist2dict(tmp.properties)
00327 #       self.ports[tmp.name]=Port(tmp, tmp_prop)
00328         if tmp_prop['port.port_type']=='DataInPort' :
00329             self.inports[tmp.name]=RtcInport(tmp,tmp_prop)
00330 #            self.inports[tmp.name]=Port(tmp, tmp_prop)
00331         elif tmp_prop['port.port_type']=='DataOutPort' :
00332             self.outports[tmp.name]=RtcOutport(tmp, tmp_prop)
00333 #            self.outports[tmp.name]=Port(tmp, tmp_prop)
00334         elif tmp_prop['port.port_type']=='CorbaPort' :
00335             self.services[tmp.name]=RtcService(tmp, tmp_prop)
00336 #            self.services[tmp.name]=Port(tmp, tmp_prop)
00337 
00338   def set_conf(self,conf_set_name,param_name,value) :
00339       conf_set=self.conf_set[conf_set_name]
00340       conf_set_data=self.conf_set_data[conf_set_name]
00341       conf_set_data[param_name]=value
00342       conf_set.configuration_data=dict2nvlist(conf_set_data)
00343       self.conf_ref.set_configuration_set_values(conf_set_name,conf_set)
00344   def set_conf_activate(self,conf_set_name,param_name,value) :
00345       self.set_conf(conf_set_name,param_name,value)
00346       self.conf_ref.activate_configuration_set(conf_set_name)
00347   def activate(self):
00348       return self.execution_contexts[0].activate_component(self.rtc_ref)
00349   def deactivate(self):
00350       return self.execution_contexts[0].deactivate_component(self.rtc_ref)
00351 #
00352 # pipe
00353 #  a pipe is an port (interface & implementation)
00354 #  whhich corresponds to an outside port interface.
00355 #  you can subscribe and communicate to the outside port with the pipe.
00356 #  you need an rtc implementation to use pipes.
00357 #
00358 class Pipe :
00359     pass
00360 
00361 class PipeOut(Pipe):
00362     def __init__(self, name, data_buf, size=8) :
00363         self.name =  name
00364         self.data =  data_buf
00365         self.OpenRTM_aist.InPort(name,data_buf,OpenRTM_aist.RingBuffer(size))


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Aug 27 2015 14:17:28