OpenVMS Source-Code Demos

SIMPLE_SERVICE_SERVER_100.C

//===============================================================================================================================
// title  : simple_service_server_xxx.c
// author : Neil Rieck
// created: 2011-10-14
// Build  :	@build_simple_service_step1_common.com (only need to do this once for every new WSDL)
//		@build_simple_service_step2_server.com
//		@build_simple_service_step3_client.com
// Notes  : Attention Hackers! be prepared to inspect file "GSOAP$ROOT:[000000.INCLUDE]stdsoap2.h"
//          looking at the SOAP structure to find variable names then working backwards
//===============================================================================================================================
// history:
// ver who when   what
// --- --- ------ ---------------------------------------------------------------------------------------------------------------
// 100 NSR 111015 1. original work
//
//	remember to modify VERSION on the first line of this file
//===============================================================================================================================
#define VERSION	"simple_service_server_100.1"					//
#define PATH	"csmis$root3:[dvlp._gsoap_simple_service_2011]"			// our current working directory
#define NSR_DEBUG	0							// must link with gsoap$root:[lib]
#define NSR_STRICT	1							// XML must be well-formed and valid
//
//	import the usual stuff
//
#include <stdio.h>								//
#include <stdlib.h>								//
#include <string.h>								//
#include <time.h>								//
//
//	stuff needed for our gSOAP app
//
#include "SIMPLE_SERVICE_GSC_H.H"						// generated by tool "soapcpp2"
#include "SIMPLESERVICESOAP11BINDING.NSMAP"					// generated by tool "soapcpp2"
//
//==============================================================================
//	main (this is our server)
//==============================================================================
int main(int argc, char **argv)							//
{	int m, s;								// master and slave sockets
	struct soap soap;							//
	soap_init(&soap);							//
#if (NSR_DEBUG==1)								// -----------------------
	char logf1[99];								//
	char logf2[99];								//
	char logf3[99];								//
	sprintf(logf1,"%s%s",PATH,"aaa_s_recv.log");				// for extensive server logging
	sprintf(logf2,"%s%s",PATH,"aaa_s_sent.log");				//
	sprintf(logf3,"%s%s",PATH,"aaa_s_test.log");				//
	soap_set_recv_logfile(&soap, logf1);					// must link to gsoapdbg.olb
	soap_set_sent_logfile(&soap, logf2);					//
	soap_set_test_logfile(&soap, logf3);					//
#endif										// -----------------------
#ifdef NSR_STRICT								// ---------------------
	soap_set_imode(&soap, SOAP_XML_STRICT);					// set input mode strict
#endif										// ---------------------
#ifdef NSR_HTTP10								// ------------------
	soap.http_version = "1.0";						// revert to HTTP 1.0
#endif										// ------------------
	if (argc < 2)								// must have been started from Apache
	{   soap_serve(&soap);							// serve as CGI application
 	}									//
	else									// must have been started from DCL
	{   m = soap_bind(&soap, NULL, atoi(argv[1]), 100);			// argv[1] holds the specified port
	    if (m < 0)								// if some sort of error
	    { soap_print_fault(&soap, stderr);					//
		exit(-1);							//
	    }									//
	    fprintf(stderr, "Program: %s\n",VERSION);				//
	    fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
	    for ( ; ; )								// loop forever
	    {   s = soap_accept(&soap);						//
		fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);
		if (s < 0)							//
		{   soap_print_fault(&soap, stderr);				//
		    exit(-1);							//
		} 								//
		soap_serve(&soap);						//
		soap_end(&soap);						//
		fprintf(stderr, "end-of-server: slave socket\n");		//
	    }									//
	};									//
	return 0;								//
}

//==============================================================================
//	ssAdd (SOAP 1.1)
//
//	1. the following declaration was found in file: SOAPSERVER.C
//	2. it can also be found in file "SOAPSTUB.H" near "Client-Side Call Stubs"
//
//	__ns2__ssAdd(	struct soap*,
//			struct _ns1__ssAdd *ns1__ssAdd,
//			struct _ns1__ssAddResponse *ns1__ssAddResponse);
//==============================================================================
//trek1
int __ns2__ssAdd(	struct soap			*soap,
			struct _ns1__ssAdd		*myAddRequest,
			struct _ns1__ssAddResponse	*myAddResponse)
{
	fprintf(stderr,"%s\n", "ssAdd: start ---");				//
	//----------------------------------------------------------------------
	//	service: ssAdd() starts here
	//----------------------------------------------------------------------
	int param0, param1, param2;						//
	param0	= *myAddRequest->param0;					//
	param1	= *myAddRequest->param1;					//
	param2	= param0 + param1;						// needs some overflow protection
	fprintf(stderr,"param0 %d\n",param0);
	fprintf(stderr,"param1 %d\n",param1);
	fprintf(stderr,"param2 %d\n",param2);
	//----------------------------------------------------
	//	method 1 (easy way which does not work)
	//----------------------------------------------------
//	*myAddResponse.return_	= param2;					//
	//----------------------------------------------------
	//	method 2 (harder way which works)
	//----------------------------------------------------
	int *safe = (int*)soap_malloc(soap, 1);					// allocate mem to survive this function
	*safe = param2;								// copy data
	myAddResponse->return_ = safe;						// copy address
	//----------------------------------------------------------------------
	fprintf(stderr,"ssAdd: normal exit ---\n");				//
	return SOAP_OK;								//
}

