Homepage of Ingo Klöckl
 Home
 Programmieren
 - Toolbox
 - Java
 - PostScript
 - Perl
 - Assembler
 - Links
 - Download
Ingo Klöckl
i.kloeckl@2k-software.de

Programming CORBA with Orbacus on Linux

Publishing references of server objects

Converting a reference into a string

The method

CORBA::String_ptr CORBA::ORB::object_to_string(CORBA::ORB::Object_ptr)
converts an object reference into a string. The string is dynamically allocated, so you have to release the string's memory after usage or use a String_var variable. The method can throw several exceptions.

A typical usage is the following:

try {
  X_impl* impl = new X_impl();
  X_var servant = impl->_this();

  CORBA::String_var strRef = orb->object_to_string(servant);

  <do something with the reference, e.g. store in a file>
  const char* refFile = "X.ref";
  ofstream out(refFile);
  out << strRef << endl;
  out.close();
} catch (const CORBA::SystemException& e){
  // something was wrong with CORBA
} catch (...){
  // something else was wrong
}
After creating the server implementation and activating it (which returns the object reference), the reference is stringified and stored in a file.

The stringified references can be transferred from server to the client by storing them in ordinary files, sending via email or using other methods. The simplest method is to store them in files. An example for publishing a server reference via a file-based IOR is given here.

Publishing references with CORBALOC

Instead of writing the reference to a server object into a file or publish the stringified reference in another way, you can use the boot manager to publish the reference under a freely choosen name via CORBALOC. You add a binding from this object ID to the reference to the boot manager:

  OB::BootManager_var bootManager = <get boot manager from somewhere>
  X_var xv = <get server reference from somewhere>
  PortableServer::ObjectId_var oid = PortableServer::string_to_ObjectId("/Serverobjectname");
  bootManager->add_binding(oid, xv);
After doing so, you can resolve this binding via string_to_object.

In a real-world example, you use the CORBALOC binding as follows:

try {
  // create implementation object
  X_impl* xImpl = new X_impl();
  PortableServer::ObjectId_var oid = PortableServer::string_to_ObjectId("/Serverobjectname");
  PortableServer::ServantBase_var servant = xImpl;
  poa->activate_object_with_id(oid, servant);

  // resolve boot manager and register server
  CORBA::Object_var bootObj = orb->resolve_initial_references("BootManager");
  if (CORBA::is_nil(bootObj)){
    // handle error
  }
  OB::BootManager_var bootManager = OB::BootManager::_narrow(bootObj);
  if (CORBA::is_nil(bootManager)){
    // handle error
  }

  // publish reference via corbaloc naming service
  X_var xv = xImpl->_this();
  bootManager->add_binding(oid, xv);
} catch (const CORBA::SystemException& e){
  // handle error
}
An example using CORBALOC can be found here.

back to CORBA page