CORBA Example App: OrChat
An IDL file which defines the relations between a client and a server:
module OrChat {
interface OrChatClientI {
void receiveMessage(in string message);
};
interface OrChatServerI {
void addClient(in OrChatClientI client,
in string username);
void sendMessage(in string message);
};
};
IDL is quite a simple specification language with primitive data types (like string
above) and a basic object-oriented syntax intended to be capable of representing
objects written in any other language. To complement the specification it is necessary to
write Java classes including implementations of the methods defined above.
Here is the code for
the server and here is the code for the client
(OrChatApp includes both the OrChatClient class and some GUI code
generated by the IDE SuperCede).
(Some ideas for the networking code were borrowed from Luke Cassady-Dorion's article in JavaPro
3/98 and Orfali/Harkey's book Client/Server Programming with Java and CORBA).
Essentially each client is registered by the server in a hashtable and whenever
a client sends a message to the server it is broadcasted along with the client's
username to all clients registered with the server.
There are two things to notice about this code:
The extra Java classes used need to be created by an IDL-to-Java converter
such as idltojava from JavaSoft or idl2java from Visigenic. At least four extra
classes are generated for each interface defined in the IDL file. I used Visigenic's
idl2java converter, and what follows is an account of my experiences with Visigenic's
downloadable runtime CORBA environment, VisiBroker (formerly Black Widow).
This system does not seem to provide the org.omg.CORBA classes in a form that can
be imported by a Java file and then compiled ordinarily; instead it is necessary
to use VisiBroker's own compiler vbjc and then VisiBroker's own interpreter
vbj
(though these executables seem to call javac and java internally).
Before running the server and the client it is necessary to set up some environment
variables (on Windows the VisiBroker setup program is a help) and start up the VisiBroker
SmartAgent, an object location service. Some extra tools are provided though these are
not strictly necessary:
After writing and compiling the code, running idl2java, and starting osagent, I then
called vbj OrChatServer from one DOS window and started client sessions in other
windows (vbj OrChatApp columbus and vbj OrChatApp magellan) getting the results shown
in the screenshot below.
OrChatServerIImplBase is the superclass for
OrChatServer.
OrChatClientIImplBase is the superclass for OrChatClient.
OrChatServerIHelper is used in the client to get the server object.