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