Browse DevX
Sign up for e-mail newsletters from DevX


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

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

Creating the Service
Now that you have the WSDL document, you can use it to generate the bulk of your client and service source code. As seen in the previous article, wsdlc is the utility used to compile a WSDL document into C++ source.

wsdlc Tempconverter.wsdl Tempconverter

The second argument to wsldc is a project name. That is, a string that wsdlc uses when naming the various files it creates. The output of this command will be a series of C++ source files that supports the development of both the client and the service. These files represent client stubs and server skeletons. As a developer, you are chiefly interested in the header files. You can remain, thankfully, ignorant of the others.

Once WASP has generated all the difficult code, you only need to do two things: implement the service and stitch everything together. First, declare the service in a distinct header file. This class declaration should be derived from the TempconverterImpl skeleton class that was created by wsdlc.

#include "tempconvImpl.h" class TempconverterService : public TempconvertrImpl { public: double ftoc (double); double ctof (double); };

That's it. Save this file as TempconverterService.h. Next, implement the TempconverterService class.

#include "TempconverterService.h" double TempconverterService::ftoc (double fahr) { return ((fahr - 32) * 5) / 9; } double TempconverterService::ctof (double cel) { return ((cel * 9) / 5) + 32; }

That's pretty simple too. Save this file as TempconverterService.cpp.

Implementing the Server
It only remains to take this service and compile it with some boilerplate infrastructure code. Keep in mind that, unlike SOAP servers available for more dynamic languages like Java and C#, C++ does not allow you to deploy object files to a distinct server process. It is therefore necessary to link your service with the WASP server code in order to create a SOAP-enabled executable. It stands to reason that the server code will then need to be made aware of the service. Thus, your last bit of server-side source code (shown in Listing 1) registers your service with WASP, starts up the WASP environment, and waits forever.

After including the requisite headers, the first thing this code does is register your service with WASP. When expanded, the WASP_FACTORY_DEFINE macro declares a factory function that returns a new instance of your class. In the main() body, create an array of these function pointers with the help of a few more macros. Under the covers, each array element holds a struct containing the name of your service and the pointer to the factory function that instantiates it.

In this case, you have just one service, but as you create more services in the future, you can simply add them to this list. No other code needs changing. So, if you were to later create a service, say, a currency converter, yentodollar, and that service was defined in its own class called CurrconverterService, you would change the above code as follows, and recompile.

WASP_FACTORY_DEFINE (TempconverterService); WASP_FACTORY_DEFINE (CurrconverterService); int main (int, char **) { WASP_FactoryDefinition serviceFactory[]={ WASP_FACTORY_ENTRY (TempconverterService), WASP_FACTORY_ENTRY (CurrconverterService), WASP_FACTORY_END () }; }

With this infrastructure in place, all you need to do now is initialize WASP, register your factories with the WASP super factory, and start her up. Remember to wrap the SOAP engine up with some exception handling. Also remember to terminate the WASP server politely. Save the server code as tcserver.cpp. Then, compile and link everything together.

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

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