Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SERVER SIDE
- 1. Create a new Java Project and call it : CorbaAdditionServer
- 2. Create an empty file and save it with an *.idl extension and compile using idlj - fall Addition.idl
- module AdditionApp
- {
- //do not use an interface name as the name of a module
- interface Addition
- {
- long add(in long a,in long b);
- oneway void shutdown();
- };
- };
- 3. Generates 6 files automatically
- POA - the server skeleton
- Holder - the instance "holder"
- Helper - provides functions for casting references to their types
- Stub - the client stub
- Operations = the operations specific to the app
- Addition.java = the "signature" file implementing the addition contract
- 4. Create a new class called AdditionObj
- import AdditionApp.*;
- import org.omg.CosNaming.*;
- import org.omg.CosNaming.NamingContextPackage.*;
- import org.omg.CORBA.*;
- import org.omg.PortableServer.*;
- import org.omg.PortableServer.POA;
- import java.util.Properties;
- class AdditionObj extends AdditionPOA {
- private ORB orb;
- public void setORB(ORB orb_val) {
- orb = orb_val;
- }
- // implement add() method
- public int add(int a, int b) {
- int r=a+b;
- return r;
- }
- // implement shutdown() method
- public void shutdown() {
- orb.shutdown(false);
- }
- }
- 5. Create a Class called StartServer
- import AdditionApp.*;
- import org.omg.CosNaming.*;
- import org.omg.CosNaming.NamingContextPackage.*;
- import org.omg.CORBA.*;
- import org.omg.PortableServer.*;
- import org.omg.PortableServer.POA;
- import java.util.Properties;
- public class StartServer {
- public static void main(String args[]) {
- try{
- // create and initialize the ORB //// get reference to rootpoa & activate the POAManager
- ORB orb = ORB.init(args, null);
- POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
- rootpoa.the_POAManager().activate();
- // create servant and register it with the ORB
- AdditionObj addobj = new AdditionObj();
- addobj.setORB(orb);
- // get object reference from the servant
- org.omg.CORBA.Object ref = rootpoa.servant_to_reference(addobj);
- Addition href = AdditionHelper.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
- NameComponent path[] = ncRef.to_name( "ABC" );
- ncRef.rebind(path, href);
- System.out.println("Addition Server ready and waiting ...");
- // wait for invocations from clients
- for (;;){
- orb.run();
- }
- }
- catch (Exception e) {
- System.err.println("ERROR: " + e);
- e.printStackTrace(System.out);
- }
- System.out.println("HelloServer Exiting ...");
- }
- }
- 6. Open a new CMD console and type in the command below
- start orbd -ORBInitialPort 1050
- i.) Click Run on the IDE -> Run Configuration
- ii.) Select the StartServer for the CorbaAdditionServer
- iii.) Click on Arguments and type in : -ORBInitialPort 1050 -ORBInitialHost localhost
- //The server should be running.
- CLIENT SIDE
- 1. Create a new Java Project and call it : CorbaAdditionClient
- 2. Copy the AdditionApp Package under CorbaAdditionServer on the src Package under CorbaAdditionClient
- 3. Create a new Class called StartClient
- import AdditionApp.*;
- import org.omg.CosNaming.*;
- import org.omg.CosNaming.NamingContextPackage.*;
- import org.omg.CORBA.*;
- import java.io.*;
- import java.util.*;
- public class StartClient {
- /**
- * @param args the command line arguments
- */
- 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
- Addition addobj = (Addition) AdditionHelper.narrow(ncRef.resolve_str("ABC"));
- Scanner c=new Scanner(System.in);
- System.out.println("Welcome to the addition system:");
- for(;;){
- System.out.println("Enter a:");
- String aa = c.nextLine();
- System.out.println("Enter b:");
- String bb = c.nextLine();
- int a=Integer.parseInt(aa);
- int b=Integer.parseInt(bb);
- int r=addobj.add(a,b);
- System.out.println("The result for addition is : "+r);
- System.out.println("-----------------------------------");
- }
- }
- catch (Exception e) {
- System.out.println("Hello Client exception: " + e);
- e.printStackTrace();
- }
- }
- }
- 4. i.) Click on Run -> Run Configuration
- ii.) Select the StartClient for the CorbaAdditionClient.
- iii.) Click on Arguments, then under the Program arguments, type in : -ORBInitialPort 1050 -ORBInitialHost localhost
- iv.) Click Apply
- 5. Run the program =)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement