为方便答复,提供服务器端相关文档: 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); }; };
服务程序AddServer.java import ArithApp.*; import org.omg.CORBA.*; import org.omg.CosNaming.*; import org.omg.PortableServer.*; import org.omg.PortableServer.POA;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 ...."); } }
接口实现程序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]; } } }
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);
};
};
import ArithApp.*;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;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 ....");
}
}
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];
}
}
}