Sunday, March 18, 2012

Axis C/C++ for Client/Server Apps

Axis is a c++ implementation of a runtime engine with client and server support for web applications. It features a soap engine. A WSDL2WS tool is used to generate client and server side stubs. Client side apps are written to produce output from a server side implementation. The below program is a calculator client side app. A subsequent server app exists to produce results from queries from the client app.
The classpath icludes the following jars:
 <AxisCPP Install dir>/lib/axis/wsdl2ws.jar: Contains the main WSDL2Ws code.
<AxisCPP Install dir>/lib/axisjava/axis.jar: Contains the Axis java code which WSDL2Ws is
based on
<AxisCPP Install dir>/lib/axisjava/commons-discovery.jar:
<AxisCPP Install dir>/lib/axisjava/commons-logging.jar;
<AxisCPP Install dir>/lib/axisjava/jaxrpc.jar;
<AxisCPP Install dir>/lib/axisjava/saaj.jar;
<AxisCPP Install dir>/lib/axisjava/wsdl4j.jar
java org.apache.axis.wsdl.wsdl2ws.WSDL2Ws Calculator.wsdl -lc++ -sclient
The above  command generates the client side stub. The *.cpp are then made into a client side service by the below instruction.

g++ *.cpp -I<Axis installation directory>/include -L<Axis installation
directory>/lib -ldl -laxiscpp_client -ocalculator

The calculator service is invoked by the:
./calculator add 10 5 http://localhost/axis/Calculator command.

#include "Calculator.hpp"
#include <axis/AxisException.hpp>
#include <iostream>
bool IsNumber (const char *p);
static void
usage (char *programName, char *defaultURL)
{
    cout << "\nUsage:\n"
 << programName << " [-? | div number1 number2 [service_url]] " << endl
 << "    -?             Show this help.\n"
 << "    service_url    URL of the service.\n"
 << "    Default service URL is assumed to be " << defaultURL << endl;
}
int
main (int argc, char *argv[])
{
    char endpoint[256];
    char original[256];
    const char *server = "localhost";
    const char *port = "80";
    const char *op = 0;
    const char *p1 = 0;
    const char *p2 = 0;
    int i1 = 0, i2 = 0;
    int iResult;
    // Set default service URL
    sprintf (endpoint, "http://localhost/axis/calculator");
    sprintf (original, endpoint);
    try
    {
 if (argc == 1)
 {
     usage (argv[0], endpoint);
     return 2;
 }
 if (argc > 1)
 {
     if (!strncmp (argv[1], "-", 1))
     {
  // Check for - only so that it works for
  //-?, -h or --help; -anything
  usage (argv[0], endpoint);
  return 2;
     }
     //less than minimum number of args OR greater than maximum number of args
     else if (argc < 4 || argc > 5)
     {
  usage (argv[0], endpoint);
  return 2;
     }
     else if (argc == 5)
     {
  sprintf (endpoint, argv[4]);
     }
 }
 cout << endl << " Using service at " << endpoint << endl << endl;
 Calculator ws (endpoint);
 op = argv[1];
 p1 = argv[2];
 p2 = argv[3];
 if (!IsNumber (p1))
 {
     printf ("Invalid value for first <parameter> %s\n\n", p1);
     usage (argv[0], original);
     return 2;
 }
 if (!IsNumber (p2))
 {
     printf ("Invalid value for second <parameter> %s\n\n", p2);
     usage (argv[0], original);
     return 2;
 }
 i1 = atoi (p1);
 i2 = atoi (p2);
 if (strcmp (op, "add") == 0)
 {
     iResult = ws.add (i1, i2);
     printf ("%d\n", iResult);
 }
 else if (strcmp (op, "sub") == 0)
 {
     iResult = ws.sub (i1, i2);
     printf ("%d\n", iResult);
 }
 else if (strcmp (op, "mul") == 0)
 {
     iResult = ws.mul (i1, i2);
     printf ("%d\n", iResult);
 }
 else if (strcmp (op, "div") == 0)
 {
     iResult = ws.div (i1, i2);
     printf ("%d\n", iResult);
 }
 else
 {
     printf ("Invalid operation %s\n\n", op);
     usage (argv[0], original);
     return 2;
 }
    }
    catch (AxisException & e)
    {
 printf ("Exception : %s\n", e.what ());
    }
    catch (exception & e)
    {
 printf ("Unknown exception has occured\n");
    }
    catch (...)
    {
 printf ("Unknown exception has occured\n");
    }
    return 0;
}
bool
IsNumber (const char *p)
{
    for (int x = 0; x < strlen (p); x++)
    {
 if (!isdigit (p[x]))
     return false;
    }
    return true;
}

No comments:

Post a Comment