Programming example: array adds
This program is an array adding program: two arrays provided by the client, then the server adds them and returns the result to the client.
Array add: Temporary service
Developing any CORBA app first uses an OMG interface definition language (IDL) to define the interface. The IDL interface definition of the array add program is as shown in the following code. Here we define a module called AritHapp (equivalent to the package in Java); an interface called Add, contains a constant, a new data type Array (one array defined as a long type), and AddaRays operation, which uses two arrays as parameter inputs (specified with keyword IN), and another array as output (specified with keyword OUT).
Code example 1: add.idl
Module Arithapp {
Interface add {
Const unsigned short size = 10;
TypedEf long array [size];
Void Addarrays (in Array A, In Array B,
Out array result);
}
}
Now you can compile this IDL interface to generate Java programs, generate program backbone and framework. This can be implemented by the IDLJ compiler. When you compile, you can specify whether only the client framework or the server framework or both simultaneously. At this time you need to generate a client and server framework. Use the following command:
Prompt> idlj -fall add.idl
Compiling will generate a lot of files, you can observe which files have been generated. You can find a new subdirectory named AritHapp being created. This is due to the module in the omg IDL corresponding to the package in Java.
Note: The IDLJ compiler in J2SE 1.4 is to generate a lightweight object adapter (POA) for the server side. But you can use the -OLDIMPLBASE parameter to build a CORBA program written in J2SE1.3 or earlier to generate files that are consistent with earlier versions. Note that the new version of the program is not reproduced to these server-side that is now not advocated.
The next step is to implement the IDL interface defined in Code 1. Code Example 2 showed such an implementation. The AddImpl class is a subclass of AddPoa, and the AddPoA class is generated from the IDLJ compiler from the IDL interface. Note the third parameter of the AddarRays function. In this case, we only use an array holder because we have used the OUT parameter to specify the type of output array.
Code Example 2: AddImpl.java
Import arithapp. *;
Import org.omg.corba. *;
Class addImpl extends addpoa {
Private Orb ORB;
Public AddIMPL (Orb ORB) {
THIS.ORB = ORB;
}
//Mplement the addarrays () Method
Public void addarrays (int A [], int b [],
Arithapp.addpackage.ArrayHolder Result) {
Result.Value = new int [arithapp.add.size];
For (int i = 0; I Result.Value [i] = a [i] b [i]; } } } Then you have to develop server-side programs. A sample program is shown in Code Example 3. The server should perform the following tasks: n New and initialized ORB. n Implement an instance for the interface and register on the ORB. n Get reference to rootpoa, start PoAManager. n Get object references to the server. n From the Name Service Get the root element name context, register a new object under the Add name. n Waiting for the call to the client. Code Example 3: Addserver.java Import arithapp. *; Import org.omg.corba. *; Import org.omg.cosnaming. *; Import Org.omg.PortablesRver. *; Import Org.omg.PortableServer.poA; Import Org.omg.cosnaming.namingContextPackage. *; Public class addserver { Public static void main (string args []) { Try { // Create and Initialize the ORB ORB ORB = Orb.init (args, null); // Create An Implementation and Register IT with THE ORB AddImpl IMPL = New AddIMPL (ORB); // Get Reference To Rootpoa & Activate The Poamanager Poa rootpoa = poahelper.narrow ORB.Resolve_initial_references ("rootpoa")); Rootpoa.The_Poamanager (). Activate (); // Get Object Reference from the Servant Org.omg.Corba.Object Ref = Rootpoa.servant_to_reference (IMPL); Add href = addhelper.narrow (REF); // Get the root naming context // Nameservice Invokes The Name Service Org.omg.corba.Object objref = Orb.resolve_initial_references ("Nameservice"); // Use NamingContextext Which IS Part of the Interoperable // naming service (ins) specification. NamingContexText ncref = NamingContexTextHelper.narrow (Objref); // bind the object reason in naming String name = "add"; NameComponent Path [] = ncref.to_name (name); NCREF.Rebind (path, href); System.out.println ("Addserver Ready to add up your arrays .... "); // Wait for Invocations from Clom Clom Clom Clom Clom Clom Clom Cliants Orb.run (); } catch (exception e) { System.err.println ("Error:" E); E.PrintStackTrace (System.out); } System.out.println ("Addserver EXITING ...."); } } Now you want to implement the client. The implementation of the client is shown in Code Example 4. The client should perform the following: n Generate and initialize the ORB. n Get references to the root element name context. n Find the Add object in the name context to get a reference to it. n calls the AddarRays method and outputs the result. Code Example 4: AddClient.java Import arithapp. *; Import org.omg.corba. *; Import org.omg.cosnaming. *; Import Org.omg.cosnaming.namingContextPackage. *; Public class addclient { Public static void main (string args []) { Try { // Create and Initialize the ORB ORB ORB = Orb.init (args, null); // Get the root naming context Org.omg.corba.Object objref = Orb.resolve_initial_references ("Nameservice"); // Use NamingContexText Instead of NamingContext. This is // Part of the interoperable naming service. NamingContexText ncref = NamingContexTextHelper.narrow (Objref); // resolve the object reference in naming String name = "add"; Add Impl = addHelper.narrow (ncref.resolve_str (name)); System.out.println ("Handle Obtained On Server Object:" IMPL); // the arrays to beadeded INT A [] = {3, 7, 6, 9, 2, 5, 0, 1, 4, 8}; INT B [] = {8, 2, 1, 7, 4, 9, 3, 5, 0, 6}; // The result will be saved in this new arch Arithapp.addpackage.ArrayHolder C = New arithapp.addpackage.ArrayHolder (); // invoke the method addarrays () Impl.addarrays (A, B, C); //print the new arch System.out.println ("The Sum of the Two Arrays IS:"); For (int i = 0; I System.out.println (C.Value [i]); } } catch (exception e) { System.out.println ("Error:" E); E.PrintStackTrace (System.out); } } } You can now compile AddIMPL, Addserver, AddClient classes, and frame files generated by the IDLJ compiler. You can compile the following javac commands: Prompt> javac * .java arithapp / *. java Run the program: 1. Start ORBD Name Service. Prompt> ORBD -ORBITIALPORT 25002500 is the port number you want ORBD running. Be careful to specify -orbinitialport as the command line parameter. 2. Start Addserver: Prompt> Java Addserver -orbinitialPort 2500 In this example we run the Addserver and ORBD on the same host. If the ORBD service is running on another host, then use the -orbinitialhost parameter to inform the server to find ORBD services. 3. Start AddClient: Prompt> Java AddClient -orbinitialPort 2500 We can see the run results added by the array.