Microsoft & .NETVisual C#How to write CORBA objects

How to write CORBA objects

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.



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:

  • setSwitch()
    to turn the fan on and off
  • isSwitch()
    to report the status of the fan.
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
modifier to the

b
parameter. It signals an input parameter, the default for Java. If IDL is not close enough to Java for your taste, Visigenic has another tool, Caffeine, that converts Java interfaces to IDL. With Caffeine, you can write CORBA objects entirely in Java but, because it is vendor-specific, we won’t use it here.

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

    org.omg.CORBA.ORB.init()
    to initialize the ORB.
  • 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

    bind()
    method replaces Java’s

    new
    keyword for distributed objects.

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()
), a standard interface to communicate with the ORB.

Next, it creates an instance of FanImpl with the standard Java

new
keyword and exports it to the ORB by calling

obj_is_ready()
. Finally, the server notifies the ORB that it is ready and waiting for requests through

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
to be effective. Note: the -C option is only for use with Windows, not for Unix. Without it, I could not get the osagent to run properly on NT. Start one or more servers as background processes:

start java FanServer FanOven01
on Windows or

java FanServer FanOven01&
on Unix where “FanOven01” is the name of the CORBA server. It need not match the computer name.

Clients remotely administer servers like:


java FanClient on FanOven01

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

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories