<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
:\
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.
<server_root>\wai\NameService
.
Create an IDL file to define interfaces (SendMsg.idl
).
c:\msdev\projects\msg> orbeline -c cpp -h hpp -S tie SendMsg.idl
HelloImpl.cpp
).
MsgService.cpp
).
MsgClient.cpp
)
prompt> cd c:\msdev\projects\msg
c:\msdev\projects\msg> nmake
c:\msdev\projects\msg> start MsgService
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.
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.
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:
_var
classes are described in The IDL Compiler.
boa->impl_is_ready
and waits for client requests.
// 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).
sayHello
method provided by the Hello service.
//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);
}
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> nmakeMakefile 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)
osagent
before starting your object server. If you are running version 3.01, you do not need osagent
.
c:\msdev\projects\msg> osagentTo 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 MsgServiceIf the application starts successfully, it will display the following message in the console window.
Ready for client requests.
c:\msdev\projects\msg> MsgClientIf 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.
Last Updated: 02/03/98 15:28:52
Any sample code included above is provided for your use on an "AS IS" basis, under the Netscape License Agreement - Terms of Use