Netscape Internet Service Broker for C++ Programmer's Guide

[Contents] [Previous] [Next]

Chapter 2
Getting Started

This chapter steps you through setting up your development environment and describes how to develop a distributed, object-based application with ISB for C++. A sample application illustrates each step of the process. This chapter includes the following major sections:

Setting Up

To develop applications using ISB for C++, you must have:

Before you start, set up your development environment and the Enterprise Server.

Setting Your PATH Variable

Set your PATH environment variable to include the directory where the ISB for C++ executables reside: <server_root>\wai\bin. For example, if the ISB distribution was installed on drive C:\ using default settings, the directory is:

c:\netscape\suitespot\wai\bin

Setting Up Your Development Environment

Add the following ISB header file and library directories to your compiler search path:

For example, if the ISB distribution was installed in C:\ using default settings, add the following ISB header file and library directories to your compiler search path:

Also, make sure the compiler and linker can find these libraries:

The sample application presented in this chapter was developed for Enterprise Server 3.0 using MSVC++ 4.2 under Windows NT 4.0 using c:\msdev\projects\msg as its root directory. A batch file named VCVARS32.BAT (provided with MSVC++ 4.2) was run to set additional environment variables. Your development environment may provide a similar file, or it may not be necessary.

Configuring Your Enterprise Server

ISB for C++ uses services provided by the Enterprise Server. To develop and run ISB for C++ applications, you must set up the server to use WAI. For complete instructions, see the Using WAI chapter of "Writing Web Applications with WAI." The key steps are:

You may also need to create a directory: <server_root>\wai\NameService.

Developing Applications

This section outlines the development process (illustrated in Figure 2.1). Details are provided later in the chapter. Following are the steps for developing applications using ISB for C++. File names and commands listed here refer to files in the sample application; replace them as appropriate for your application.

Create an IDL file to define interfaces (SendMsg.idl).

  1. Compile the IDL file to generate client and server support files:
    c:\msdev\projects\msg> orbeline -c cpp -h hpp -S tie SendMsg.idl

  2. Implement the service interface (HelloImpl.cpp).

  3. Implement the object server application (MsgService.cpp).

  4. Implement the client application (MsgClient.cpp)

  5. Build everything:
    prompt> cd c:\msdev\projects\msg
    c:\msdev\projects\msg> nmake

  6. Start the object server:
    c:\msdev\projects\msg> start MsgService

  7. Run the client application:
    c:\msdev\projects\msg> MsgClient
Figure 2.1    Developing the sample application.

A Sample Application

The rest of this chapter presents a small sample application. A C++ object server named MsgService exposes one service, accessed via the Hello interface, that prints a message on the server and sends a message to the client.

This sample application was developed under Windows NT 4.0. It assumes that c:\msdev\projects\msg is its root directory. If you recreate this sample application using a different configuration, change code and commands accordingly.

The most important files in this application are listed in the following table. You, the programmer, will write each of these files. The application also uses files generated by the orbeline compiler.

File Name

Description

SendMsg.idl

Defines the interface to the Hello service.

HelloImpl.cpp

Implements the interface to the Hello service.

MsgService.cpp

Activates and registers the service, making it available to clients.

MsgClient.cpp

Accesses the service and calls a method from its interface.

Defining Interfaces in IDL

The standard way to define CORBA interfaces is to use the Interface Definition Language (IDL). An IDL file begins by defining a module. A module contains one or more interfaces that provide one or more methods. The following file (named SendMsg.idl) defines the SendMsg module that defines the Hello interface. The interface provides one method, sayHello, that returns a string.

SendMsg.idl defines the Hello interface.

// SendMsg.idl
module SendMsg {
    interface Hello {
        string sayHello();
    };
};

Running the IDL Compiler

The ISB for C++ IDL compiler is named orbeline. The following command invokes orbeline to compile the IDL file SendMsg.idl for the sample application.

