Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 import sys
00020 import omniORB.CORBA as CORBA
00021 import CosNaming
00022
00023 class NSHelper:
00024 def __init__(self):
00025 self.orb = None
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 def Connect(self, name_server):
00036 try:
00037 if self.orb != None:
00038 self.orb.destroy()
00039 arg = ["-ORBInitRef",
00040 "NameService=corbaname::",
00041 "-ORBclientCallTimeOutPeriod",
00042 "2000"]
00043 arg[1] = arg[1] + name_server
00044 self.orb = CORBA.ORB_init(arg)
00045 obj = self.orb.resolve_initial_references("NameService")
00046 self.root_cxt = obj._narrow(CosNaming.NamingContext)
00047 except:
00048 self.root_cxt = None
00049
00050
00051 def GetNSDict(self):
00052 ns_dict = None
00053 if self.root_cxt != None:
00054 ns_dict = self.__GetNameTreeRecursive__(self.root_cxt)
00055 return ns_dict
00056
00057 def __GetNameTreeRecursive__(self, cxt):
00058 ns_dict = {}
00059 try:
00060 cur_cxt = cxt.list(100)
00061 except:
00062 except_mess("cxt.list method error!:")
00063 return
00064
00065 for bc in cur_cxt[0]:
00066 if bc != None:
00067 try:
00068 cur_obj = cxt.resolve(bc.binding_name)
00069 dict_key = bc.binding_name[0].id \
00070 + "|" \
00071 + bc.binding_name[0].kind
00072 if bc.binding_type == CosNaming.ncontext:
00073 ns_dict[dict_key] = (
00074 {
00075 "objref":cur_obj,
00076 "id":bc.binding_name[0].id,
00077 "kind":bc.binding_name[0].kind,
00078 "bname":bc.binding_name
00079 },
00080 self.__GetNameTreeRecursive__(cur_obj))
00081 elif bc.binding_type == CosNaming.nobject:
00082 ns_dict[dict_key] = (cur_obj, None)
00083 ns_dict[dict_key] = (
00084 {
00085 "objref":cur_obj,
00086 "id":bc.binding_name[0].id,
00087 "kind":bc.binding_name[0].kind,
00088 "bname":bc.binding_name
00089 },None)
00090 except:
00091 except_mess("context method error!:")
00092 ns_dict = {}
00093 break
00094
00095 return ns_dict
00096
00097 def DeleteToContext(self,cxt,bname):
00098 if cxt != None:
00099 try:
00100 cxt.unbind(bname)
00101 except:
00102 except_mess('contex not found:')
00103
00104
00105 if __name__ == '__main__':
00106 import sys
00107 nsh = RtmNSHelper()
00108 if len(sys.argv) == 1:
00109 print sys.argv[0] + " [CosNameService Host Name]"
00110 sys.exit(1)
00111
00112 nsh.Connect(sys.argv[1])
00113 print nsh.GetNSDict()
00114