About Us
Education and Training
Consulting
Gifts
Contacting Us
Employment
Journal
Journal

Home

The Pantheon Systems Journal

A Simple CORBA IDL Program
by Niranjan Ramakrishnan

In the last article we examined the IDL, the Interface Definition Language for CORBA program specification. We used a simple example - a stopwatch, to show what an interface means. In this article we shall see how a simple CORBA program can be written using IDL, continuing with the same example. (For the purpose of this lab we shall use Visibroker for Java from Visigenic Software. A similar program can be written using any other ORB). The great thing, of course, is that the IDL code remains exactly the same.

Let us start with the interface specification for the stopwatch. We shall describe the interface in IDL, compile the IDL file into Java, build the Java server and client, and run it. The recipe for building any CORBA program is pretty standard:

  1. Write the .idl file
  2. Compile it with an idl compiler (in our case, idl2java)
  3. Use the output to build the server and client

The IDL Specification
Here's the IDL file, stopwatch.idl:

module stopwatch { 
    interface Stopwatch { 
        void start(); // Start the stopwatch 
        void stop(); // Stop the stopwatch 
        readonly attribute double elapsedTime; 
          // Attribute to store the elapsed time between 
          // the last start-stop pair. This statement 
          // generates no variable, 
          // only a pair of set/get 
          // statements for this variable. 
     }; 
}; 

To compile this idl file into Java, we use a command-line program called idl2java, thus:

C:\CORBA\Examples\Ex1> idl2java -no_tie -no_examples -no_comments stopwatch.idl
Creating: stopwatch
Creating: stopwatch\Stopwatch.java
Creating: stopwatch\StopwatchHolder.java
Creating: stopwatch\StopwatchHelper.java
Creating: stopwatch\_st_Stopwatch.java
Creating: stopwatch\_sk_Stopwatch.java
Creating: stopwatch\_StopwatchImplBase.java
C:\CORBA\Examples\Ex1>

Don't worry too much about the flags, -no_tie, -no_comments and -no_examples. These mainly serve to reduce the number and bulk of the files generated.

Understanding the files
First, note that all the generated files are put into a single directory called 'stopwatch'. This is because the stopwatch classes and interfaces, which is what these files represent, are all in a single package called stopwatch.
The key file is stopwatch.java, which contains the Java interface definition of the stopwatch interface specified in the .idl file. Recall that interface in IDL stands for a Java interface. Here's stopwatch.java:

  package stopwatch; 

  public interface Stopwatch 
          extends org.omg.CORBA.Object { 
      public void start(); 
      public void stop(); 
      public double elapsedTime(); 
  } 

As you can see, this is a faithful replication, in Java, of the interface we specified in IDL. If you are wondering why there is no member variable corresponding to the elapsedTime attribute, it is because interfaces are not allowed to have any (non-static) member variables. Instead, the IDL compiler generates mutator (set) and accessor (get) methods for the IDL attribute. The reason this particular interface only has an accessor method is because we specified in the IDL specification that it should be a readonly.

After the interface
We now have an interface. We need a real class to implement it, to instantiate an instance of it on the server, to provide clients with a stopwatch service. The IDL compiler generates a java class which implements this interface, as well as knows how to create the CORBA object with a given name. Here's _StopwatchImplBase.java:

    package stopwatch; 
    abstract public class _StopwatchImplBase 
             extends org.omg.CORBA.portable.Skeleton 
             implements stopwatch.Stopwatch { 
       protected 
      _StopwatchImplBase(java.lang.String name) { 
          super(name); 
       } 
       protected _StopwatchImplBase() {} 
     
      // Other generated methods, 
      // all having to do with publishing 
      // your three methods, omitted here... 
    } 

Writing the server class
Now we can define our server class. This is a concrete class, derived from _StopwatchImplBase, called StopwatchServer:

   import stopwatch.*;
   import java.util.*;
   class StopwatchServer 
                  extends _StopwatchImplBase {
      public StopwatchServer(java.lang.String s) {
         super(s);
      }
      public void stop() {
         cal.setTime(new Date());
         stopTime = (double) cal.getTime().getTime();
      } 
      public void start() {
         cal.setTime(new Date());
         startTime = (double) cal.getTime().getTime();
      } 
      public double elapsedTime(){
         double elapsed = (stopTime - startTime);
         startTime = stopTime = 0.0; 
             // reset,  return elapsed time;
      }
      // Attributes
      double startTime = 0.0;
      double stopTime = 0.0;
      Calendar cal = Calendar.getInstance();
    }

