spacer
spacer

gSOAP for C++

Create the Stubs

Using wsdl2h to generate the header file used as an intermediate to generate the stubs. The -o option to give the generated files a more meaningful name:

wsdl2h -oWSDbfetchService "http://www.ebi.ac.uk/ws/services/WSDbfetch?wsdl"

Process the generated header file (WSDbfetchService.h) with soapccp2 to generate the actual stubs:

soapcpp2 -i -I/usr/include/gsoap -pWSDbfetch -C WSDbfetchService.h

Using -i to generate wrapping service proxy objects, -I to specify the path to the required STL files, -p so specify more meaningful file names (e.g. WSDbfetch* rather than soap*), and -C to generate only the client stubs, by default both client stubs and server skeletons are generated.

Client Code

Now we have stubs, we need to write some code that uses them, for example (wsdbfetch_gsoap_cpp.cpp):

// Include generated stubs
#include "WSDbfetchWSDbfetchSoapBindingProxy.h"
#include "WSDbfetchSoapBinding.nsmap"
 
int main(int argc, char* argv[])
{
  // Exit status to return
  int exitStatus = 0;
  // Arguments for query
  std::string query = "UNIPROT:WAP_RAT";
  std::string format = "fasta";
  std::string style = "raw";
  // For the result
  std::string result;
 
  // Service proxy
  WSDbfetchSoapBindingProxy srvProxy;
 
  // Make the request, checking for failure
  if (srvProxy.fetchData(query, format, style, result) == SOAP_OK)
    {
      // Print the result
      std::cout << result << std::endl;
    }
  else
    {
      // An error occured, so print the message.
      std::cerr << srvProxy.soap_fault_string() << std::endl;
      exitStatus = 1;
    }
  return exitStatus;
}

Compile the Client

Compiling the C++ source and linking to create an executable:

cxx -o wsdbfetch_gsoap_cpp WSDbfetchC.cpp WSDbfetchWSDbfetchSoapBindingProxy.cpp wsdbfetch_gsoap_cpp.cpp -lgsoap++

Complex Types

Complex types, such as the data structures used to pass the parameters and data to the runWUBlast operation of the WSWUBlast service, are mapped into classes:

  // Populate parameters
  ns1__inputParams params;
  params.email = "your@email";
  bool async = true;
  params.async = &async;
  params.program = "blastp";
  params.database = "swissprot";
  // Populate data
  ns1__data content;
  content.type = "sequence";
  content.content = "uniprot:slpi_human";
  ns1__data *contentArray = new ns1__data[1];
  contentArray[0] = content;
  WSArrayofData data;
  data.__ptr = &contentArray;
  data.__size = 1;
 
  // Service proxy
  WSWUBlastSoapBindingProxy srvProxy;
 
  // Submit job
  std::string jobId;
  if (srvProxy.runWUBlast(&params, &data, jobId) != SOAP_OK)
    {
      // An error occured, so print the message.
      std::cerr << srvProxy.soap_fault_string() << std::endl;
      exitStatus = 1;
      return exitStatus;
      }
  // Print the job identifier
  std::cout << jobId << std::endl;

Examples

Complete source code for the examples are available:

 
tutorials/06_programming/c_cpp/soap/gsoap_cpp.txt · Last modified: 2011/03/29 15:32 by hpm
spacer
spacer