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


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:06