Welcome, guest | Sign In | My Account | Store | Cart

CORBA has a reputation for being hard to use, but it is really very easy, expecially if you use Python. This example shows the complete implementation of a fortune cookie server and its client.

Python, 48 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// fortune.idl -- the interface definition
module Fortune {
  interface CookieServer {
    string get_cookie();
  };
};


#
# The client, on the command line
#

>>> import CORBA, Fortune
>>> orb = CORBA.ORB_init()
>>> o = orb.string_to_object("corbaloc::host.example.com/fortune")
>>> o = o._narrow(Fortune.CookieServer)
>>> print o.get_cookie()

#
# The server
#

#!/usr/bin/env python

import sys, os
import CORBA, Fortune, Fortune__POA

FORTUNE_PATH = "/usr/games/fortune"

class CookieServer_i (Fortune__POA.CookieServer):
    def get_cookie(self):
        pipe   = os.popen(FORTUNE_PATH)
        cookie = pipe.read()
        if pipe.close():
            # An error occurred with the pipe
            cookie = "Oh dear, couldn't get a fortune\n"
        return cookie

orb = CORBA.ORB_init(sys.argv)
poa = orb.resolve_initial_references("RootPOA")

servant = CookieServer_i()
poa.activate_object(servant)

print orb.object_to_string(servant._this())

poa._get_the_POAManager().activate()
orb.run()

To run this example, you need a Python CORBA implementation. With most ORBs, you must convert the IDL interface definition into Python declarations with an IDL compiler. For example, with omniORBpy you do:

omniidl -bpython fortune.idl

This creates modules named Fortune and Fortune__POA, to be used by clients and servers.

When you run the server, it prints out a long hex string like

IOR:010000001d00000049444c3a466f7274756e652f436f6f6b69655365727665723 a312e3000000000010000000000000060000000010102001200000068656c6c6f2e65 78616d706c652e636f6d00f90a07000000666f7274756e65000200000000000000080 000000100000000545441010000001c00000001000000010001000100000001000105 090101000100000009010100

Give that to the client's orb.string_to_object() call to contact your server. Notice that about half the server's code is dealing with obtaining the fortune cookie, and nothing to do with CORBA at all.

It's easy to make your server support a simple corbaloc URL string like the client example, but that involves some omniORB specific code. See the omniORBpy manual for details.

There used to be a live Fortune server running at AT&T Laboratories Cambridge, but the lab closed in April 2002, so the server is no longer available.

Download omniORBpy from http://omniorb.sourceforge.net/

4 comments

Noah Spurrier 21 years, 7 months ago  # | flag

Also check out FNORB for a Pure Python CORBA ORB. Also check out FNORB for a pure Python CORBA ORB.

It can be found at:

http://sourceforge.net/projects/fnorb

Paul McGuire 20 years, 7 months ago  # | flag

Latest Fnorb is CORBA 2.1 only. Does not support newer CORBA features, esp. POA (Portable Object Adapter).

While not pure Python, omniORB is quite Python-friendly.

W.J. van der Laan 20 years, 5 months ago  # | flag

Dynamic IDL Compiling. You could also use ORBit, which supports dynamic IDL compiling:

http://www.gnome.org/projects/ORBit2/python/dynamic_idl.html

This is especially useful for clients, as you don't have to compile the IDLs to stubs and skeletons beforehand.

Ladislav Lencucha 16 years, 8 months ago  # | flag

Client class. Hello,

nice example - but I have some questions:

If I have class in client.py which can be subscribed to some events using class with specified interface, e.g.:

"class eventclient(SomeClass__POA): def event_something_added(self, added_object): print "something was added"

here is some code to register the connect to ORB, register the class , etc

..."

What method should I run to make the script active (not end after the connection and registration) and receive all the events? Something like ORB.mainloop() ...

thanks for reply

Created by Duncan Grisby on Fri, 12 Oct 2001 (PSF)
Python recipes (4591)
Duncan Grisby's recipes (1)
Python Cookbook Edition 2 (117)

Required Modules

  • (none specified)

Other Information and Tasks