ACE+TAO Opensource Programming Notes/Find a service on a naming server

From Wikibooks, open books for an open world
Jump to navigation Jump to search

After your naming service (and other services are running), the presence of your services can be tested for by trying the following:

tao_nslist -ORBInitRef NameService=corbaloc:iiop:localhost:12345/NameService

This should then produce the following output:

Naming Service:
---------

Where your named services appear below the dashed line.

Now to actually connect the two applications. To make our previous example Naming Service aware, we need to add in the code to attach to the Naming Service, then, resolve our service from within that context. The following code, put in place of the code to bring in the IOR from the command line resolves a reference to the factory object using a human readable URL from the command line which is persistent from reboot to reboot. Here's the code snippet we'll be applying:

#include "corbalocC.h"
#include "orbsvcs/CosNamingC.h"

#include "ace/SString.h"

ACE_TString corbaloc_url_;
CosNaming::NamingContextExt_var naming_context_;
corbaloc_url_ = argv[1];

// Get a reference to the Naming Service
CORBA::Object_var naming_context_object =
orb->string_to_object (corbaloc_url_.c_str());

// Narrow to get the correct reference
naming_context_ =
CosNaming::NamingContextExt::_narrow (naming_context_object.in ());

//Setup to get service from within the naming context
CosNaming::Name name (1);

name.length (1);
name[0].id = CORBA::string_dup ("Widgits");

// Resolve the name
CORBA::Object_var factory_object =
naming_context_->resolve (name);

// Narrow
corbaloc::Status_var factory =
corbaloc::Status::_narrow (factory_object.in ());

//And use as desired
factory-> ... do something

Note that the code is potentially all boiler plate which can be placed in a reusable function. The only two original parts of the code are the URL pulled in from the command line, and the string representing the name of or remote class, "Widgets".

./client corbaloc::localhost:12345/NameService

In this example, everything is on one box, but in reality, none of these services need be running on the same hardware, they only need to be reachable by the indicated ports. Now, lets take a look at the client code.