We will demonstrate the use of J-Integra® Espresso with the help of a simple example. We create two console applications: a server and a client. The server provides functionality which the client will call using CORBA.
Any CORBA-based design begins with the layout of the interfaces needed by the application. All interfaces and additional types are defined in an IDL file. In our 'hello' example we have one interface 'Greetings' with a single method 'hello'. The parameter is a string.
// hello.idl module Example { interface Greetings { void hello( in string strName ); }; }; |
The IDL source is stored in the file 'hello.idl'. In order to generate code, one has to call the IDL compiler IcsIdl2Cs.exe.
> IcsIdl2Cs.exe hello.idl |
The IDL compiler creates a file 'hello.cs' containing stubs, skeletons, and several helper classes.
It takes only a few steps to create the server part of the application:
One of the central tasks for writing the server code is the implementation of the interface 'Greetings'. The generated class 'GreetingsPOA', which serves as the base class for 'GreetingsImpl', provides a link between the object implementation and the object adapter. In the 'Main' of our server program we first initialize the ORB and the Portable Object Adapter (POA), register the servant, export the Interoperable Object Reference (IOR) and call the ORB's run() method which starts the main event loop waiting for external CORBA requests.
using System; using Example; namespace Server { public class GreetingsImpl: GreetingsPOA { public override void hello( string strName ) { Console.WriteLine("Function 'Hello', Parameter '{0}'", strName); } } class Server { static void Main(string[] args) { Ics.CORBA.ORB oOrb = Ics.CORBA._ORB.init( args, null); Ics.PortableServer.POA oRootPOA = Ics.PortableServer.POAHelper.narrow( oOrb.resolve_initial_references( "RootPOA" ) ); // activate POA oRootPOA.the_POAManager.activate(); // implementation of the Greetings Interface GreetingsImpl oGreetings = new GreetingsImpl(); // register servant with POA Greetings oIGreeting = oGreetings._this( oOrb ); // write IOR-File Ics.CORBA._ORB.wrIORtoFile("c:\\hello.ior", oIGreeting ); // workup Requests oOrb.run(); } } } |
The client is created in a similar way:
The client reads the IOR from a file which the server created at startup. With the help of the IOR the client can connect to the server and call the function 'hello'.
using System; using Example; namespace Client { class Client { static void Main(string[] args) { Ics.CORBA.ORB oOrb = Ics.CORBA._ORB.init(args, null); Ics.CORBA.Object oGreetingsObj = oOrb.string_to_object("file://C:\\hello.ior"); Greetings oIGreetings = GreetingsHelper.narrow( oGreetingsObj ); oIGreetings.hello("Santa Claus"); } } } |
After starting the server and the client, the output on the server screen should contain the following line.
Function 'Hello', Parameter 'Santa Claus'. |