NSHelper.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # @file RtmNSHelper.py
5 # @brief rtc-link name service access helper class
6 # @date $Date: 2005-05-12 09:06:19 $
7 # @author Noriaki Ando <n-ando@aist.go.jp>
8 #
9 # Copyright (C) 2004-2005
10 # Task-intelligence Research Group,
11 # Intelligent Systems Research Institute,
12 # National Institute of
13 # Advanced Industrial Science and Technology (AIST), Japan
14 # All rights reserved.
15 #
16 # $Id: RtmNSHelper.py 775 2008-07-28 16:14:45Z n-ando $
17 #
18 
19 import sys
20 import omniORB.CORBA as CORBA
21 import CosNaming
22 
23 class NSHelper:
24  def __init__(self):
25  self.orb = None
26 
27 # def __del__(self):
28 # if self.orb != None:
29 # try:
30 # self.orb.destroy()
31 # except:
32 # except_mess('CORBA Name Server: Destroy Error!! <' \
33 # + name_server + '>')
34 
35  def Connect(self, name_server):
36  try:
37  if self.orb != None:
38  self.orb.destroy()
39  arg = ["-ORBInitRef",
40  "NameService=corbaname::",
41  "-ORBclientCallTimeOutPeriod",
42  "2000"]
43  arg[1] = arg[1] + name_server
44  self.orb = CORBA.ORB_init(arg)
45  obj = self.orb.resolve_initial_references("NameService")
46  self.root_cxt = obj._narrow(CosNaming.NamingContext)
47  except:
48  self.root_cxt = None
49 # print 'CORBA Name Server: Connect Error!! <',name_server,'>'
50 
51  def GetNSDict(self):
52  ns_dict = None
53  if self.root_cxt != None:
54  ns_dict = self.__GetNameTreeRecursive__(self.root_cxt)
55  return ns_dict
56 
57  def __GetNameTreeRecursive__(self, cxt):
58  ns_dict = {}
59  try:
60  cur_cxt = cxt.list(100)
61  except:
62  except_mess("cxt.list method error!:")
63  return
64 
65  for bc in cur_cxt[0]:
66  if bc != None:
67  try:
68  cur_obj = cxt.resolve(bc.binding_name)
69  dict_key = bc.binding_name[0].id \
70  + "|" \
71  + bc.binding_name[0].kind
72  if bc.binding_type == CosNaming.ncontext:
73  ns_dict[dict_key] = (
74  {
75  "objref":cur_obj,
76  "id":bc.binding_name[0].id,
77  "kind":bc.binding_name[0].kind,
78  "bname":bc.binding_name
79  },
80  self.__GetNameTreeRecursive__(cur_obj))
81  elif bc.binding_type == CosNaming.nobject:
82  ns_dict[dict_key] = (cur_obj, None)
83  ns_dict[dict_key] = (
84  {
85  "objref":cur_obj,
86  "id":bc.binding_name[0].id,
87  "kind":bc.binding_name[0].kind,
88  "bname":bc.binding_name
89  },None)
90  except:
91  except_mess("context method error!:")
92  ns_dict = {}
93  break
94 
95  return ns_dict
96 
97  def DeleteToContext(self,cxt,bname):
98  if cxt != None:
99  try:
100  cxt.unbind(bname)
101  except:
102  except_mess('contex not found:')
103 
104 
105 if __name__ == '__main__':
106  import sys
107  nsh = RtmNSHelper()
108  if len(sys.argv) == 1:
109  print sys.argv[0] + " [CosNameService Host Name]"
110  sys.exit(1)
111 
112  nsh.Connect(sys.argv[1])
113  print nsh.GetNSDict()
114 
def GetNSDict(self)
Definition: NSHelper.py:51
def DeleteToContext(self, cxt, bname)
Definition: NSHelper.py:97
def Connect(self, name_server)
Definition: NSHelper.py:35
def __GetNameTreeRecursive__(self, cxt)
Definition: NSHelper.py:57
def __init__(self)
Definition: NSHelper.py:24
def except_mess(mess)
Definition: RtmDialog.py:100


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:43