Why and when to perform this task
Before a CORBA server can create and make available a servant object, it must have a logical name space for the servant object to exist in. This logical name space is a naming context for servant objects. The server can create a new naming context within any location within the root naming context. For example, a server called servantServer might create a new naming context called servantContext into which the server binds the servant object. Optionally, this context might be located within a domain context, which in turn is located within the root naming context. (You can create a servant context with only the root naming context as its parent or with one or more intermediary parent contexts.)
Use this task to get a pointer to the root naming context. This task adds a "get name context" method (for example, get_name_context) to the source file for a CORBA client. This method is used to access the naming service and return a pointer to the root naming context. The method performs the following actions after the client environment has been initialized:
Steps for this task
// This method accesses the Name Service and then gets // the root naming context, which it returns; // the WSLogger context. ::CosNaming::NamingContext_ptr get_naming_context() { ::CosNaming::NamingContext_ptr rootNameContext = NULL; ::CORBA::Object_ptr objPtr; // Get access to the Naming Service. try { objPtr = op->resolve_initial_references( "NameService" ); } // catch exceptions ... if ( objPtr == NULL ) { cerr << "ERROR: resolve_initial_references returned NULL" << endl; release_resources( op ); return( NULL ); } else cout << "resolve_initial_references returned = " << objPtr << endl; // Get the root naming context. rootNameContext = ::CosNaming::NamingContext::_narrow(objPtr); if ( ::CORBA::is_nil( rootNameContext ) ) { cerr << "ERROR: rootNameContext narrowed to nil" << endl; release_resources( op ); return( NULL ); } else cout << "rootNameContext = " << rootNameContext << endl; // Release the temporary pointer. ::CORBA::release(objPtr); return( rootNameContext ); }
where
This code receives a pointer to the naming service, narrows the pointer object to the appropriate object type, assigns it to the new pointer object called rootNameContext, performs some checks, and releases the original pointer object, objPtr.
... if ( ( rc = perform_initialization( argc, argv ) ) != 0 ) exit( rc ); // Get the root naming context. rootNameContext = get_naming_context(); if ( ::CORBA::is_nil( rootNameContext ) ) exit( -1 );
Results
This step returns a pointer object, rootNameContext, to the root naming context.What to do next
Add code to the client source file to access the servant object created by the server as described in Adding code to access the servant object.