CORBA

Programming Example: Array Adder
Now let's see how to use the POA to develop a CORBA application. The application that we will develop here is an array adder: the client provides two arrays and the server adds them together and sends the result back to the client. We will develop two versions of the application: a transient server and a persistent server.

Array Adder: Transient Server
The first step in developing any CORBA application is to define the interface in the OMG Interface Definition Language (IDL). The IDL interface for the array adder is shown in Code Sample 1. Here I define a module ArithApp (which is equivalent to a Java package), an interface Add that contains a constant, a new data type array (which is a synonym for an array of longs and an operation addArrays that takes in two arrays as input (specified using the in) and another array as the output holder (specified using the out).

Code Sample 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);
};
};

You can now compile this IDL interface to map it to Java and generate stubs and skeletons. This can be done using the idlj compiler. When you run this tool you can request it to generate client stubs only, server side skeletons only, or both. Here you want to generate both, client stubs and server skeletons. To do so use the following command:

prompt> idlj -fall Add.idl
This command will generate several files. Check the local directory where you run the command from to see the files. You will notice that a new subdirectory with the name ArithApp has been created. This is because an OMG IDL module is mapped to a Java package. For more information on the idlj compiler and the options you can use, please see the IDL-to-Java Compiler.


--------------------------------------------------------------------------------
Note: The new idlj compiler in J2SE 1.4 generates server-side mappings for the Portable Object Adapter (POA). The new compiler is, however, backward compatible with earlier releases since it provides the -ImplBase flag that can be used to generate server-side mappings for existing applications that have been created using J2SE 1.3 or earlier versions. Therefore, in order to talk to existing applications that have been created using J2SE 1.3 or earlier, you need to use the -ImplBase flag to generate server-side mappings. New applications do not need to generate these deprecated server-side mappings.
--------------------------------------------------------------------------------

The next step is to implement the IDL interface in Code Sample 1. An implementation is shown in Code Sample 2. The AddImpl class is a subclass of AddPOA, which is generated by the idlj compiler from the IDL interface. Note the third parameter to the addArrays operation. Here I am using an array holder simply because I am using the out parameter as a holder for the output array.

Code Sample 2: AddImpl.java

import ArithApp.*;
import org.omg.CORBA.*;

class AddImpl extends AddPOA {
private ORB orb;

public AddImpl(ORB orb) {
this.orb = orb;
}

// implement 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<ArithApp.Add.SIZE; i++) {
result.value[i] = a[i] + b[i];
}
}
}

The next step is to develop the server. A sample server is shown in Code Sample 3. The server performs the following tasks:

Creates and initializes the ORB.
Creates an instance of the interface implementation and registers it with the ORB.
Gets a reference to the RootPOA and activates the POAManager.
Gets an object reference from the servant.
Gets the root naming context from the naming service and registers the new object under the name "Add".
Waits for invocations from clients.
Code Sample 3: AddServer.java

import ArithApp.*;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.PortableServer.*;
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 Reference 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 clients
orb.run();
} catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
System.out.println("AddServer Exiting ....");
}
}

Now, implement the client. A sample client is shown in Code Sample 4. The client code performs the following tasks:

Creates and initializes the ORB.
Obtains a reference to the root naming context.
Looks up the "Add" object in the naming context and obtains a reference to it.
Invokes the addArrays method and prints the results.
Code Sample 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 be added
int a[] = {6, 6, 6, 6, 6, 6, 6, 6, 6, 6};
int b[] = {7, 7, 7, 7, 7, 7, 7, 7, 7, 7};

// the result will be saved in this new array
ArithApp.AddPackage.arrayHolder c =
new ArithApp.AddPackage.arrayHolder();

// invoke the method addArrays()
impl.addArrays(a, b, c);
// print the new array
System.out.println("The sum of the two arrays is: ");
for(int i=0;i<ArithApp.Add.SIZE;i++) {
System.out.println(c.value[i]);
}

} catch (Exception e) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}
}

Now you can compile the classes AddImpl, AddServer, AddClient, and the stubs and skeletons that were generated by the idlj compiler. This is done using the javac compiler as follows:

prompt> javac *.java ArithApp/*.java
To run the application:

Start the orbd, which is a name server:


prompt> orbd -ORBInitialPort 2500
The number 2500 is the port number where you want the orbd to run. Note that the -ORBInitialPort is a require command-line argument.


Start the AddServer:


prompt> java AddServer -ORBInitialPort 2500
This command starts the server as shown in Figure 2.



Figure 2: Starting the AddServer


Here we are assuming that both the AddServer and orbd are running on the same host. If the orbd is running on a different host, use the -ORBInitialHost argument to inform the server where to find the orbd.


Start the AddClient:


prompt> java AddClient -ORBInitialPort 2500
You should see the sum of the two arrays as shown in



Figure 3: Starting the AddClient
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值