Create the Server class
The server is now ready to run. Here's server program, Server.java, to create our StopwatchServer object:

   class Server {
      public static void main(String[] args) {
         System.out.println("Stopwatch Server up...");
         try { 
            org.omg.CORBA.ORB orb = 
                   org.omg.CORBA.ORB.init(); 
              // Create and initialize the ORB
            org.omg.CORBA.BOA boa = 
                        orb.BOA_init();
              // Create/initialize the 
              // Basic Object Adaptor
            StopwatchServer ss = 
                  new StopwatchServer("Stopwatch");
              // Create an object of type StopwatchServer 
            boa.obj_is_ready(ss);
              // Tell the BOA that the created object 
              // is ready. If you create multiple 
              // server objects (of same or
              // of different types) you'll need 
              // one such statement for each.
            boa.impl_is_ready();
              // One statement for the entire BOA, 
              // saying the implementation is all 
              // set up.
          }
          catch (Exception e) { 
              System.out.println(e);
          }
      }
   }

Build and Run the Server

C:\CORBA\Examples\Ex1> javac Server.java
C:\CORBA\Examples\Ex1> java Server
Stopwatch Server up...

Build the Client
To build the client, we first need a few of the IDL-generated files. These are:

stopwatch\Stopwatch.java
stopwatch\StopwatchHelper.java
stopwatch\_st_Stopwatch.java

The first, Stopwatch.java, is just the interface in Java. The second, StopwatchHelper.java, allows us to bind to the object. The third, _st_Stopwatch.java, contains stubs for the methods in the interface. We just made a copy of the stopwatch directory into our client directory and deleted the rest of the files. Here's our client program, Client.java:

   import stopwatch.*;
   class Client {
      public static void main(String[] args) { 
         try {
            org.omg.CORBA.ORB orb = 
                 org.omg.CORBA.ORB.init();
            Stopwatch stopwatch = 
                StopwatchHelper.bind(orb, "Stopwatch");
            stopwatch.start();
            int sleepTime = Integer.parseInt(args[0]);
            Thread.sleep(sleepTime);
            stopwatch.stop();
            System.out.println(stopwatch.elapsedTime());
         }
         catch (Exception e) { 
            System.out.println(e);
         }
      }
   }

You can now build the client (obviously, you need to put the abridged stopwatch package into a place where your client can find it):

C:\StopwatchClient> javac Client.java
C:\StopwatchClient
>

If you try to run your client right now,

C:\StopwatchClient> java Client
org.omg.CORBA.COMM_FAILURE[completed=MAYBE, reason=Could not reconnect to OSAgent]

The reason: we are missing an agent that locates objects on the network. This is called (in Visigenic) osagent, and is essential to bind to the server object. To run this in the background,

C:\StopwatchClient> osagent -C
C:\StopwatchClient>

Only one copy of osagent needs to be running. Now, things work!

C:\StopwatchClient> java Client 2000
2030
C:\StopwatchClient>

Summary
What did we do? We can list our efforts in two parts.

On the server side...

  1. We wrote a server specification in IDL.
  2. We compiled this specification into Java, generating a package of .java files.
  3. We wrote an implementation, in Java, per the specification.
  4. We wrote a Server engine that would create and register the server object.
  5. We ran the server engine, so it would create the client and be ready to take client requests.

On the client side...

  1. We wrote a client program to bind to the server object and invoked operations on it.
  2. We ran the locator service (Note: if osagent were already running, we would not have to run it again).
  3. We ran the client program with a suitable argument.

This concludes our simple CORBA application in Java. In the upcoming articles, we shall look at other CORBA issues, as well as how CORBA's competition - COM/DCOM - works, and addresses the same problems.


Top
Journal Archives | Send Feedback

Copyright© Pantheon Systems, Inc., All rights reserved.