OpenVMS Source Code Demos

CALCSERVER

#define VERSION	"calcserver-cpp-100.1"						// <<<--- change as required
//===============================================================================================================================
// title:	calcserver.cpp
// author:	Neil Rieck (adapted from "gsoap/samples/calc++/calcserver.c")
// created:	2013-05-27
// os:		OpenVMS-8.4 (Alpha)
// target:	gSOAP for OpenVMS
// compiler:	HP C++ V7.3-009 for OpenVMS Alpha V8.4
// build:	$ cxx/names=(as_is,shortened)/prefix=all/float=ieee/ieee=denorm	-
//			/using_std/define=(__USE_STD_IOSTREAM)			-
//			/include=gsoap$root:[include] calcserver.cpp /list
// caveat:	if you don't do it this way on an OpenVMS machine then the compiler will throw error messages like this:
//			%CXX-E-NOTMEMBER, namespace "std" has no member "istream"
//			%CXX-E-NOTMEMBER, namespace "std" has no member "ostream"
//===============================================================================================================================
#include <iostream>								// required when using c++ apps with gSOAP
#include "SoapCalcService.h"							// note that "c" used "soaph.h"
#include "calc.nsmap"								//
//
int main(int argc, char **argv)
{   calcService calc;
    fprintf(stderr, "-i-program: %s\n",VERSION);				//
    if (argc < 2) {								// if no port argument provided
	fprintf(stderr, "-i-running as a CGI application\n");			//
	calc.serve();								// serve as CGI application
    }else{									//
	int port = atoi(argv[1]);						//
	if (!port) {								//
	    fprintf(stderr, "-e-Usage: calcserver++ <port>\n");			//
	    exit(0);								//
	}									//
	//
	// run iterative server on port until fatal error
	//
	fprintf(stderr, "-i-standalone application listening to port: %ld\n",port);
 	if (calc.run(port)) {
	    calc.soap_stream_fault(std::cerr);
	    exit(-1);
	}
    }
    return 0;
}

int calcService::add(double a, double b, double *result)
{   *result = a + b;
    return SOAP_OK;
}

int calcService::sub(double a, double b, double *result)
{   *result = a - b;
    return SOAP_OK;
}

int calcService::mul(double a, double b, double *result)
{   *result = a * b;
    return SOAP_OK;
}

int calcService::div(double a, double b, double *result)
{   if (b){
	*result = a / b;
    }else{
	char *s = (char*)soap_malloc(this, 1024);
	sprintf(s, "<error xmlns=\"http://tempuri.org/\">Can't divide %f by %f</error>", a, b);
	return soap_senderfault("Division by zero", s);
    }
    return SOAP_OK;
}

int calcService::pow(double a, double b, double *result)
{   *result = ::pow(a, b);
    if (soap_errno == EDOM)					// soap_errno is like errno, but compatible with Win32
    {	char *s = (char*)soap_malloc(this, 1024);
	sprintf(s, "Can't take the power of %f to %f", a, b);
	sprintf(s, "<error xmlns=\"http://tempuri.org/\">Can't take power of %f to %f</error>", a, b);
	return soap_senderfault("Power function domain error", s);
    }
    return SOAP_OK;
}