C++ realization

Instructor's Guide

Introduction Interface Broker C++ Java Prolog Configure Conclusions
The C++ realization consists of a broker that gives access to the ORB for the client and to both the ORB and the BOA for the server. Apart from a client and server, we need an implementation for the world, which is given in respectively the world-srv.h and world-srv.c files.

C++ realization

slide: C++ realization


The broker class in C++ provides a constructor, an initialization method, the operator that starts the server's event loop, and methods to convert strings to object references and methods to read and write object identifiers.

C++ broker -- class

  class broker { 
public: broker(); void init(int& argc, char**& argv, int fl=0);
1 = client
int operator()();
to run the server
char* ref(corba::object* x);
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

slide: C++ broker -- class

Below, the implementation is given of the constructor, which needs to do nothing, the init method, that initializes the ORB, and the BOA in the case of a server.

C++ broker -- implementation

  broker::broker() { }  
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()); }

slide: C++ broker -- implementation

As said before, the broker class provides only shorthands and abstractions from vendor-specific code.


The code snippet below shows how the client creates and initializes a broker object, how it obtains an object reference, and also some calls to the world server, including hello and a request to be informed about Clinton's affairs, which certainly had some relevance at the time of writing this.

C++ client

      int main(int argc, char* argv[], char*[]) { 
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) { ... } }

slide: client

Note that the object that is obtained from world.ref must be dynamically downcasted to its actual type.


The server code is similar as far as the creation and initialization of the broker is concerned, but instead of creating an object from a string identifier, it writes the identity of the server object created to the file world.ref.

C++ server

  int main(int argc, char* argv[], char*[]) 
{ 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
run the the world
} catch(corba::exception& ex) { ... } }

slide: server

The C++ class that implements the world interface might appear disappointingly trivial. It is left to the student to think of something less trivial.

C++ world implementation

  void universe_world_srv::hello() {  
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); }

slide: world-srv.c


Writing a CORBA application might indeed be simpler than you thought. Follow the steps taken in this example as a recipe, and you master the art of writing CORBA applications instantly.