Java Blog

 
 
 
// IDL

module GameModule
{
    interface Game
    {
        double guess(in double operand1);
    };

};


// GameImpl
// Contains the implementation of the methods defined in the IDL file.

import GameModule.GamePOA;

class GameImpl extends GamePOA
{
    GameImpl()
    {
        super();
        System.out.println(\"game Object Created\");
    }

    public double guess(double num)
    {
        if(num < 50)
            return(-1);
        else if(num > 50)
            return(1);
        else
            return(0);
    }
}


// OUTPUT

1
1
You enter number is Lower
Enter no=
32
32
You enter number is Lower
Enter no=
34
34
You enter number is Lower
Enter no=
65
65
You enter number is Higher
Enter no=
99
99
You enter number is Higher
Enter no=
50
50
You enter Exact num in trial =5


// Server


import GameModule.Game;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;

class GameServer
{

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

            // initialize the BOA/POA
            POA rootPOA = POAHelper.narrow(orb.resolve_initial_references(\"RootPOA\"));
            rootPOA.the_POAManager().activate();

            // creating the calculator object
            GameImpl game = new GameImpl();

            // get the object reference from the servant class
            org.omg.CORBA.Object ref = rootPOA.servant_to_reference(game);

            System.out.println(\"Step1\");
            Game h_ref = GameModule.GameHelper.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 = \"Game\";
            NameComponent path[] = ncRef.to_name(name);
            ncRef.rebind(path,h_ref);

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


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


// Client

import GameModule.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.DataInputStream;
import java.lang.Integer;

class GameClient
{

    public static void main(String args[])
    {
        Game GameImpl=null;
        int count=0;
        int data;
        try
        {
            // initialize the ORB
            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 = \"Game\";
            GameImpl = GameHelper.narrow(ncRef.resolve_str(name));

            while(true)
            {
            int num;
            System.out.println(\"Enter no=\");
            DataInputStream in=new DataInputStream(System.in);
            num=Integer.parseInt(in.readLine());
            System.out.println(num);

             data=(int) GameImpl.guess(num);
                if(data == -1)
                {
                    System.out.println(\"You enter number is Lower\");
                    count=count+1;
                }
                else if(data == 1)
                {
                    System.out.println(\"You enter number is Higher\");
                    count=count+1;
                }
                else if(data == 0)
                {
                    System.out.println(\"You enter Exact num in trial =\"+count);
                    break;
                }
            }


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


}

 
 
Didn't find what you were looking for? Find more on Corba program of game