![]() |
A Simple CORBA IDL Program 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:
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: 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 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
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
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 Build the Client To build the client, we first need a few of the IDL-generated files. These are: stopwatch\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 If you try to run your client right now, C:\StopwatchClient> java Client 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 Only one copy of osagent needs to be running. Now, things work! C:\StopwatchClient> java Client 2000 Summary
On the server side...
On the client side...
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.
Copyright© Pantheon Systems, Inc., All rights reserved. |