Java Blog

 
 
 
----------------------------------------------------------------------------------
                    IDL
----------------------------------------------------------------------------------
module CircleApp
{
    interface Circle
    {
        float area(in float 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;

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

    public float area(float val)
    {
        float res = (float)( Math.PI * val * val );
        return res;
    }
    public void shutdown()
    {
        orb.shutdown(false);
    }
}


public class CircleServer
{

    public static void 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.*;

public class CircleClient
{
    public float 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;
    }

    public static void 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