[Enterprise Extensions only]
  Previous topic

Creating CORBA server main code (server.cpp), adding code to name, create, and bind servant objects

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:

  1. Add code to the main method to get a new ::CosNaming::Name for servant objects and to call a method to create and bind servant objects, as shown in the following code extract:
    
    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:

    servantNameContext
    is the pointer to the naming context for servant objects.
    servantObject1
    is the name under which we choose to bind this servant object in the servantNameContext This name uniquely defines the servant_Impl object in the servantNameContext.
    create_and_bind method
    is the name of the method that the server calls to create servant objects and bind them into the naming context.

    The ::CosNaming::Name is obtained outside the create_and_bind() method so that the name can be reused later, when terminating the server.

  2. Add a create_and_bind method, and add a statement to the main method to call the new method, as shown in the following code extract:
    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.

    Note: A string is passed to the servant object's initializer, to specify a default name for the log file. This name is not used by client programs, because clients change the default name to a user-specified value by invoking the method setFileName().

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.

  Previous topic