Main BLOGGER
Google
WWW THIS BLOG
Thursday, June 16, 2005
 
asynchronous messaging in gSoap
1. Define asynchronous style messaging interface. The point is use Void as the last parameter. gSoap will take it as the output by default. Since the output is a void, gSoap will generate asynchronous codes for this head file.

-------------------------------------------------------------------------------------------
//gsoap ns service name: Calc service
//gsoap ns service style: document
//gsoap ns service encoding: literal
//gsoap ns service namespace: http://platform.com/AsynCalc.wsdl
//gsoap ns service location: http://localhost:2005

//gsoap ns schema namespace: urn:AsynCalc
enum ns__CalcType {CALC_ADD, CALC_SUB, CALC_MUL, CALC_DIV, CALC_DONE };

//gsoap ns service method-action: Evaluate according to "CalcType"
int ns__AsynCalc(int opA, int opB, enum ns__CalcType calcType, int calcResult, void);
-------------------------------------------------------------------------------------------

2. On server side
(1) create soap with SOAP_IO_KEEPALIVE
soap_init2(&soap, SOAP_IO_KEEPALIVE, SOAP_IO_KEEPALIVE);
/*From the source code in stdsoap2.cpp, we will see soap_closesock() will not close the socket if this flag is set. */

(2) In the event handling function
int ns__AsynCalc(struct soap *soap, int opA, int opB, enum ns__CalcType calcType, int calcResult)

<1> (option) using soap_send_empty_response() to notify the receipt of request.
<2> using soap_send_ns__AsynCalc() which is defined in client side code "soapclient.cpp" to send the result back to the client. Note: both the server and the client will use the same XML message schema (defined in WSDL as input message)
And since the socket is kept alive, we dont need to specify the endpoint URL and action, e.g.
soap_send_ns__AsynCalc(resp, "http://", NULL,opA,opB,calcType,m_result);

3. On the client side
(1) create soap with SOAP_IO_KEEPALIVE
soap_init2(&soap, SOAP_IO_KEEPALIVE, SOAP_IO_KEEPALIVE);
(2) using soap_send_ns__AsynCalc() to send the asynchronous request to the server, e.g.,
soap_send_ns__AsynCalc( &soap,__endpoint,__action,opA,opB,CALC_ADD,result);
(3) At some point, if the client wants to fetch the response back, it will call
soap_recv_ns__AsynCalc()
Note: Since this function is non-blocking, the client needs to check the receiving status in a loop or something like that.


struct ns__AsynCalc response;
for (;;)
{
if (!soap_valid_socket(soap.socket))
{
fprintf(stderr,"connection was terminated\n");
break;
}
if (soap_recv_ns__AsynCalc(&soap,&response))
{
if (soap.error == SOAP_EOF)
fprintf(stderr,"connection was gracefully closed by server\n");
else
soap_print_fault(&soap,stderr);
break;
}else
{
fprintf(stderr,"result = %d\n",response.calcResult);
}
}



<< Home

Powered by Blogger

Google
WWW THIS BLOG