![]() |
|
Use this task to get a pointer to the root naming context. This task adds a "get name context" method (for example, called 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 server environment has been initialized:
This task is one step of the parent task to create the CORBA client main code, as described in Creating a CORBA client main code (client.cpp).
To add a get_name_context() method to the source file for a CORBA server main code, edit the server source file, servantServer.cpp, and add the following code:
// 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 gets a pointer to the naming service then narrows the pointer object to the appropriate object type and assigns it to the new pointer object called rootNameContext, performs some checks, then 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 );
This step returns a pointer object, rootNameContext, to the root naming context.
You need to add code to the client source file to access the servant object created by the server, as described in Creating CORBA client main code (client.cpp), adding code to access the servant object.
![]() |