CORBA is a great tool for effortlessly writing distributed, object-oriented applications. Unlike competitors such as RMI or DCOM, CORBA is both cross-platform and cross-language.
For the developer, CORBA provides a clean, object-oriented interface to low-level network access. It makes writing distributed applications little more difficult than writing stand-alone applications.
CORBA recognizes two roles for applications: client and server. Servers expose objects. Clients remotely invoke methods on these objects. Note that an application can play both roles; that is, a server may also act as a client to another server.
Client and server may reside on different computers. They may run on different operating systems, and they may even be written in different languages. All such variations are transparent to the developer.
CORBA is implemented by several vendors in Object Request Broker (ORB). The code in this article was written with VisiBroker for Java 3.1. VisiBroker is interesting because it is distributed with Netscape ONE (Open Network Environment). A free demonstration copy that compiles the code presented here is available from Visigenic. Although conceptually similar, ORBs from other vendors may differ in details. Consult your vendor documentation where appropriate.
Example: a fan controller
As an illustration, we will take a practical example and build a distributed industrial fan controller. In industrial environments, it is often important to remotely control devices. Indeed, product lines in factories are managed from a central console.
The distributed fan controller consists of two elements:
- the server, which actually interfaces with the device and exposes a CORBA object. (For the sake of simplicity, we won’t build a real fan but will simulate one with
.)println()
- the client, which remotely controls the server.
IDL interface
CORBA objects are described by IDL interfaces. Just like Java interfaces, they are abstract descriptions of classes. Unlike Java, though, IDL is not a programming language. The interfaces are compiled in the target language — Java, in this case — for implementation. IDL compilers are available for the most popular languages: C++, Smalltalk and Cobol — enabling cross-language development.
Logically enough, we start by writing an IDL interface (see listing 1). It defines two methods:
-
to turn the fan on and offsetSwitch()
-
to report the status of the fan.isSwitch()
Listing 1: Fan.idl
interface Fan
{
void setSwitch(in boolean b);
boolean isSwitch();
}
Luckily, IDL syntax is close to Java. The only difference here is the
in |
b |
To compile the IDL interface in Java, use the idl2java compiler:
idl2java Fan.idl |
This will generate many Java files, but we only need Fan.java, _FanImplBase.java, _st_Fan.java and FanHelper.java.
Writing the client
The client is straightforward (see listing 2). It connects to the server and executes one command remotely. It takes the server name and the command from the command line. There are only two lines specific for CORBA:
- the client calls
to initialize the ORB.org.omg.CORBA.ORB.init()
- the client calls FanHelper.bind() to obtain a reference to a remote object. FanHelper was generated by the IDL compiler. In its simplest form, CORBA’s
method replaces Java’sbind()
keyword for distributed objects.new
The rest is standard Java code! Because the ORB takes care of network communication, remote objects behave like local ones.
Listing 2: FanClient.java
public class FanClient
{
static public void main(String[] args)
{
// supply default values if required
String command = args.length < 1 ? "query" : args[0], servername = args.length < 2 ? "default_FanServer" : args[1]; // initialize the ORB and find the server org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null); Fan remoteFan = FanHelper.bind(orb,servername); // from now on, "normal" OO code if(command.equalsIgnoreCase("on")) remoteFan.setSwitch(true); else if(command.equalsIgnoreCase("off")) remoteFan.setSwitch(false); else System.out.println(servername + ": " + (remoteFan.isSwitch() ? "on" : "off")); } }
Writing the server
The server is not much more difficult. First, we implement the Fan interface. Listing 3 simulates a fan through
println() |
Listing 3: FanImpl.java
public class FanImpl extends _FanImplBase
{
private boolean switchValue = false;
public FanImpl(String objectname)
{
super(objectname);
System.out.println(objectname + ” created”);
}
public void setSwitch(boolean b)
{
switchValue = b;
System.out.println(“turn ” +
(switchValue ? “on” : “off”));
}
public boolean isSwitch()
{
System.out.println(“query: ” +
(switchValue ? “on” : “off”));
return switchValue;
}
}
Again, there’s little CORBA-specific code. The only indication that FanImpl is a distributed object is that it extends _FanImplBase, a class generated by the IDL compiler, instead of implementing the Fan interface directly.
The very last step is to write the server. In its simplest form, it looks like Listing 4.
Listing 4: FanServer.java
public class FanServer
{
static public void main(String[] args)
{
// default values, if required
String servername = args.length < 1 ? "default_FanServer" : args[0]; // initialize the ORB org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null); // initialize the BOA org.omg.CORBA.BOA boa = orb.BOA_init(); // create the fan object FanImpl fan = new FanImpl(servername); // export the newly created object boa.obj_is_ready(fan); // wait for incoming requests boa.impl_is_ready(); } }
This contains more CORBA-specific code, but it is easy and repetitive. FanServer initializes the ORB, like the client. It goes on to initialize the BOA (
boa_init() |
Next, it creates an instance of FanImpl with the standard Java
new |
obj_is_ready() |
impl_is_ready() |
Wrapping it up
Before compiling the application, make sure VisiBroker packages are in the CLASSPATH of your Java compiler. Don’t forget to compile Fan.java, _FanImplBase.java, _st_Fan.java and FanHelper.java (the files generated by the IDL compiler) as well.
For testing, VisiBroker SmartAgent must be active. I have found the command osagent
-C |
start java FanServer FanOven01 |
java FanServer FanOven01& |
Clients remotely administer servers like:
java FanClient on FanOven01java FanClient query FanOven01
Watch the console of the server during testing and experiment with different machines.
Conclusion
Writing distributed applications with CORBA is easy. It hides the drudgery of network programming behind an object-oriented interface. CORBA cross-language and cross-platform support makes it ideal to interface with legacy applications written in C++ or Cobol.Link on this article:
Benoît Marchal runs his own consulting company, PineappleSoft. His interests are in distributed applications, object-oriented programming and design, handhelds and computer languages. He also enjoys teaching and writing. bmarchal@pineapplesoft.com.