#include "CipresCommlib/CipresNameService.h" #include "CipresCommlib/CipresFacilitator.h" #include #include using namespace std; CipresNameService::CipresNameService() : m_namingContext(CosNaming::NamingContext::_nil()) { } CipresNameService::~CipresNameService() { } /// sets the m_namingContext member by obtaining a reference from the orb (via the CipresFacilitator). void CipresNameService::initialize() { if (!CORBA::is_nil(m_namingContext.in())) { // already initialized. return; } CORBA::ORB_ptr orb = CipresFacilitator::getSingletonPtr()->getORB(); cout << "CipresNameService:resolve ref for NameService" << endl; CORBA::Object_var naming_context_object = orb->resolve_initial_references ("NameService"); if (CORBA::is_nil(naming_context_object.in())) { cerr << "Unable to resolve initial ref: NameService" << endl; throw 1; } // Keep a ref to the naming service for future use. m_namingContext = CosNaming::NamingContext::_narrow( naming_context_object.in ()); if (CORBA::is_nil(m_namingContext.in())) { cerr << "Unable to narrow ref to NamingContext" << endl; throw 1; } } /// registers objectRef with the name service under the name objectName. /// \returns bool context (0 for failure) int CipresNameService::nsRegister(const char *objectName, CORBA::Object *objectRef) { try { if (CORBA::is_nil(m_namingContext.in())) { throw 1; } // Create and initialize the name. CosNaming::Name name (1); name.length (1); name[0].id = CORBA::string_dup (objectName); // Bind the object m_namingContext->rebind (name, objectRef); } catch (int) { cout << "Reference to naming service has not been initialized" << endl; return 0; } catch (CORBA::Exception &) { cout << "Caught exception trying to use the naming service:" << endl; return 0; } return 1; } /// searches the name service for an object reference with name == "objectName" /// returns nil on failure. CORBA::Object *CipresNameService::nsFind(const char *objectName) { try { if (CORBA::is_nil(m_namingContext.in())) { throw 1; } CORBA::Object_var obj; CosNaming::Name name(1); name.length(1); name[0].id = CORBA::string_dup(objectName); obj = m_namingContext->resolve(name); return obj._retn(); } catch (int) { cout << "Reference to naming service has not been initialized" << endl; return CORBA::Object::_nil(); } catch (CORBA::Exception &) { cout << "Unable to get " << objectName << " from NameService" << endl; return CORBA::Object::_nil(); } }