Java Tutorial

 
 
 
 
// CountClient.java  

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

class CountClient { 

  static Count countImpl;

  public static void main(String args[])
  { try
    { // Initialize the ORB
      System.out.println(\"Initializing the ORB\");
      org.omg.CORBA.ORB orb = org.omg.CORBA.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 = \"Count\";
        countImpl = CountHelper.narrow(ncRef.resolve_str(name));

        System.out.println(\"Obtained a handle on server object: \" + countImpl);

      // Set sum to initial value of 0
      System.out.println(\"Setting sum to 0\");
      countImpl.sum((int)0);

      // Calculate Start time
      long startTime = System.currentTimeMillis();

      // Increment 1000 times
      System.out.println(\"Incrementing\");
      for (int i = 0 ; i < 1000 ; i++ )
      { countImpl.increment();
      }

      // Calculate stop time; print out statistics
      long stopTime = System.currentTimeMillis();
      System.out.println(\"Avg Ping = \"
                       + ((stopTime - startTime)/1000f) + \" msecs\");
      System.out.println(\"Sum = \" + countImpl.sum());
    } catch(Exception e)
    { System.err.println(\"System Exception\");
      System.err.println(e);
    }
  }
}


// CountServer.java: The Count Server main program

import Counter.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;

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

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

      // Create the Count object
      CountImpl count = new CountImpl(\"My Count\");

      // get object reference from the servant
      org.omg.CORBA.Object ref = rootpoa.servant_to_reference(count);
      Count href = Counter.CountHelper.narrow(ref);

      // get the root naming context
      // NameService invokes the name service
      org.omg.CORBA.Object objRef =
          orb.resolve_initial_references(\"NameService\");
      // Use NamingContextExt which is part of the Interoperable
      // Naming Service (INS) specification.
      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

      // bind the Object Reference in Naming
      String name = \"Count\";
      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(e);
      }
   }
}


// CountClientApplet.java  Applet Client, VisiBroker for Java

import Counter.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.awt.*;

public class CountClientApplet extends java.applet.Applet
{ private TextField countField, pingTimeField;
  private Button runCount;
  private Counter.Count counter;
  public void init()
  { // Create a 2 by 2 grid of widgets.
    setLayout(new GridLayout(2, 2, 10, 10));

    // Add the four widgets, initialize where necessary
    add(new Label(\"Count\"));
    add(countField = new TextField());
    countField.setText(\"1000\");
    add(runCount = new Button(\"Run\"));
    add(pingTimeField = new TextField());
    pingTimeField.setEditable(false);

    try
    { // Initialize the ORB.
      showStatus(\"Initializing the ORB\");
      org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(this, null);

      // Bind to the Count Object
        // 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 = \"Count\";
        counter = CountHelper.narrow(ncRef.resolve_str(name));

      showStatus(\"Binding to Count Object\");

    } catch(Exception e)
    {
      showStatus(\"Applet Exception\" + e);
      e.printStackTrace(System.out);
    }
  }

  public boolean action(Event ev, java.lang.Object arg)
  { if(ev.target == runCount)
    { try
      { // Set Sum to initial value of 0
        showStatus(\"Setting Sum to 0\");
        counter.sum((int)0);

        // get data from and set value of applet fields
        showStatus(\"Incrementing\");
        int stopCount = Integer.parseInt(countField.getText());
        pingTimeField.setText(\" \");

        // Calculate Start time
        long startTime = System.currentTimeMillis();

        // Increment stopCount times
        for (int i = 0 ; i < stopCount ; i++ )
        { counter.increment();
        }

        // Calculate stop time; show statistics
        long stopTime = System.currentTimeMillis();
        pingTimeField.setText(\"Avg Ping = \"
                  + Float.toString((float)(stopTime- startTime)/stopCount)
                  + \" msecs\");
        showStatus(\"Sum = \" + counter.sum());
      } catch(Exception e)
      { showStatus(\"System Exception\" + e);
      e.printStackTrace();
      }
      return true;
    }
    return false;
  }
}


// CountImpl.java: The Count Implementation

class CountImpl extends Counter.CountPOA
{
  private int sum;

  // Constructors
  CountImpl(String name)
  { super();
    System.out.println(\"Count Object Created\");
    sum = 0;
  }

  // get sum
  public  int sum()
  { return sum;
  }

  // set sum
  public  void sum(int val)
  { sum = val;
  }

  // increment method
  public int increment()
  { sum++;
    return sum;
  }
}


// Count Client Applet

<h1>Count Client Applet</h1>
<hr>
<center>
<APPLET CODE=CountClientApplet.class WIDTH=300 HEIGHT=60
        CODEBASE=.>
        <PARAM name=\"org.omg.CORBA.ORBInitialHost\" value=MCA329>
        <PARAM name=\"org.omg.CORBA.ORBInitialPort\" value=1050>
</APPLET>
</center>
<hr>

    Related Post:
  1. Game application in CORBA for which player will guess a number between 1 to 100, which will be compared to the random number generated by the class

  2. File handling program to count number of characters in a file

  3. Pass the marks of 10 students from the client side design a servlet, which takes these marks and returns the number of students who obtained first cls

  4. Program to show the use of Labeled Continue Statement

  5. 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

  6. Program that will count the number of lines in each file that is specified on the command line

  7. Program to show the use of operator (&lt;&lt;) Shift Left filling with zero from the right

  8. RMI application for counting factorial of a given number.

  9. Write a class whose objects holds a current value and have a method to add that value, printing the new value

  10. Program to display following things on the client machine a)date and time of the server machine b)date and time of the client machine and difference

  11. Program to show an example of creating a class and using its object

  12. Program to show the use of Do-While Loop

  13. Program to show the use of Nested If-Else Statements

  14. Program to print a string on Standard Output Device

  15. Program of binary search tree

  16. Program to show an example of using multiple classes in a program

  17. Program to show the use of If Statement

  18. RMI application for getting the final total price of the shopping list with some of the following items and quantities chosen by the user

  19. RMI based application, which reads a file with list of marks of student from a client, send it to server and find how many students having distinct

  20. CORBA program for displaying the date and time of the server machine, client machine and the difference between these two date and time

 
 
Didn't find what you were looking for? Find more on Corba program of Count Client Applet, IDL,Server, Client