July 2002
Intermediate to advanced
608 pages
15h 46m
English
Credit: Duncan Grisby
You need to implement a CORBA server and client to distribute a processing task, such as the all-important network-centralized, fortune-cookie distribution.
CORBA is a mature object-oriented RPC protocol, and several CORBA
ORBs offer excellent Python support. This recipe requires multiple
files. Here is the interface definition file,
fortune.idl:
module Fortune {
interface CookieServer {
string get_cookie( );
};
};The server script is a simple Python program:
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( )And here’s a demonstration of the client code, using the Python interactive 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( )
CORBA has a reputation for being hard to use, but it is really very easy, especially if you use Python. This example shows ...