Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » CorbaRSS Feeds

CORBA based application for addition of two mathematical expressions of the form (ax + by) and displaying the result on client side

Posted By: Milind Mishra     Category: Java     Views: 5139

Develop a CORBA based application for addition of two mathematical expressions of
the form (ax + by) and displaying the result on client side. Use an applet on the
client side. Eg.(3x+4y) + (5x+8y) = (8x+12y)

Code for CORBA based application for addition of two mathematical expressions of the form (ax + by) and displaying the result on client side in Java

// 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.*;

publicclass AddExprClient
{
    static AddExpr addExprImpl;
    publicstaticvoid 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;

  publicvoid setORB(ORB orb_val) {
    orb = orb_val; 
  }

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


publicclass AddExprServer {

  publicstaticvoid 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)
  
Share: 



Milind Mishra
Milind Mishra author of CORBA based application for addition of two mathematical expressions of the form (ax + by) and displaying the result on client side is from India.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!