Java Tutorial

 
 
 
 
// ComplexNumber.idl

module Ex3Module
{
    interface ComplexNumber
    {
        void setFirstNumber(in long long real , in long long imaginary) ;
        void setSecondNumber(in long long real , in long long imaginary) ;
        void doSum() ;
        long long getReal() ;
        long long getImaginary() ;
    } ;
} ;


// ComplexNumberImpl.java

import Ex3Module.ComplexNumberPOA ;

class ComplexNumberImpl extends ComplexNumberPOA
{
    long FirstReal , FirstImaginary ;
    long SecondReal , SecondImaginary ;
    long SumReal , SumImaginary ;

    public void setFirstNumber(long real , long imaginary)
    {
        FirstReal = real ;
        FirstImaginary = imaginary ;
    }

    public void setSecondNumber(long real , long imaginary)
    {
        SecondReal = real ;
        SecondImaginary = imaginary ;
    }    

    public void doSum()
    {
        SumReal = FirstReal + SecondReal ;
        SumImaginary = FirstImaginary + SecondImaginary ;
    }

    public long getReal()
    {
        return SumReal ;
    }

    public long getImaginary()
    {
        return SumImaginary ;
    }
}


// ComplexNumberClient.java

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

class ComplexNumberClient
{
    public static void main(String [] args)    
    {
        ComplexNumber cNumber = null ;

        try
        {
            org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

            org.omg.CORBA.Object objRef = orb.resolve_initial_references(\"NameService\");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);    

            String name = \"ComplexNumber\" ;

            cNumber = ComplexNumberHelper.narrow(ncRef.resolve_str(name));

            cNumber.setFirstNumber(9 , 4) ;
            cNumber.setSecondNumber(8 , 2) ;
            cNumber.doSum() ;

            System.out.println(\"The sum of two complexnumber is : \" + cNumber.getReal() + \" + \" + cNumber.getImaginary() + \"i\") ;


        }
        catch(Exception e)
        {
            e.printStackTrace() ;
        }
    }
}


// ComplexNumberServer.java

import Ex3Module.ComplexNumber ;
import org.omg.CosNaming.* ;
import org.omg.CosNaming.NamingContextPackage.* ;
import org.omg.PortableServer.* ;

class ComplexNumberServer
{
    public static void main(String [] args)
    {
        try
        {
            org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args , null) ;

            POA rootPOA = POAHelper.narrow(orb.resolve_initial_references(\"RootPOA\"));

            rootPOA.the_POAManager().activate();

            ComplexNumberImpl cNumber = new ComplexNumberImpl() ;

            org.omg.CORBA.Object ref = rootPOA.servant_to_reference(cNumber) ;

            System.out.println(\"Step1\");
            ComplexNumber h_ref = Ex3Module.ComplexNumberHelper.narrow(ref);

            System.out.println(\"Step2\");
            org.omg.CORBA.Object objRef = orb.resolve_initial_references(\"NameService\");

            System.out.println(\"Step3\");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

            System.out.println(\"Step4\");
            String name = \"ComplexNumber\";

            NameComponent path[] = ncRef.to_name(name);
            ncRef.rebind(path,h_ref);

            System.out.println(\"ComplexNumber Server reading and waiting....\");
            orb.run();

        }
        catch(Exception e)        
        {
            e.printStackTrace() ;
        }
    }
}