This example is meant to break that barrier, and to show the elementary steps to be taken in writing a CORBA application. To complicate matters a bit, we write a three-language system, consisting of three servers and three clients, written respectively in C++, Java and Prolog.
In the example, it is shown in complete detail how to write clients and servers in the three respective languages, how to write a test program to test all possible combinations and how to manage the configuration of a CORBA application.
The example is based on the original Hello World example
that came with Orbacus 3.1 from
Object-Oriented Concepts
and examples from the experimental
SWI-Prolog CORBA binding
for Orbacus 3.1.
The interface -- IDL
The definition below defines a module universe
that contains an interface named world.
The world interface provides the method hello
and two additional methods ask and tell.
In addition, the world interface supports a method halt,
which was introduced to stop the world on the client's demand.
In actual applications, you do not want to provide
such a method without ensuring that the client has the
right to stop the world.
The implementation of the actual world
server is as simple as its C++ peer.
The implementation of the server object consists of
a collection of predicate definitions (procedures),
that have the interface or class name as a prefix.
Testing CORBA applications is significantly more difficult
than testing stand-alone applications.
This is due on the one hand to the fact that it concerns
a distributed application, involving communication
over a network, and on the other hand to the indirection
that must take place within both the client and the server
to invoke and answer methods over the object request broker
software bus.
As in other areas, in CORBA programming it is good advice
to start small and grow incrementally.
Develop your system in such a way that you can
gradually forget about the wiring, and
concentrate on the application logic instead.
draft version 0.1 (15/7/2001) universe.idl
interface world {
void hello();
void ask(in string x);
string tell();
oneway void halt();
};
};
C++ realization
C++ realization
Broker
C++ broker -- class
public:
broker();
void init(int& argc, char**& argv, int fl=0); // 1 = client
int operator()(); // to run the server
char* ref(corba::object* x); // object_to_string
corba::object* ref(const char* s); // string_to object
corba::object* object(const char* file); // get object IOR file
void refout(const char* f, corba::object* x); // IOR to file
};
C++ broker -- implementation
void broker::init(int& argc, char**& argv, int fl=0) {
_orb = CORBA_ORB_init(argc, argv);
if (!fl) _boa = _orb -> BOA_init(argc, argv);
}
}
int broker::operator()() {
_boa -> impl_is_ready(CORBA_ImplementationDef::_nil());
}
Client
C++ client
broker* _broker = new broker(); // get yourself a broker
try
{
_broker->init(argc,argv); // initialize broker
corba::object* obj = _broker->object("world.ref");
universe_world_var world = universe_world::_narrow(obj);
{ // some object invocations
world -> hello();
world -> ask("How are Clinton's affairs?");
cout << "client:" << world->tell() << endl;
}
} catch(corba::exception ex) { ... }
}
Server
C++ server
{
broker* _broker = new broker(); // get yourself a broker
try
{
_broker->init(argc,argv); // initialize orb and boa
universe_world_var p = new universe_world_srv;
_broker->refout("world.ref", p); // write identity
_broker->operator()(); // run the the world
}
catch(corba::exception& ex) { ... }
}
C++ world implementation
cout << "Hello World!" << endl;
}
void universe_world_srv::ask(const char* s) {
cout << "Asking: " << s << endl;
}
char* universe_world_srv::tell() {
char result[] = "ok";
CORBA_String_var s = (const char*) result;
return s._retn();
}
void universe_world_srv::halt() {
exit(0);
}
Discussion
Java realization
Java realization
Broker
import java.io.*;
public class broker {
boolean _fl = false;
java.applet.Applet _applet = null;
public broker() { }
public broker(boolean fl) { _fl = fl; }
public broker(java.applet.Applet x) {
_applet = x;
init(null);
}
public void init(String[] args) {
if (_applet == null) {
_orb = ORB.init(args,new java.util.Properties());
if (!_fl)
_boa = _orb.BOA_init(args,new java.util.Properties());
} else
_orb = ORB.init(_applet, null);
}
public int operator() { run -- server-only
if (!_fl) _boa.impl_is_ready(null);
return 0; // OK
}
....
};
Client
Java -- client
import java.io.*;
import hush.broker; // see broker.java
public class client
{
public static void main(String args[])
{
broker _broker = new broker(true); // true means client only
try
{
_broker.init(args); // init orb
org.omg.CORBA.Object obj = _broker.object("world.ref");
world world = worldHelper.narrow(obj);
if (world == null)
throw new RuntimeException();
System.out.println("Enter 'h' (hello) or 'x' (exit):");
... // do some requests to the world
} catch(...) { ... }
}
};
Server
Java -- server
import org.omg.CORBA.*;
import java.io.*;
import hush.broker; // see broker.java
public class server
{
public static void main(String args[])
{
broker _broker = new broker();
try
{
_broker.init(args); // create orb en boa;
world_srv p = new world_srv(); // create impl object
_broker.refout("world.ref",p); // write ref
_broker.html("world.htm",p, // create world.htm
" code=universe/applet.class "
+ "width=500 height=300");
_broker.operator(); // run == boa.impl_is_ready(null);
} catch(SystemException ex) {
_broker.print(ex); System.exit(1);
}
System.exit(0);
}
}
Java -- server implementation
import org.omg.CORBA.*;
public class world_srv extends _worldImplBase
{
public void hello() {
System.out.println("Hello World!");
}
public void ask(String msg) {
System.out.println(msg);
}
public String tell() {
String s = new String("ok");
return s;
}
public void halt() {
System.exit(0);
}
}
Discussion
Prolog realization
Prolog realization
...
corba_initialize_orb([], _),
factory(F). // gives a handle to the server object
broker(server(M:I)) :-
...
corba_initialize_server([server,server(test),
timeout(infinite)],Server),
... // create server object
G =.. [_Create,_Server,Self],
call(G),
corba_object_to_string(Self,IOR),
open(IORFile,write,Fd), // write reference to file
format(Fd,'string',[IOR]),
close(Fd),
corba_main_loop(Server). // enter main loop
Client
:- use_module(universe). // see universe.pl
:- [broker]. // include broker
main :-
broker(client(universe:F)), // initialize the broker
assert(client(factory(F))),
run.
run :-
h,
ask('What is the state of Clinton s affairs?'),
write('Type h to say hallo, x to quit.'),nl.
ask(X) :-
client(factory(F)),
write(client(ask(X))),nl,
world_ask(F,X),
world_tell(F,R),
write(client(ans(R))),nl.
h :- client(factory(F)), world_hello(F).
q :- client(factory(F)), world_halt(F), halt.
x :- halt.
Server
main :-
broker(server(universe:world)).
Prolog -- implementation
world_hello(_Self) :-
write('Hello World'),nl.
world_ask(_Self, X) :-
write(asking(X)), nl.
world_tell(_Self,Y) :-
Y = 'logically, ok',
write(telling(Y)),nl.
world_halt(_Self) :-
halt.
Discussion
Configure, make and test
Configure, make and test
Conclusions
Conclusions
[]
readme
course
preface
1
2
3
4
5
6
7
8
9
10
11
12
appendix
lectures
resources
eliens@cs.vu.nl