If you want to develop a CORBA Helloworld, there are basically the following steps:
1. Use the idl language to develop the idl file, which describes the definition of the interface
module: corresponds to the package in java
interface: corresponds to the interface in java, HelloWorld is the interface name
sayHello: corresponds to the method declared by the interface in java
string: corresponds to the return value of the method in java
2. Use the idlj command in java to translate idl language into java language and generate java code
idlj -fall m.idl
idlj: tools that come with java
-fall: Generate server and client-side code, or generate server or client separately
The following contains 6 java source files, which are described as follows:
HelloPOA POA refers to Portable Object Adapter (lightweight object adapter). This abstract class is a stream-based server-side skeleton that provides basic server-side CORBA functionality.
_HelloSutb client stub class that provides CORBA functionality to the client.
Hello This is the java version of the Hello interface, which provides standard CORBA object functionality.
HelloHelper This is a helper class responsible for writing or reading objects to or from a CORBA stream.
HelloHolder This is a final class that holds a public Hello instance variable. It is used to manipulate parameters of CORBA input and output streams.
The HelloOperations class is the interface we envisioned, containing only the method we defined, not anything from CORBA.
3. Develop the code on the server side
The implementation is on the server side and needs to be inherited from HelloWorldPOA
Copy six stub files to the Server project
1) Interface implementation class
package server;
import helloworld.HelloWorldPOA;
/**
* Server-side implementation code
*
*/
public class HelloWorldImpl extends HelloWorldPOA {
@Override
public String sayHello() {
return "Hello World!12333"; //Server-side implementation code
}
}
2) Service class
package server;
import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ServantNotActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;
import helloworld.HelloWorld;
import helloworld.HelloWorldHelper;
public class HelloServer {
public static void main(String[] args) throws ServantNotActive, WrongPolicy, InvalidName, AdapterInactive, org.omg.CosNaming.NamingContextPackage.InvalidName, NotFound, CannotProceed {
//Specify the port number of ORB - ORBInitialPort 1050
args = new String[2];
args[0] = "-ORBInitialPort";
args[1] = "1050";
//Create an ORB instance
ORB orb = ORB.init(args, null);
//Get the reference of RootPOA and activate POAManager, which is equivalent to starting the server
org.omg.CORBA.Object obj=orb.resolve_initial_references("RootPOA");
POA rootpoa = POAHelper.narrow(obj);
rootpoa.the_POAManager().activate();
//Create an instance of HelloWorldImpl
HelloWorldImpl helloImpl = new HelloWorldImpl();
//Get a reference to the object from the service and register it with the service
org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloImpl);
HelloWorld href = HelloWorldHelper.narrow(ref);
// get a context with root name
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
// bind this object in the naming context
String name = "Hello";
NameComponent path[] = ncRef.to_name(name);
ncRef.rebind(path, href);
//Start the thread service and wait for the client to call
orb.run();
System.out.println("server startup...");
}
}
4. Develop client-side code
The implementation of the server side and the service monitoring have been completed. Now you need to write a client to call the server method.
Copy six stub files to the client project
package client;
import helloworld.HelloWorld;
import helloworld.HelloWorldHelper;
import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;
public class HelloClient {
static HelloWorld helloWorldImpl;
static {
System.out.println("client starts to connect to server.......");
//Initialize ip and port number, -ORBInitialHost 127.0.0.1 -ORBInitialPort 1050
String args[] = new String[4];
args[0] = "-ORBInitialHost";
//The IP address of the server, defined in HelloServer
args[1] = "127.0.0.1";
args[2] = "-ORBInitialPort";
//The port on the server side, defined in HelloServer
args[3] = "1050";
//Create an ORB instance
ORB orb = ORB.init(args, null);
// Get the root name context
org.omg.CORBA.Object objRef = null;
try {
objRef = orb.resolve_initial_references("NameService");
} catch (InvalidName e) {
e.printStackTrace ();
}
NamingContextExt neRef = NamingContextExtHelper.narrow(objRef);
String name = "Hello";
try {
//Get the implementation class instantiated by the server through ORB
helloWorldImpl = HelloWorldHelper.narrow(neRef.resolve_str(name));
} catch (NotFound e) {
e.printStackTrace ();
} catch (CannotProceed e) {
e.printStackTrace ();
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
e.printStackTrace ();
}
System.out.println("client connected server.......");
}
public static void main(String args[]) throws Exception {
sayHello ();
}
// call the method of the implementing class
public static void sayHello() {
String str = helloWorldImpl.sayHello();
System.out.println(str);
}
}
5. Start the orbd service
Just creating the server and client code is not enough to run a HelloWorld, you also need to start an orbd service, orbd includes self-starting services, transparent naming services, persistent naming services and background processing processes of the naming manager.
orbd -ORBInitialPort 1050 -ORBInitialHost 127.0.0.1
6. Start the server service
7. Start the client