Web Services & C++
by Peter Lacey


Listing One
interface Planet {
  String getPlanet(int pos);
}

Listing Two
#include "PlanetImpl.h"
class PlanetService : public PlanetImpl {
  public:
    virtual WASP_VString getPlanet(int pos);
};

Listing Three
#include "PlanetService.h"
WASP_VString PlanetService::getPlanet(int pos) {
  if ( (pos < 1) || (pos > 9) ) {
    throw new WASP_Exception("Index out of range. Must be 1 through 9.");
  }
  char * planets[] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter",
                      "Saturn", "Uranus", "Neptune", "Pluto"};
  WASP_VString planet = planets[pos-1];
  return planet;
}

Listing Four
#include <waspc/common.h>
#include <waspc/runtime/Runtime.h>
#include <waspc/runtime/SuperFactory.h>
#include <stdio.h>
#include "PlanetService.h"
WASP_FACTORY_DEFINE (PlanetService);
int main (int, char **) {
    WASP_FactoryDefinition serviceFactory[]={
        WASP_FACTORY_ENTRY (PlanetService),
        WASP_FACTORY_END ()
    };
    WASP_Runtime::serverInitialize ();
    WASP_SuperFactory::registerFactory (serviceFactory);
    try {
        WASP_Runtime::serverStart ("config.xml",NULL);
    } catch (WASP_Exception *exc) {
        char *trace=GET_TRACE (exc);
        cout << "Exception during startup: " << exc->getCharMessage() << endl;
        cout << "Stack trace follows: " << endl << trace << endl;
        delete trace;
        delete exc;
    };
    WASP_Runtime::serverTerminate ();
    return 0;
}

Listing Five
<?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="Planet.wsdl"
        sep:url="/PlanetService/">
        <cppa:instance cppa:ref="planetsref"/>
    </sep:serviceEndpoint>
    <!-- Service instances - implementation classes -->
    <svci:serviceInstance
         svci:class="PlanetService"
         svci:name="planetsref"/>
</waspc-config>

Listing Six
#include <iostream.h>
#include <waspc/common.h>
#include <waspc/runtime/Runtime.h>
#include <waspc/client/StubFaultException.h>
#include "Planet.h"

int main (int argc,char *argv[]) {
  WASP_Runtime::clientInitialize ();
  try {
    WASP_Runtime::clientStart("conf/client.xml", NULL);
    int pos = atoi(argv[1]);
    WASP_VString RetPlanet = "";
    Planet PlanetService;
    RetPlanet = PlanetService.getPlanet(pos);
    PlanetStr = RetPlanet.transcode();
    cout << "Planet number " << pos << " is " << PlanetStr << endl;
    delete PlanetStr;
  }
  catch (WASP_StubFaultException *exc) {
    WASP_XMLProtocolFault *fault = exc->getFault();
    if (fault) {
       WASP_VString msg;
       msg << "Fault Received\n";
       msg << "  Fault Code: "      << fault->getFaultCodeName()      << "\n";
       msg << "  Fault Namespace: " << fault->getFaultCodeNamespace() << "\n";
       msg << "  Fault String: "    << fault->getFaultString()        << "\n";
       char *transcodedMsg = msg.transcode();
       cout << transcodedMsg;
       delete transcodedMsg;
    }
    delete exc;
  } catch (WASP_Exception *exc) {
    char *trace=GET_TRACE (exc);
    cout << "Exception during call: " << exc->getCharMessage() << endl;
    cout << "Stack trace follows: " << endl << trace << endl;
    delete trace;
    delete exc;
  }
  WASP_Runtime::clientTerminate();
  return 0;
}





2


