1. Overview:
In the previous articles have been implemented CORBA environment configuration, Eclipse simple development implementation;
In the daily development, the common ways of remote invocation and implementation of CORBA interface are two kinds: IOR (file mode) and Nameservice (naming service mode);
The following is the IOR file mode specific service and client implementation code, as business requirements are implemented using Java, and other languages will be later filled up.
2. Description:
An interoperable object reference in which the IOR file converts the server-side implementation class into a string that is stored in a file with a suffix named IOR, and the client side obtains the object of the implementation class by reading the string in the file.
3, Service end:
public class Server_aom {public static void main (string[] args) {//Generate an Orb and initialize, this is the same as the Server side Properties props = Sys
Tem.getproperties ();
Configure the publishing port and IP props.put ("Org.omg.CORBA.ORBInitialPort", "1050");
Props.put ("Org.omg.CORBA.ORBInitialHost", "172.168.xxx.xxx");
System.out.println ("ORB initialised\n");
try {//Initialize the orb. Org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init (args, props);
Get a reference to the root POA org.omg.CORBA.Object obj = orb.resolve_initial_references ("Rootpoa");
POA poaroot = Poahelper.narrow (obj); Create policies for our persistent POA org.omg.corba.policy[] policies = {//Poaroot.create_lifespan_policy (Lifespanpo licyvalue.persistent), Poaroot.create_id_assignment_policy (idassignmentpolicyvalue.user_id), PoaRoot.create_
Thread_policy (Threadpolicyvalue.orb_ctrl_model)}; Create Mypoa with the right policies POA POA = POAROOT.CREATE_POA ("Helloserverimpl_poa", Poaroot.the_poamanager (), Poli
cies); Create the servant Helloserverimpl SERvant = new Helloserverimpl ();
Activate the servant with the ID on Mypoa byte[] objectId = "Anyobjectid". GetBytes ();
poa.activate_object_with_id (objectId, servant);
Activate the POA Manager poaroot.the_poamanager (). Activate ();
Get a reference to the servant and write it down.
obj = poa.servant_to_reference (servant);
PrintWriter PS = new PrintWriter (New FileOutputStream ("Server.ior"));
Ps.println (orb.object_to_string (obj));
Ps.close ();
System.out.println ("CORBA Server Ready ...");
Wait for incoming requests orb.run ();
catch (Exception ex) {ex.printstacktrace ();}} The service side is focused on reading Server.ior files, 4, client clients: class Helloclientimpl {private HelloApp.
Hello target = null;
Private Org.omg.CORBA.ORB ORB = null; /** * Constructor for Helloclientimpl * * @throws ioexception/Public Helloclientimpl () throws Exception {Initorb (NULL)
; }/** * Constructor for Helloclientimpl * * * @throws IOException * @see java.lang.object#object () */Public Helloclientim Pl(string[] args) throws Exception {Initorb (args);}
/** * Initialize ORB. * * @param args * @throws ioexception/public void Initorb (string[] args) throws Exception {//Set remote Invoke IP and Port Properties PR
OPS = System.getproperties ();
Props.put ("Org.omg.CORBA.ORBInitialPort", "1050");
Props.put ("Org.omg.CORBA.ORBInitialHost", "172.168.xxx.xxx");
Initialize The Orb orb = Org.omg.CORBA.ORB.init ((string[)) args, props); ----Uncomment below to enable naming Service access.
----LineNumberReader input = new LineNumberReader (New FileReader ("Server.ior"));
String IOR = Input.readline ();
Org.omg.CORBA.Object obj = Orb.string_to_object (IOR); target = HelloApp.
Hellohelper.narrow (obj);
}/** * Obtain ORB Interface. * * @return/public HelloApp.
Hello Getorbinterface () {return target;}
/** * Shutdown ORB.
*/public void shutdown () {Orb.shutdown (true);}
/** * Test driver for Helloclientimpl. * * @param args */public static void main (string[] args) {try {Helloclientimpl test = new Helloclientimpl ();
String result = Test.getorbinterface (). SayHello ();
SYSTEM.OUT.PRINTLN ("Result:" +result);
Stop Test.shutdown ();
catch (Exception ex) {ex.printstacktrace ();}}
}