Wednesday, 10 October 2012


Implementing CORBA Services using java to java technology.

A) Find factorial of the number.
B) Find the Reverse of the number and string
C) Find the square of the number

(A) Find factorial of the number

Code:

FactorialIntf.idl
module FactApp
{
 interface FactorialIntf
  {
 long factorial(in long num);
 oneway void shutdown();
 };
};

FactClient.java
import FactApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;

public class FactClient{

  public static void main(String args[]){
 
    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 = "Hello";
      FactorialIntf helloImpl = FactorialIntfHelper.narrow(ncRef.resolve_str(name));

      System.out.println("Obtained a handle on server object: " + helloImpl);
System.out.println("Enter the number");
int num=Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());

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

FactServer.java
//Server Program
import FactApp.*;
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 FactImpl extends FactorialIntfPOA{
  private ORB orb;

  public void setORB(ORB orb_val){
    orb = orb_val;
  }
 
  public int factorial(int num1){
   int facto=1;
   if(num1==0)
      {
       return 1;
      }
      else
      {
          while(num1>0)
          {
              facto=facto*num1;
              num1--;
          }
          return facto;
      }
       
  }
 
  public void shutdown(){
    orb.shutdown(false);
  }
}

public class FactServer{

  public static void 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
      FactImpl helloImpl = new FactImpl();
      helloImpl.setORB(orb);

      // create a tie, with servant being the delegate.
      FactorialIntfPOATie tie = new FactorialIntfPOATie(helloImpl, rootpoa);

      // obtain the objectRef for the tie
      // this step also implicitly activates the
      // the object
      FactorialIntf href = tie._this(orb);
       
      // get the root naming context
      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
     
      // Use NamingContextExt which is part of the Interoperable
      // Naming Service specification.
      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

      // bind the Object Reference in Naming
      String name = "Hello";
      NameComponent path[] = ncRef.to_name( name );
      ncRef.rebind(path, href);

      System.out.println("HelloServer 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("HelloServer Exiting ...");
   
  }
}

Output:




(B) Find the Reverse of the number and string

Code:

ReverseIntf.idl
module ReverseApp
{
 interface ReverseIntf
  {
 string getReverseString(in string str1);
 long getReverseNumber(in long num1);
 oneway void shutdown();
 };
};

ReverseClient.java
import ReverseApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;

public class ReverseClient{

  public static void main(String args[]){
 
    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 = "Hello";
      ReverseIntf helloImpl = ReverseIntfHelper.narrow(ncRef.resolve_str(name));

      System.out.println("Obtained a handle on server object: " + helloImpl);
      System.out.println("Enter the String to reverse");
      String  strn=(new BufferedReader(new InputStreamReader(System.in)).readLine());

     System.out.println("Enter the number");
     int num=Integer.parseInt(new BufferedReader(new                                InputStreamReader(System.in)).readLine());
    System.out.println("Reverse of the string is");
    System.out.println(helloImpl.getReverseString(strn));
    System.out.println("Reverse of the number is "+helloImpl.getReverseNumber(num));
      helloImpl.shutdown();
      }
     
    catch (Exception e) {
      System.out.println("ERROR : " + e) ;
      e.printStackTrace(System.out);
    }
  }
}

ReverseServer.java
//Server Program
import ReverseApp.*;
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 ReverseImpl extends ReverseIntfPOA{
  private ORB orb;

  public void setORB(ORB orb_val){
    orb = orb_val;
  }
 
  public String getReverseString(String str){
    String str2="";

    for(int i=str.length()-1;i>=0;i--)
      {
        str2=str2+str.charAt(i);
      }
        return str2;
  }
   public int  getReverseNumber(int num1)
    {
        int temp;
        String finNum="";


        while(num1>0)
        {
            finNum=finNum+num1%10+"";
            num1=num1/10;
           
        }
        int num=Integer.parseInt(finNum);
        return num;
    }
  public void shutdown(){
    orb.shutdown(false);
  }
}

public class ReverseServer{

  public static void 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
      ReverseImpl helloImpl = new ReverseImpl();
      helloImpl.setORB(orb);
 // create a tie, with servant being the delegate.
      ReverseIntfPOATie tie = new ReverseIntfPOATie(helloImpl, rootpoa);

      // obtain the objectRef for the tie
      // this step also implicitly activates the
      // the object
      ReverseIntf href = tie._this(orb);
       
      // get the root naming context
      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
     
      // Use NamingContextExt which is part of the Interoperable
      // Naming Service specification.
      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

      // bind the Object Reference in Naming
      String name = "Hello";
      NameComponent path[] = ncRef.to_name( name );
      ncRef.rebind(path, href);

      System.out.println("HelloServer 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("HelloServer Exiting ...");
   
  }
}

Output:





C) Find the square of the number

Code:

SquareIntf.idl
module SquareApp
{
 interface SquareIntf
  {
 long getSquare(in long num);
 oneway void shutdown();
 };
};

SquareClient.java
import SquareApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;

public class SquareClient{

  public static void main(String args[]){
 
    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 = "Hello";
      SquareIntf helloImpl = SquareIntfHelper.narrow(ncRef.resolve_str(name));

      System.out.println("Obtained a handle on server object: " + helloImpl);
      System.out.println("Enter the number");
     int num=Integer.parseInt(new BufferedReader(new               InputStreamReader(System.in)).readLine());



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

SquareServer.java
import SquareApp.*;
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 SquareImpl extends SquareIntfPOA{
  private ORB orb;

  public void setORB(ORB orb_val){
    orb = orb_val;
  }
 
  public int getSquare(int num1){
    return num1*num1;
       
  }
 
  public void shutdown(){
    orb.shutdown(false);
  }
}

public class SquareServer{

  public static void 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
      SquareImpl helloImpl = new SquareImpl();
      helloImpl.setORB(orb);

      // create a tie, with servant being the delegate.
      SquareIntfPOATie tie = new SquareIntfPOATie(helloImpl, rootpoa);

      // obtain the objectRef for the tie
      // this step also implicitly activates the
      // the object
      SquareIntf href = tie._this(orb);
       
      // get the root naming context
      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
     
      // Use NamingContextExt which is part of the Interoperable
      // Naming Service specification.
      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

      // bind the Object Reference in Naming
      String name = "Hello";
      NameComponent path[] = ncRef.to_name( name );
      ncRef.rebind(path, href);

      System.out.println("HelloServer 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("HelloServer Exiting ...");
   
  }
}

Output:



Related Posts :



0 comments:

Confused? Feel free to ask

Post a Comment