Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 65: Measure data transfer speeds via Sun's ORB in JDK 1.2 beta 4

Determine effective data transfer speeds between client and server using CORBA

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 3

   public void actionPerformed (ActionEvent evt) {
        String what = evt.getActionCommand();
        System.out.println ("what = " + what);
        long time_start;
        int n, i, size[] = {100, 500, 1000, 5000,
                        10000, 50000, 100000, 500000,
                        1000000, 5000000};
        if ("send byte".equals(what)) {
           byte[] array;
           for (i=0; i<10; i++) {
                n = size[i];
                System.out.println("Allocating array: n=" + n);
                try { array = new byte[n]; }
                catch (java.lang.OutOfMemoryError ex) {
                        System.out.println("Ouch!  Could not allocate:
" + n);
                        break;
                }
                time_start = System.currentTimeMillis();
                viewRef.send_byte(time_start, n, array);
           }
        }
        if ("send integer".equals(what)) {
           int[] array;
           for (i=0; i<10; i++) {
                n = size[i];
                System.out.println("Allocating array: n=" + n);
                try { array = new int[n]; }
                catch (java.lang.OutOfMemoryError ex) {
                        System.out.println("Ouch!  Could not allocate:
" + n);
                        break;
                }
                time_start = System.currentTimeMillis();
                viewRef.send_int(time_start, n, array);
           }
        }
        if ("send double".equals(what)) {
           double[] array;
           for (i=0; i<10; i++) {
                n = size[i];
                System.out.println("Allocating array: n=" + n);
                try { array = new double[n]; }
                catch (java.lang.OutOfMemoryError ex) {
                        System.out.println("Ouch!  Could not allocate:
" + n);
                        break;
                }
                time_start = System.currentTimeMillis();
                viewRef.send_double(time_start, n, array);
           }
        }
        if ("send float".equals(what)) {
           float[] array;
           for (i=0; i<10; i++) {
                n = size[i];
                System.out.println("Allocating array: n=" + n);
                try { array = new float[n]; }
                catch (java.lang.OutOfMemoryError ex) {
                        System.out.println("Ouch!  Could not allocate:
" + n);
                        break;
                }
                time_start = System.currentTimeMillis();
                viewRef.send_float(time_start, n, array);
           }
        }
   }
}


The server consists of two java files -- one with the main() method and the other with the class that implements the methods defined in the idl file.

The main() method creates a ViewServant object, registers it with the Naming Service, and allocates one frame.

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
public class ViewServer extends Applet {
   boolean appletFlag = true;
   /**
    * Sets the "applet" flag which is used to determine
    * whether or not this program is running as an applet.
    */
   public void setAppletFlag(boolean flag) {
      appletFlag = flag;
   }
    /**
     * Calls the various methods necessary to initialize the
     * program.
     */
    public void init() {
        // Set the layout manager
        setLayout(new BorderLayout());
   }
    /**
     *  Inner class used to "kill" the window when running as
     *  an application.
     */
    static class killAdapter extends WindowAdapter {
       public void windowClosing(WindowEvent event) {
          System.exit(0);
       }
    }
    /**
     *  Used when running as an application.
     */
    public static void main(String[] args) {
       ViewServer drag = new ViewServer();
       drag.setAppletFlag(false);
       drag.init();
       Frame frame = new Frame();
       frame.setSize(400, 400);
       frame.add("Center", drag);
       frame.addWindowListener(new killAdapter());
       frame.setVisible(true);
        System.out.println ("ViewServer: contacting the ORB");
      try{
            // create and initialize the ORB
            ORB orb = ORB.init(args, null);
            // create servant and register it with the ORB
            ViewServant viewRef = new ViewServant();
            orb.connect(viewRef);
            // get the root naming context
            org.omg.CORBA.Object objRef =
                orb.resolve_initial_references("NameService");
            NamingContext ncRef = NamingContextHelper.narrow(objRef);
            // bind the Object Reference in Naming
            NameComponent nc = new NameComponent("ViewServer",
"server");
            NameComponent path[] = {nc};
            ncRef.rebind(path, viewRef);
            // wait for invocations from clients
            System.out.println ("ViewServer: waiting for clients.");
            java.lang.Object sync = new java.lang.Object();
            synchronized (sync) {
                sync.wait();
            }
        } catch (Exception e) {
            System.err.println("ERROR: " + e);
            e.printStackTrace(System.out);
        }
    }
}


The ViewServant class implements the methods declared in the idl file. Each method in this class captures the current time and subtracts the timestamp sent by the client from the current time.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • More information on CORBA can be found at the OMG site http://www.omg.org