Browse DevX
Sign up for e-mail newsletters from DevX


Developing Web Services in C++, Part II : Page 4

Part I of this series set out to address the shortage of information on C++ and Web services, showing readers how to create a C++ Web service client. Part II continues the mission by describing how to author an actual Web service, using SOAP, C++, and a little Java.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Configuring the Server
You may be tempted to start your new server, but don't—it won't work. If you think about things for a second, you'll realize there's a couple of loose threads. For instance, the server does not know that the Tempconverter.wsdl document is related to the TempconverterService service. Nor does it know what URL should cause the TempconverterService service to be instantiated. Fortunately, the answers to these questions do not require any further coding. This information (and more, if necessary) can be setup in a simple XML configuration file.

<?xml version="1.0"?> <waspc-config xmlns:wasp="urn:WaspServer" xmlns:cppa="urn:CppAdaptor" xmlns:sep="urn:ServiceEndpoint" xmlns:svci="urn:ServiceInstanceRepository"> <wasp:import ref="conf/server.xml"/> <!-- Service binding --> <sep:serviceEndpoint sep:dispatcherRef="DefaultDispatcher" sep:adaptorRef="DefaultCppAdaptor" sep:wsdl="Tempconverter.wsdl" sep:url="/tempconverterservice/"> <cppa:instance cppa:ref="tempconverter"/> </sep:serviceEndpoint> <!-- Service instances - implementation classes --> <svci:serviceInstance svci:class="TempconverterService" svci:name="tempconverter"/> </waspc-config>

After some initial (boilerplate) namespace declarations and server configuration, you arrive at the elements that control the service endpoint. Of the components in the <serviceEndpoint> attribute list, the ones you're concerned with are the wsdl and url attributes. The wsdl attribute describes the path to the service's WSDL document relative to the start-up directory of the server. The url attribute, obviously, specifies the URL of the service relative to the root of the HTTP server.

The use of the <cppa:instance> tag simply names this endpoint so that it can later be associated with a service instance as known to your server, i.e. your object code. This is done with the <serviceInstance> element. Here, you can see that name refers back to the serviceEndpoint/instance ref attribute, and class refers to the name of the class as specified in the WASP_FACTORY_DEFINE macro of your server. Save this file as config.xml (as dictated by the WASP_Runtime::serverStart() function).

With all the components in place, you can start your server.

$ ./tcserver&

Creating the Client
Servers aren't too interesting without their clients, so you'll want to develop a SOAP client to exercise your new service. I recommend creating the client code in the separate client directory you made earlier. If you do this, you'll want to copy over a few of the files that wsdlc generated.

$ cp Tempconverter.h Tempconverter.cpp TempconverterStructs.h \ TempconverterStucts.cpp ../client

Listing 2 shows a sample client that is even simpler than the Google client presented in Part I. Save this client code as tcclient.cpp in the client directory, then compile and link it.

$ g++ -o tcclient *.cpp I/usr/local/waspc/include \ /usr/local/waspc/lib/libwasp.so

When you run this client, you should see the following:

$ ./tcclient 70.00 degrees Fahrenheit = 21.11 degrees Celsius 70.00 degrees Celsius = 158.00 degrees Celsius

That's how easy it is to develop Web services in C++ or to expose existing C/C++ code as Web services!

Peter Lacey has over 15 years of industry experience. He is currently an engineer for Systinet, Corp. Prior to his current position, he held engineering and product management positions at Cisco Systems and Netscape Communications. You can reach him at lacey@systinet.com.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date