RtmNSHelper.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 #
00004 #  @file RtmNSHelper.py
00005 #  @brief rtc-link name service access helper class
00006 #  @date $Date: 2005-05-12 09:06:19 $
00007 #  @author Noriaki Ando <n-ando@aist.go.jp>
00008 # 
00009 #  Copyright (C) 2004-2005
00010 #      Task-intelligence Research Group,
00011 #      Intelligent Systems Research Institute,
00012 #      National Institute of
00013 #          Advanced Industrial Science and Technology (AIST), Japan
00014 #      All rights reserved.
00015 # 
00016 #  $Id: RtmNSHelper.py 775 2008-07-28 16:14:45Z n-ando $
00017 # 
00018 
00019 import sys
00020 #sys.path.append("/usr/local/lib/python2.3/site-packages")
00021 import omniORB.CORBA as CORBA
00022 import CosNaming
00023 import RTM
00024 from RtmDialog import *
00025 
00026 
00027 class RtmNSHelper:
00028         def __init__(self):
00029                 self.orb = None
00030 
00031         def __del__(self):
00032                 if self.orb != None:
00033                         try:
00034                                 self.orb.destroy()
00035                         except:
00036                                 except_mess('CORBA Name Server: Destroy Error!! <' + name_server + '>')
00037 
00038         def Connect(self, name_server):
00039                 try:
00040                         if self.orb != None:
00041                                 self.orb.destroy()
00042                         arg = ["-ORBInitRef", "NameService=corbaname::","-ORBclientCallTimeOutPeriod","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 #                       print 'CORBA Name Server: Connect Error!! <',name_server,'>'
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                 


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