![]() |
|
Use this task to add code to the source file for a CORBA server, to get a new ::CosNaming::Name for servant objects, create servant objects, and bind them into the appropriate naming context. This makes it possible for clients to find and use servant objects.
This task is one step of the parent task to create the CORBA server main code, as described in Creating a CORBA server main code (server.cpp).
To add code to name, create, and bind servant objects, edit the server source file, servantServer.cpp, and add the following code:
int create_and_bind( ::CosNaming::Name *nc, ::CosNaming::NamingContext_var servantNameContext ) { return( 0 ); } void main( int argc, char *argv[] ) { ... // Get the various naming contexts. ::CosNaming::NamingContext_var servantNameContext = NULL; servantNameContext = get_naming_context(); if ( ::CORBA::is_nil( servantNameContext ) ) exit( -1 ); // Get a new ::CosNaming::Name for our servant_Impl object. // This is done here rather than in create_and_bind() so that the // name can be reused later, when terminating the server. ::CosNaming::Name *nc = new ::CosNaming::Name; nc->length( 1 ); (*nc)[0].id = ::CORBA::string_dup( "servantObject1" ); (*nc)[0].kind = ::CORBA::string_dup( "" ); // Create a new servant_Impl object and bind it to the servantNameContext. if ( ( rc = create_and_bind( nc, servantNameContext ) ) != 0 ) exit( -1 ); ... }
Where:
The ::CosNaming::Name is obtained outside the create_and_bind() method so that the name can be reused later, when terminating the server.
int create_and_bind( ::CosNaming::Name *nc, ::CosNaming::NamingContext_var servantNameContext ) { // Create a servant object. servantImpl = new servantImpl( "defaultlog" ); // Bind the object to this name in the servant naming context. try { servantNameContext->bind( *nc, objectPtr ); cout << "bind of servantNameContext succeeded" << endl; } // catch exceptions ... return( 0 ); }
This method takes as input the ::CosNaming::Name obtained before the method is called and the pointer to the naming context for servant objects. The code creates a servant object then binds the object to the ::CosNaming::Name in the servant naming context and performs a variety of binding checks.
This task adds code that enables a CORBA server to name, create, and bind servant objects.
You need to add code to the server source file to enable the server to create a server shutdown object that can be used to help shutdown the server, as described in Creating CORBA server main code (server.cpp), adding code to create a server shutdown object.
![]() |