Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Developing Web Services in C++ : Page 2

A lot of press surrounds the Web services market today. But almost all of the information available for programmers is concentrated around Java and C#. This article intends to address this deficit by showing how to create a C++ Web Service (aka SOAP) client for Google's Web Service-enabled search API.


advertisement
Creating a SOAP Client
With WSDL in hand, developing a SOAP client for a service is simplicity itself. Via a process that should look familiar to CORBA developers, use Google's WSDL document to create a SOAP Remote Procedure Call client that calls the doGoogleSearch service. This service, among others, is denoted in the <portType> section of the WSDL.

<portType name="GoogleSearchPort"> ... <operation name="doGoogleSearch"> <input message="typens:doGoogleSearch"/> <output message="typens:doGoogleSearchResponse"/> </operation> </portType>

As a programmer, however, it is not strictly necessary to understand the entirety of the WSDL document. It is only necessary to convert it into C++ code. Assuming you have installed WASP according to the documentation, exported WASPC_HOME to your environment, and added the waspc/bin directory to your PATH, you can simply run the wsdlc utility which converts a WSDL document to C++ client stubs and server skeletons. I recommend that you create a new directory for the development of your client, and copy the GoogleSearch.wsdl document into it.

$ mkdir google_client $ cp [googleapi]/GoogleSearch.wsdl google_client $ cd google_client $ wsldc GoogleSearch.wsdl GoogleSearch

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 is a series of C++ source files that support you in developing both a client and a service. Since the service already exists, you won't need the server oriented files, and can safely delete them (you'll encounter them again in the second installment of this series).


$ rm GoogleSearchImpl.cpp GoogleSearchImpl.h

Of the remaining files, you are chiefly interested only in GoogleSearch.h and GoogleSearchStructs.h. The others become important later, as they implement the base classes you'll extend and the various helper classes and methods that WASP will use.

Now that WASP has generated all the difficult code, simply write some code to exercise it. The complete source code listing can be found here. In the following sections, I'll describe it.

Initializing the Environment
The listing begins with a number of boilerplate #includes, the last of which is one of the header files output by the wsdlc utility. A quick examination of the GoogleSearch.h file shows that WASP has converted the <input> message (i.e., the parameters) of the doGoogleSearch service to a series of native C++ types and WASP-provided wrapper types. The first thing you want to do in this program is create and initialize these arguments:

WASP_VString key = ; WASP_VString query = ; int start = 0; int maxResults = 10; bool filter = false;

The WASP_VString class is a wrapper around an ordinary C/C++ char *, primarily offered to allow transcoding back and forth from ASCII to the UTF-8 and UTF-16 character encoding used in SOAP. It should be used for all string data types passed back and forth between SOAP clients and servers.

The call to WASP_Runtime::clientInitialize() is a mandatory call that, obviously, initializes and configures the client environment, and should be called before any SOAP-oriented code is executed. It differs from the WASP_Runtime::clientStart() function a few lines down in that clientInitialize() performs core functions like initializing threads and factories, while clientStart() initializes the transport layer, the serialization mechanism, transcoders, and such, all of which can be manipulated. Note that clientStart() requires a configuration filename; the default of conf/client.xml, found in the WASP_HOME/share/waspc directory, is sufficient for our needs.

The first interesting thing here is the TRY macro. The TRY macro and its related CATCH, AND_CATCH_ALL, and STOP_CATCH_ALL macros are provided by WASP so that exception handling may be supported on all platforms, including Windows CE and those platforms unable to use exceptions in shared libraries.

Next, initialize the two most important parameters to the doGoogleSearch service; the activation key and the query itself. For the code listing, I have starred out my key, and you will need to supply the one you received from Google.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap