2012-10-13 12:37:25 -04:00
|
|
|
/* A simple asynchronous XML-RPC client program written in C, as an example of
|
2006-12-20 22:57:49 -05:00
|
|
|
Xmlrpc-c asynchronous RPC facilities. This is the same as the
|
|
|
|
simpler synchronous client xmlprc_sample_add_client.c, except that
|
|
|
|
it adds 3 different pairs of numbers with the summation RPCs going on
|
|
|
|
simultaneously.
|
2008-05-23 16:56:24 -04:00
|
|
|
|
|
|
|
Use this with xmlrpc_sample_add_server. Note that that server
|
|
|
|
intentionally takes extra time to add 1 to anything, so you can see
|
|
|
|
our 5+1 RPC finish after our 5+0 and 5+2 RPCs.
|
2006-12-20 22:57:49 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <xmlrpc-c/base.h>
|
|
|
|
#include <xmlrpc-c/client.h>
|
|
|
|
|
|
|
|
#include "config.h" /* information about this build environment */
|
|
|
|
|
|
|
|
#define NAME "Xmlrpc-c Asynchronous Test Client"
|
|
|
|
#define VERSION "1.0"
|
|
|
|
|
|
|
|
static void
|
2008-05-23 16:56:24 -04:00
|
|
|
die_if_fault_occurred(xmlrpc_env * const envP) {
|
|
|
|
if (envP->fault_occurred) {
|
2006-12-20 22:57:49 -05:00
|
|
|
fprintf(stderr, "Something failed. %s (XML-RPC fault code %d)\n",
|
2008-05-23 16:56:24 -04:00
|
|
|
envP->fault_string, envP->fault_code);
|
2006-12-20 22:57:49 -05:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-05-23 16:56:24 -04:00
|
|
|
handle_sample_add_response(const char * const serverUrl,
|
|
|
|
const char * const methodName,
|
|
|
|
xmlrpc_value * const paramArrayP,
|
2012-10-13 12:37:25 -04:00
|
|
|
void * const user_data,
|
2006-12-20 22:57:49 -05:00
|
|
|
xmlrpc_env * const faultP,
|
|
|
|
xmlrpc_value * const resultP) {
|
2008-05-23 16:56:24 -04:00
|
|
|
|
2006-12-20 22:57:49 -05:00
|
|
|
xmlrpc_env env;
|
|
|
|
xmlrpc_int addend, adder;
|
|
|
|
|
|
|
|
/* Initialize our error environment variable */
|
|
|
|
xmlrpc_env_init(&env);
|
|
|
|
|
|
|
|
/* Our first four arguments provide helpful context. Let's grab the
|
|
|
|
addends from our parameter array.
|
|
|
|
*/
|
2008-05-23 16:56:24 -04:00
|
|
|
xmlrpc_decompose_value(&env, paramArrayP, "(ii)", &addend, &adder);
|
2006-12-20 22:57:49 -05:00
|
|
|
die_if_fault_occurred(&env);
|
|
|
|
|
|
|
|
printf("RPC with method '%s' at URL '%s' to add %d and %d "
|
2008-05-23 16:56:24 -04:00
|
|
|
"has completed\n", methodName, serverUrl, addend, adder);
|
2006-12-20 22:57:49 -05:00
|
|
|
|
|
|
|
if (faultP->fault_occurred)
|
2008-05-23 16:56:24 -04:00
|
|
|
printf("The RPC failed. %s\n", faultP->fault_string);
|
2006-12-20 22:57:49 -05:00
|
|
|
else {
|
|
|
|
xmlrpc_int sum;
|
|
|
|
|
|
|
|
xmlrpc_read_int(&env, resultP, &sum);
|
|
|
|
die_if_fault_occurred(&env);
|
|
|
|
|
|
|
|
printf("The sum is %d\n", sum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int const argc,
|
2012-10-13 12:37:25 -04:00
|
|
|
const char ** const argv) {
|
2006-12-20 22:57:49 -05:00
|
|
|
|
2008-05-23 16:56:24 -04:00
|
|
|
const char * const serverUrl = "http://localhost:8080/RPC2";
|
|
|
|
const char * const methodName = "sample.add";
|
2006-12-20 22:57:49 -05:00
|
|
|
|
|
|
|
xmlrpc_env env;
|
2008-05-23 16:56:24 -04:00
|
|
|
xmlrpc_client * clientP;
|
2006-12-20 22:57:49 -05:00
|
|
|
xmlrpc_int adder;
|
|
|
|
|
|
|
|
if (argc-1 > 0) {
|
|
|
|
fprintf(stderr, "This program has no arguments\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize our error environment variable */
|
|
|
|
xmlrpc_env_init(&env);
|
|
|
|
|
2008-05-23 16:56:24 -04:00
|
|
|
/* Required before any use of Xmlrpc-c client library: */
|
|
|
|
xmlrpc_client_setup_global_const(&env);
|
|
|
|
die_if_fault_occurred(&env);
|
|
|
|
|
|
|
|
xmlrpc_client_create(&env, XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION, NULL, 0,
|
|
|
|
&clientP);
|
2006-12-20 22:57:49 -05:00
|
|
|
die_if_fault_occurred(&env);
|
|
|
|
|
|
|
|
for (adder = 0; adder < 3; ++adder) {
|
|
|
|
printf("Making XMLRPC call to server url '%s' method '%s' "
|
|
|
|
"to request the sum "
|
2008-05-23 16:56:24 -04:00
|
|
|
"of 5 and %d...\n", serverUrl, methodName, adder);
|
2006-12-20 22:57:49 -05:00
|
|
|
|
|
|
|
/* request the remote procedure call */
|
2008-05-23 16:56:24 -04:00
|
|
|
xmlrpc_client_start_rpcf(&env, clientP, serverUrl, methodName,
|
2006-12-20 22:57:49 -05:00
|
|
|
handle_sample_add_response, NULL,
|
|
|
|
"(ii)", (xmlrpc_int32) 5, adder);
|
|
|
|
die_if_fault_occurred(&env);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("RPCs all requested. Waiting for & handling responses...\n");
|
|
|
|
|
2008-05-23 16:56:24 -04:00
|
|
|
/* Wait for all RPCs to be done. With some transports, this is also
|
|
|
|
what causes them to go.
|
|
|
|
*/
|
|
|
|
xmlrpc_client_event_loop_finish(clientP);
|
2006-12-20 22:57:49 -05:00
|
|
|
|
|
|
|
printf("All RPCs finished.\n");
|
|
|
|
|
2008-05-23 16:56:24 -04:00
|
|
|
xmlrpc_client_destroy(clientP);
|
|
|
|
xmlrpc_client_teardown_global_const();
|
2006-12-20 22:57:49 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|