[Enterprise Extensions only]
  Previous topic

Creating CORBA server main code (server.cpp), adding code to initialize the server environment

Use this task to add a server initialization method to the source file for a CORBA server. This code is used to perform the initialization tasks needed when the server is started.

The aim of the server initialization method is to complete the following tasks to initialize the server environment:

  1. Getting a pointer to the Implementation Repository
  2. Getting a pointer to the ImplementationDef associated with the server alias.
  3. Initializing the communications protocol.
  4. Initializing the ORB and object adapter.
  5. Registering the server application as a CORBA server.

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 a server initialization method to the source file for a CORBA server main code, edit the server source file, servantServer.cpp, and add the following code:

  1. Add an initialization method, and add a statement to the main method to call the new method, as shown in the following code extract:
    // This function performs general initializtion, including retrieval
    // of the appropriate ImplementationDef, setting the communications
    // protocol, and initalization of the ORB and BOA.
    
    
    int perform_initialization( int argc, char *argv[] )
    {
    
      return( 0 );
    }
    
    
    void main( int argc, char *argv[] )
    {
      ::CORBA::Object_ptr objPtr;
      ::CORBA::Status stat;
      int rc = 0;
    
      // Validate the input parameters.
      if ( argc != 2 )
      {
        cerr << "Usage: WSLoggerServer <server_alias>" << endl;
        exit( -1 );
      }
      
    
      if ( ( rc = perform_initialization( argc, argv ) ) != 0 )
        exit( rc );
    
    
    ...
     
    } 
    

    Where:

    server_alias
    Specifies the server alias predefined in the system Implementation Repository.
    perform_initialization( argc, argv )
    Calls the initialization method of the server main code, to perform tasks to initialize the server environment. The method takes as argument the string (server alias) specified on the command used to start the server.

  2. Edit the initialization method, to add code to initialize the server's implementation repository and retrieve the appropriate implementation definition, as shown in the following code extract:
    int perform_initialization( int argc, char *argv[] )
    {
      // Initialize the server's Implementation Repository.
      ::CORBA::ImplRepository_ptr implrep = new ::CORBA::ImplRepository();
      // Retrieve the appropriate ImplementationDef by using the server alias.
      try
      {
    
        imp = implrep->find_impldef_by_alias( argv[1] );
    
      }
    
      // catch exceptions ...
    
      cout << "Retrieved ImplementationDef" << endl;
    
    ...
    
    }
    
  3. Edit the initialization method, to add code to initialize the server's communications protocol, as shown in the following code extract:
    int perform_initialization( int argc, char *argv[] )
    {
    ...
      cout << "Retrieved ImplementationDef" << endl;
    
      // Set the server's communication protocol.
    
      imp->set_protocols("SOMD_TCPIP");
    
      cout << "Set communication protocol" << endl;
    
    ...
     
    } 
    
  4. Edit the initialization method, to add code to initialize the ORB and object adapter, as shown in the following code extract:
    int perform_initialization( int argc, char *argv[] )
    {
    ...
      cout << "Set communication protocol" << endl;
    
    ...
      // Initialize the ORB.
    
      op = ::CORBA::ORB_init(argc, argv, "DSOM");
    
    
      // Initialize the BOA.
      try
      {
    
        bp = op->BOA_init(argc, argv, "DSOM_BOA");
    
      }
    
      // catch exceptions ...
    
      cout << "Initialized ORB" << endl;
    
    ...
    
    }
    
  5. Edit the initialization method, to add code to register the server, as shown in the following code extract:
    int perform_initialization( int argc, char *argv[] )
    {
    ...
      cout << "Initialized ORB" << endl;
    
      // Initialize this application as a server, allow it to accept
      // incoming request messages, and register it with the somorbd
      // daemon.
      try
      {
    
        bp->impl_is_ready( imp, 0 );
    
      }
    
      // catch exceptions ...
    
      cout << "Finished initialization of implementation" << endl;
    
      return( 0 );
    }

    This enables the server to accept incoming request messages, and registers it with the somorbd daemon.

This task adds code to initialize the server environment for a CORBA server.

You need to add code to the server source file to enable the server to access naming contexts, as described in Adding code to access naming contexts to a CORBA server main code (server.cpp).

  Previous topic