c:\msdev\projects\msg> orbeline -c cpp -h hpp -S tie SendMsg.idl
This example uses several command line arguments. The argument -c cpp tells the compiler to give C++ source files an extension of .CPP, the argument -h hpp tells the compiler to give C++ header files an extension of .HPP, and the argument -S tie tells the compiler not to generate code to support the tie mechanism (the sample application does not use the tie mechanism). By default, orbeline generates header files with an extension of .hh and source files with an extension of .cc. The choice of extensions is provided as a convenience. The default extensions make it easy to distinguish generated files, but they may not be recognized by some C++ development environments. For more information on the command line options for the IDL compiler, see the Netscape Internet Service Broker for C++ Reference Guide.

Generated Files

Compiling SendMsg.idl generates the files listed in the following table.

File Name

Description

SendMsg_c.hpp,
SendMsg_c.cpp

Stub code for the client application. These header and source files contain generated code for the Hello service object on the client side. Do not modify these files.

SendMsg_s.hpp,
SendMsg_s.cpp

Skeleton code for the object server. These header and source files contain generated code for the Hello service object on the server side. Do not modify these files.

Implementing an Interface

In CORBA, interface definitions and implementations are separate and distinct. You define interfaces once, then implement them one or more times in one or more languages, depending on your application requirements. In this sample application, the Hello service interface is implemented in C++ using a file generated by orbeline. Following is the implementation file HelloImpl.cpp. Normally, you, the programmer, would create this file.

// HelloImpl.cpp
#include "SendMsg_s.hpp"

class HelloImpl : public _sk_SendMsg::_sk_Hello {
public:
HelloImpl(const char* object_name = NULL) : _sk_SendMsg::_sk_Hello(object_name) {
};

char* sayHello() {
cerr << "Client called sayHello." << endl;
return "Hello!";
};
};

Writing a Server Application

The server application (also called an object server) creates, activates, and registers services, making them available to clients. In this sample application, the object server is named MsgService. Many of the files used to implement MsgService are generated by the IDL compiler, orbeline. The MsgService.cpp file presented here is not generated. Normally you, the programmer, would create this file. This file provides a main method that does the following:

Figure 2.2    The object server for the sample application. 

// MsgService.cpp
#include "HelloImpl.cpp"
#include <iostream.h>
#include <NameUtil.hpp>

int main(int argc, char* const* argv) {
try {
// Initialize the ORB and the BOA.
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
CORBA::BOA_var boa = orb->BOA_init(argc, argv);
HelloImpl* hi = new HelloImpl();
WAIReturnType_t rc;
char *objectName = "Howdy";
char *hostAndPort = "mikelee:80";
rc = registerObject(hostAndPort, objectName, hi);
boa->obj_is_ready(hi);
cerr << "Ready for client requests." << endl;
boa->impl_is_ready();
} catch (CORBA::SystemException& e) {
cerr << e << endl;
return(1);
}
return(0);
}

Writing a Client Application

The client application locates a service, then invokes methods that service provides. In this sample application, the client application is named MsgClient. Many of the files used to implement MsgClient are generated by orbeline. The MsgClient.cpp file is presented here is not generated. Normally you, the programmer, would create this file. It performs the following functions:

Initializes the Object Request Broker (ORB).

  1. Locates the Hello service. Client-side code must prepend the object name with the string "/NameService/." In this example, the desired object name is "Howdy,", so the complete string is "/NameService/Howdy."

  2. Calls the sayHello method provided by the Hello service.

  3. Displays the resulting message.
    //MsgClient.cpp
    #include "SendMsg_c.hpp"
    #include <iostream.h>
    #include <NameUtil.hpp>

    int main(int argc, char* const* argv) {
    try {
    // Initialize the ORB.
    CORBA::ORB_ptr orb = CORBA::ORB_init(argc, argv);
    SendMsg::Hello_var hi;
    // Find a reference to the Hello service.
    CORBA::Object_ptr p;
    WAIReturnType_t rc;
    char* host = "mikelee";
    int port = 80;
    char* uri = "/NameService/Howdy";
    rc = resolveURI(host, port, uri, p);
    // Narrow the reference.
    hi = SendMsg::Hello::_narrow(p);
    // Call a method provided by the service.
    cout << hi->sayHello() << endl;
    } catch (CORBA::SystemException& e) {
    cerr << e << endl;
    return(1);
    }
    return(0);
    }

