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