Polymorphic Object Exchange Example

This example shows you how to create a combined client and server that exchange polymorphic objects. This example demonstrates a ``black box'' server that accepts derived object instances and sends them back to the client. The objects keep their identity. This is a unique feature of the SOAP C/C++ stub and skeleton compiler.

Step 1: Write the Main Program

We wrote an example client program for you: click here and select "File - Save As" from your browser's menu.

Step 2: Generate the SOAP Stub Routines

Before you can compile and run your  program, you must generate C/C++ sources using our stub compiler. The generated sources take care of the SOAP serialization and deserialization of the data required to access the SOAP service.

The input to our stub compiler is a specification of the names of the SOAP methods (with optional namespaces) and the data structures given as C/C++ declarations. You can get this header file specification here. The remote method takes an instance of class ns__Object and returns the instance. When the instance is a derived class instance, the identity of the instance is automatically preserved by the (de)marshalling routines. The remote method function prototype is:

ns__polytest(ns__Object *in, struct ns__polytestResponse { ns__Object *out; } &result);

Use the SOAP C/C++ stub and skeleton compiler to process the header file:

soapcpp polymorph.h

Step 3: Compiling Your Client and Service Apps

Change the endpoint URL's in the polymorph.cpp source to your endpoint URL for this service.
Compile the sources. For example, in Linux the command to compile would be:
g++ -o polytest.cgi polymorph.cpp soapC.cpp soapClient.cpp soapServer.cpp stdsoap2.cpp
Deploy the polytest.cgi executable as a CGI application in your Web server such that its endpoint URL refers to this app. The executable polytest.cgi is both a client and a server. When called with an argument, it is a client.

Step 4: Try it Out

Execute the commands:
"polytest.cgi o" will send an object with a name to the server who bounces it back to the client.
"polytest.cgi s" will send a shape (triangle) to the server. A shape is derived from object.
"polytest.cgi q" will send a square to the server. A square is derived from shape.
"polytest.cgi l" will send a list of object, shape, square, and another list. The service returns the list to demonstrate that the identity was preserved.