field-theory.org
the blog of wolfram schroers

From a local to a distributed object

The source code can be downloaded as a gzipped .tar-file.

This is the first part of the CORBA tutorial. It implements a distributed object with a single method. This example is inspired by the Orbit2 echo example, using the naming conventions. It demonstrates the workflow of going from a local object to a remote one.

The source code has been developed and tested using the mico ORB on MacOS X. On other systems, the Makefile(s) may need to be adjusted properly. The files can also be downloaded on the software section of this site.

Table of contents:

The local object
Introducing the remote object
Implementing the remote object
Implementing the client
Compiling and running the code

The local object

We start with a very simple linear, local C++ program called direct.cc. It is the object-oriented form of a Hello, world! program. The core part is a call to the method echoString of the instance hi of the object Echo_impl:

36   // Execute the "echoString" method
37   hi->echoString("Hello, world.");

Apart from this call, the main program only needs to declare and instantiate hi. The implementation of the class Echo_impl is straightforward as well and (hopefully) does not need any further explanation.

Introducing the remote object

Let's say we want to have a make Echo_impl a remote object whose echoString method can be reached by clients from over the network. The design of the interface is straightforward; casting the interface in a language-independent IDL is done in echo.idl which looks as follows:

 1 // MICO example program - The Echo object
10 interface Echo
11 {
12   void echoString(in string input);
13 };

The syntax of the IDL language is actually similar to that of C++. However, the crucial difference is that it does not define an actual object, it just defines an interface. The method echoString returns nothing (the void data type) and takes a single string as the only input parameter. The interface keyword encompasses the individual method declarations. See the manual at Iona for a thorough overview to the IDL language.

Implementing the remote object

With this interface declared, we can run the IDL compiler and generate client stubs and server templates: echo.cc, echo.h, and echo_skel.cc (these are included, but must not be edited!), furthermore echo_impl.h as well as echo_impl.cc (these are templates for the actual objects that we have to provide). In the downloaded sources, the latter two files – echo_impl.h and echo_impl.cc – are already written by me with similar logic as in direct.cc.

In addition, I have written a server in server.cc. The server has code that initializes the CORBA middleware, instantiates an object of class Echo_impl as echo_obj, writes out information for finding the object (like the IP address, port number etc. – we don't need to know the exact details) and then hands over control to the CORBA system. The only thing that this program has in common with the former implementation of Echo_impl in direct.cc is the instantiation of the object. However, the rest is essentially boilerplate code that does not depend on the details of the object or the interface it provides. So we can get pretty far without understanding how exactly it works under the hood!

Implementing the client

The client written in client.cc is actually very similar to the local implementation — there is some minimal initialization code, then an object of type Echo_var (which acts as a proxy to the “real” object provided in server.cc) is instantiated and finally it is used just like in the local version:

34   // Execute the "echoString" method
35   hi->echoString("Hello, world.");

From the client's point of view, not much has changed except for a few lines of initialization code!

Compiling and running the code

The files can be compiled with the accompanying Makefile by typing make. After the files are compiled, we can run the server in one window by typing ./server and the client in another window by typing ./client.

Using CORBA example code 1

The client can be run as often as needed from any other window as long as the corresponding file ior.dat is in the current directory. We will learn about this later in part four of this tutorial.

Congratulations! You have now successfully completed your first distributed object tutorial!

You can continue reading part two now.