Building the Client and Server

The sample application presented in this chapter was developed for Enterprise Server 3.0 using MSVC++ 4.2 under Windows NT 4.0 using c:\msdev\projects\msg as its root directory. A batch file named VCVARS32.BAT (provided with MSVC++ 4.2) was run to set additional environment variables. Your development environment may provide a similar file, or it may not be necessary.

Below is a VC++ 4.2 makefile for building the sample application. To invoke it, issue commands like the following (actual file contents and commands may vary depending on your development platform):

prompt> cd c:\msdev\projects\msg
c:\msdev\projects\msg> nmake
Makefile for building the sample application.

CC = CL -DWIN32 -DXP_WIN32 /GX  /MT /O2
DEBUG =
ISBDIR = \netscape\suitespot\wai
ESROOT = \netscape\suitespot
ORBCC = orbeline -v _c -m _s -c cpp -h hpp
CCINCLUDES = -I. -I$(ISBDIR)\include -I$(ESROOT)\include
CCFLAGS = $(CCINCLUDES) $(DEBUG)
LIBDIR = $(ISBDIR)\lib
STDCC_LIBS = wsock32.lib kernel32.lib
NSCP_LIBS = $(LIBDIR)\ONEiiop10.lib
LIBORB = $(LIBDIR)\orb_r.lib
.SUFFIXES: .CPP .obj .h .hh .hpp

.CPP.obj:
$(CC) $(CCFLAGS) -c $<
EXE = MsgClient.exe MsgService.exe
#EXE = MsgService.exe
all: $(EXE)
clean:
del *.obj *.hh *_c.cpp *_s.cpp $(EXE) *.log
SendMsg_c.cpp: SendMsg.idl
$(ORBCC) SendMsg.idl
SendMsg_s.cpp: SendMsg.idl
$(ORBCC) SendMsg.idl
MsgClient.exe: SendMsg_c.obj MsgClient.obj
$(CC) -o MsgClient.exe MsgClient.obj SendMsg_c.obj \
$(LIBORB) $(STDCC_LIBS) $(NSCP_LIBS)
MsgService.exe: SendMsg_s.obj SendMsg_c.obj HelloImpl.obj MsgService.obj
$(CC) -o MsgService.exe MsgService.obj SendMsg_s.obj \
HelloImpl.obj SendMsg_c.obj $(LIBORB) \
$(STDCC_LIBS) $(NSCP_LIBS)

Starting the Object Server

If you are running Enterprise Server 3.0, start osagent before starting your object server. If you are running version 3.01, you do not need osagent.

c:\msdev\projects\msg> osagent
To start a server application (under Windows NT), open a console window and use the start command. For example, the following command starts the MsgService application.

c:\msdev\projects\msg> start MsgService
If the application starts successfully, it will display the following message in the console window.

Ready for client requests.

Running the Client Application

After starting the object server, you can start the MsgClient application.

c:\msdev\projects\msg> MsgClient
If the application starts successfully, it will display the following message in the client's console window.

Hello!
Also, the following message will appear in the object server's console window:

Client called sayHello.
If your development environment is not connected to a network (that is, if you are developing the application on a stand-alone system), it may take several seconds for the message to appear. Response times are generally faster on networked systems.


[Contents] [Previous] [Next]

Last Updated: 02/03/98 15:28:52


Copyright © 1997 Netscape Communications Corporation

Any sample code included above is provided for your use on an "AS IS" basis, under the Netscape License Agreement - Terms of Use