Package logilab-common-0 :: Package 39 :: Package 0 :: Module corbautils
[frames] | no frames]

Source Code for Module logilab-common-0.39.0.corbautils

  1  """A set of utility function to ease the use of OmniORBpy. 
  2   
  3  :copyright: 2000-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
  4  :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr 
  5  :license: General Public License version 2 - http://www.gnu.org/licenses 
  6  """ 
  7  __docformat__ = "restructuredtext en" 
  8   
  9  from omniORB import CORBA, PortableServer 
 10  import CosNaming 
 11   
 12  orb = None 
 13   
14 -def get_orb():
15 """ 16 returns a reference to the ORB. 17 The first call to the method initialized the ORB 18 This method is mainly used internally in the module. 19 """ 20 21 global orb 22 if orb is None: 23 import sys 24 orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID) 25 return orb
26
27 -def get_root_context():
28 """ 29 returns a reference to the NameService object. 30 This method is mainly used internally in the module. 31 """ 32 33 orb = get_orb() 34 nss = orb.resolve_initial_references("NameService") 35 rootContext = nss._narrow(CosNaming.NamingContext) 36 assert rootContext is not None,"Failed to narrow root naming context" 37 return rootContext
38
39 -def register_object_name(object, namepath):
40 """ 41 Registers a object in the NamingService. 42 The name path is a list of 2-uples (id,kind) giving the path. 43 44 For instance if the path of an object is [('foo',''),('bar','')], 45 it is possible to get a reference to the object using the URL 46 'corbaname::hostname#foo/bar'. 47 [('logilab','rootmodule'),('chatbot','application'),('chatter','server')] 48 is mapped to 49 'corbaname::hostname#logilab.rootmodule/chatbot.application/chatter.server' 50 51 The get_object_reference() function can be used to resolve such a URL. 52 """ 53 context = get_root_context() 54 for id, kind in namepath[:-1]: 55 name = [CosNaming.NameComponent(id, kind)] 56 try: 57 context = context.bind_new_context(name) 58 except CosNaming.NamingContext.AlreadyBound, ex: 59 context = context.resolve(name)._narrow(CosNaming.NamingContext) 60 assert context is not None, \ 61 'test context exists but is not a NamingContext' 62 63 id,kind = namepath[-1] 64 name = [CosNaming.NameComponent(id, kind)] 65 try: 66 context.bind(name, object._this()) 67 except CosNaming.NamingContext.AlreadyBound, ex: 68 context.rebind(name, object._this())
69
70 -def activate_POA():
71 """ 72 This methods activates the Portable Object Adapter. 73 You need to call it to enable the reception of messages in your code, 74 on both the client and the server. 75 """ 76 orb = get_orb() 77 poa = orb.resolve_initial_references('RootPOA') 78 poaManager = poa._get_the_POAManager() 79 poaManager.activate()
80
81 -def run_orb():
82 """ 83 Enters the ORB mainloop on the server. 84 You should not call this method on the client. 85 """ 86 get_orb().run()
87
88 -def get_object_reference(url):
89 """ 90 Resolves a corbaname URL to an object proxy. 91 See register_object_name() for examples URLs 92 """ 93 return get_orb().string_to_object(url)
94
95 -def get_object_string(host, namepath):
96 """given an host name and a name path as described in register_object_name, 97 return a corba string identifier 98 """ 99 strname = '/'.join(['.'.join(path_elt) for path_elt in namepath]) 100 return 'corbaname::%s#%s' % (host, strname)
101