Java Tutorial

 
 
 
 
// Addexpr.idl

module AddExprApp
{
        struct Expr
        {
                long Xcord;
                long Ycord;
        };
        interface AddExpr
        {
                Expr addition(in Expr one, in Expr two);
        };
};


// Addexprclient.java

import AddExprApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;

import java.io.*;

public class AddExprClient
{
    static AddExpr addExprImpl;
    public static void main(String args[]) throws IOException
    {
      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 = \"AddExpr\";
        addExprImpl = AddExprHelper.narrow(ncRef.resolve_str(name));

    System.out.println(\"Obtained a handle on the server object : \" + addExprImpl);

                        Expr one,two;
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                        System.out.print(\"Enter first expression X-cord : \");
                        int x=Integer.parseInt(br.readLine());
                        System.out.print(\"Enter first expression Y-cord : \");
                        int y=Integer.parseInt(br.readLine());
                        one=new Expr(x,y);

                        System.out.print(\"Enter second expression X-cord : \");
                        x=Integer.parseInt(br.readLine());
                        System.out.print(\"Enter second expression Y-cord : \");
                        y=Integer.parseInt(br.readLine());
                        two=new Expr(x,y);

                        one=addExprImpl.addition(one,two);

                        System.out.println(\"result of addition is \" + one.Xcord +
                                \"X + (\" + one.Ycord + \"Y)\");

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


// AddexprServer.java

import AddExprApp.*;
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;
import java.util.StringTokenizer;

class AddExprImpl extends AddExprPOA {
  private ORB orb;

  public void setORB(ORB orb_val) {
    orb = orb_val; 
  }

        public Expr addition (Expr one, Expr two)
        {
                return new Expr(one.Xcord+two.Xcord,one.Ycord+two.Ycord);
        }
}


public class AddExprServer {

  public static void main(String args[]) {
    try{
      // create and initialize the ORB
      ORB orb = ORB.init(args, null);

      // get reference to rootpoa & activate the POAManager
      POA rootpoa = POAHelper.narrow(orb.resolve_initial_references(\"RootPOA\"));
      rootpoa.the_POAManager().activate();

      // create servant and register it with the ORB
      AddExprImpl addExprImpl = new AddExprImpl();
      addExprImpl.setORB(orb); 

      // get object reference from the servant
      org.omg.CORBA.Object ref = rootpoa.servant_to_reference(addExprImpl);
      AddExpr href = AddExprHelper.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 = \"AddExpr\";
      NameComponent path[] = ncRef.to_name( name );
      ncRef.rebind(path, href);

      System.out.println(\"AddExprServer ready and waiting ...\");

      // wait for invocations from clients
      orb.run();
    } 

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

      System.out.println(\"AddExprServer Exiting ...\");

  }
}


// OUTPUT

Enter first expression X-cord : 12

Enter first expression Y-cord : 23

Enter second expression X-cord : 4

Enter second expression Y-cord : 5

result of addition is (16x+28y)