//==============================================================================
//	ssSubtract (SOAP 1.1)
//
//	1. the following declaration was found in file: SOAPCLIENT.C
//	2. it can also be found in file "SOAPSTUB.H" near "Client-Side Call Stubs"
//
//	__ns2__ssSubtract(	struct soap*,
//				struct _ns1__ssSubtract *ns1__ssSubtract,
//				struct _ns1__ssSubtractResponse *ns1__ssSubtractResponse);
//==============================================================================
//trek2
int __ns2__ssSubtract(	struct soap			*soap,
			struct _ns1__ssSubtract 	*mySubtractRequest,
			struct _ns1__ssSubtractResponse	*mySubtractResponse)
{
	fprintf(stderr,"firing: ssSubtract\n");					//
	int param0, param1, param2;						//
	param0	= *mySubtractRequest->param0;					//
	param1	= *mySubtractRequest->param1;					//
	param2	= param0 - param1;						// needs some overflow protection
	//----------------------------------------------------
	//	method 1 (easy way which does not work)
	//----------------------------------------------------
//	mySubtractResponse.return_	= param2;				//
	//----------------------------------------------------
	//	method 2 (harder way which works)
	//----------------------------------------------------
	int *safe = (int*)soap_malloc(soap, 1);					// allocate mem to survive this function
	*safe = param2;								// copy data
	mySubtractResponse->return_ = safe;					// copy address
	//----------------------------------------------------------------------
	fprintf(stderr,"ssAdd: normal exit ---\n");				//
	return SOAP_OK;								//
}

//==============================================================================
//	ssEcho (SOAP 1.1)
//
//	1. the following declaration was found in file: SOAPCLIENT.C
//	2. it can also be found in file "SOAPSTUB.H" near "Client-Side Call Stubs"
//
//	__ns2__ssEcho(		struct soap*,
//				struct _ns1__ssEcho *ns1__ssEcho,
//				struct _ns1__ssEchoResponse *ns1__ssEchoResponse);
//==============================================================================
//trek3
int __ns2__ssEcho(	struct soap			*soap,
			struct _ns1__ssEcho		*myEchoRequest,
			struct _ns1__ssEchoResponse	*myEchoResponse)
{
	fprintf(stderr,"firing: ssEcho\n");					//
	fprintf(stderr,"%s%s\n","data: ",myEchoRequest->param0);		//
	//----------------------------------------------------
	//	method 1 (easy way which does not work)
	//----------------------------------------------------
//	char buffer[255] = '\0';
//	sprintf(buffer,"You sent: %s\n",myEchoRequest.param0");
//	mySubtractResponse->return_ = buffer;					// copy input directly to output
	//----------------------------------------------------
	//	method 2 (harder way which works)
	//----------------------------------------------------
	int my_Size = strlen(myEchoRequest->param0);				// get the size of the string sent here
	char *s = (char*)soap_malloc(soap, my_Size + 20);			// allocate mem to survive this function
	sprintf(s, "You sent \"%s\"",myEchoRequest->param0);			//
	myEchoResponse->return_ =  s;						// copy address of new variable
	fprintf(stderr,"Sending back: %s\n",s);					// send this to stdout (or DCL)
	//----------------------------------------------------------------------
	fprintf(stderr,"ssEcho: normal exit ---\n");				//
	return SOAP_OK;								//
}

//====================================================================================
//	ssEcho (soap 1.2)
//====================================================================================
int __ns3__ssEcho(	struct soap			*soap,
			struct _ns1__ssEcho		*ns1__ssEcho,
			struct _ns1__ssEchoResponse	*ns1__ssEchoResponse)
{
	fprintf(stderr,"ssEcho (soap 1.2)\n");						//
	int yada = __ns2__ssEcho(soap, ns1__ssEcho, ns1__ssEchoResponse);		//
	return yada;									//
}

//====================================================================================
//	ssAdd (soap 1.2)
//====================================================================================
int __ns3__ssAdd(	struct soap			*soap,
			struct _ns1__ssAdd		*ns1__ssAdd,
			struct _ns1__ssAddResponse	*ns1__ssAddResponse)
{
	fprintf(stderr,"ssAdd (soap 1.2)\n");						//
	int yada = __ns2__ssAdd(soap, ns1__ssAdd,ns1__ssAddResponse);			//
	return yada;									//
}

//====================================================================================
//	ssSubtract (soap 1.2)
//====================================================================================
int __ns3__ssSubtract(	struct soap			*soap,
			struct _ns1__ssSubtract		*ns1__ssSubtract,
			struct _ns1__ssSubtractResponse	*ns1__ssSubtractResponse)
{
	fprintf(stderr,"ssSubtract (soap 1.2)\n");					//
	int yada = __ns2__ssSubtract(soap, ns1__ssSubtract, ns1__ssSubtractResponse);	//
	return yada;									//
}