Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » CorbaRSS Feeds

Program for calculating area of a circle on server side,when a client sends request along with given radius and then displaying result on client side

Posted By: Jacob Evans     Category: Java     Views: 18413

Write a CORBA program for calculating area of a circle on server side,when a client sends request along with given radius and then displaying result on client side.

Code for Program for calculating area of a circle on server side,when a client sends request along with given radius and then displaying result on client side in Java

----------------------------------------------------------------------------------
                    IDL
----------------------------------------------------------------------------------
module CircleApp
{
    interface Circle
    {
        float area(infloat val);
        oneway void shutdown();
    };
};

-------------------------------------------------------------------------------------------------
                    Server
-------------------------------------------------------------------------------------------------

import CircleApp.*;
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 CircleImpl extends CirclePOA
{
    private ORB orb;

    publicvoid setORB(ORB orb_val)
    {
        orb = orb_val; 
    }
    
    publicfloat area(float val)
    {
        float res = (float)( Math.PI * val * val );
        return res;
    }
    publicvoid shutdown()
    {
        orb.shutdown(false);
    }
}


publicclass CircleServer
{

    publicstaticvoid main(String args[])
    {
        try
        {
            ORB orb = ORB.init(args, null);
            POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
            rootpoa.the_POAManager().activate();
            
            CircleImpl circleImpl = new CircleImpl();
            circleImpl.setORB(orb); 

            org.omg.CORBA.Object ref = rootpoa.servant_to_reference(circleImpl);
            Circle href = CircleHelper.narrow(ref);
      
            org.omg.CORBA.Object objRef =orb.resolve_initial_references("NameService");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

            String name = "Circle";
            NameComponent path[] = ncRef.to_name( name );
            ncRef.rebind(path, href);

            System.out.println("CircleServer ready and waiting ...");

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

        System.out.println("CircleServer Exiting ...");
    }
}

-------------------------------------------------------------------------------------------------
                    Client
-------------------------------------------------------------------------------------------------

import CircleApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;

publicclass CircleClient
{
    publicfloat getValue() throws IOException
    {
        float val = 0;
        try
        {
            System.out.print("Enter the value : ");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String s=br.readLine();
            val = Float.parseFloat (s);
        }
        catch (IOException e)
        {
            System.out.println(e);
        }
        return val;
    }
    
    publicstaticvoid main(String args[])
    {
        Circle circleImpl=null;
        CircleClient cc = new CircleClient();

        try
        {
            ORB orb = ORB.init(args, null);
            org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
            String name = "Circle";
            circleImpl = CircleHelper.narrow(ncRef.resolve_str(name));

            float val = 0, res = 0;
            val = cc.getValue();
            res=circleImpl.area(val);
            System.out.println("Result : " + res);

            circleImpl.shutdown();

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

}

-------------------------------------------------------------------------------------------------
                    Output
-------------------------------------------------------------------------------------------------

Enter the value : 2
Result : 12.566371
  
Share: 



Jacob Evans
Jacob Evans author of Program for calculating area of a circle on server side,when a client sends request along with given radius and then displaying result on client side is from London, United Kingdom.
 
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!