2
0

xmlrpc_asynch_client.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* A simple asynchronous XML-RPC client program written in C, as an example of
  2. Xmlrpc-c asynchronous RPC facilities. This is the same as the
  3. simpler synchronous client xmlprc_sample_add_client.c, except that
  4. it adds 3 different pairs of numbers with the summation RPCs going on
  5. simultaneously.
  6. Use this with xmlrpc_sample_add_server. Note that that server
  7. intentionally takes extra time to add 1 to anything, so you can see
  8. our 5+1 RPC finish after our 5+0 and 5+2 RPCs.
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <xmlrpc-c/base.h>
  13. #include <xmlrpc-c/client.h>
  14. #include "config.h" /* information about this build environment */
  15. #define NAME "Xmlrpc-c Asynchronous Test Client"
  16. #define VERSION "1.0"
  17. static void
  18. die_if_fault_occurred(xmlrpc_env * const envP) {
  19. if (envP->fault_occurred) {
  20. fprintf(stderr, "Something failed. %s (XML-RPC fault code %d)\n",
  21. envP->fault_string, envP->fault_code);
  22. exit(1);
  23. }
  24. }
  25. static void
  26. handle_sample_add_response(const char * const serverUrl,
  27. const char * const methodName,
  28. xmlrpc_value * const paramArrayP,
  29. void * const user_data,
  30. xmlrpc_env * const faultP,
  31. xmlrpc_value * const resultP) {
  32. xmlrpc_env env;
  33. xmlrpc_int addend, adder;
  34. /* Initialize our error environment variable */
  35. xmlrpc_env_init(&env);
  36. /* Our first four arguments provide helpful context. Let's grab the
  37. addends from our parameter array.
  38. */
  39. xmlrpc_decompose_value(&env, paramArrayP, "(ii)", &addend, &adder);
  40. die_if_fault_occurred(&env);
  41. printf("RPC with method '%s' at URL '%s' to add %d and %d "
  42. "has completed\n", methodName, serverUrl, addend, adder);
  43. if (faultP->fault_occurred)
  44. printf("The RPC failed. %s\n", faultP->fault_string);
  45. else {
  46. xmlrpc_int sum;
  47. xmlrpc_read_int(&env, resultP, &sum);
  48. die_if_fault_occurred(&env);
  49. printf("The sum is %d\n", sum);
  50. }
  51. }
  52. int
  53. main(int const argc,
  54. const char ** const argv) {
  55. const char * const serverUrl = "http://localhost:8080/RPC2";
  56. const char * const methodName = "sample.add";
  57. xmlrpc_env env;
  58. xmlrpc_client * clientP;
  59. xmlrpc_int adder;
  60. if (argc-1 > 0) {
  61. fprintf(stderr, "This program has no arguments\n");
  62. exit(1);
  63. }
  64. /* Initialize our error environment variable */
  65. xmlrpc_env_init(&env);
  66. /* Required before any use of Xmlrpc-c client library: */
  67. xmlrpc_client_setup_global_const(&env);
  68. die_if_fault_occurred(&env);
  69. xmlrpc_client_create(&env, XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION, NULL, 0,
  70. &clientP);
  71. die_if_fault_occurred(&env);
  72. for (adder = 0; adder < 3; ++adder) {
  73. printf("Making XMLRPC call to server url '%s' method '%s' "
  74. "to request the sum "
  75. "of 5 and %d...\n", serverUrl, methodName, adder);
  76. /* request the remote procedure call */
  77. xmlrpc_client_start_rpcf(&env, clientP, serverUrl, methodName,
  78. handle_sample_add_response, NULL,
  79. "(ii)", (xmlrpc_int32) 5, adder);
  80. die_if_fault_occurred(&env);
  81. }
  82. printf("RPCs all requested. Waiting for & handling responses...\n");
  83. /* Wait for all RPCs to be done. With some transports, this is also
  84. what causes them to go.
  85. */
  86. xmlrpc_client_event_loop_finish(clientP);
  87. printf("All RPCs finished.\n");
  88. xmlrpc_client_destroy(clientP);
  89. xmlrpc_client_teardown_global_const();
  90. return 0;
  91. }