2
0

compound_value_client.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* An XML-RPC client program written in C, as an example of using
  2. compound XML-RPC values.
  3. For a simple client program that just deals with integer values,
  4. see xmlrpc_sample_add_client.c. This example focuses just on the
  5. compound XML-RPC values and not the client functions.
  6. This client invokes the example.divide XML-RPC method that the example
  7. server program compound_value_server.c provides. That method takes a
  8. list of pairs of numbers and returns the list of their quotients.
  9. Compound XML-RPC values are arrays and structures. We call them compound
  10. because they are made up of other XML-RPC values (e.g. an array of XML-RPC
  11. integers).
  12. The arguments to the example.divide method are specified as follows:
  13. There are two arguments:
  14. Argument 0: Integer. Version number of this argument protocol. Must
  15. be 1.
  16. Argument 1: Array. One element for each pair of numbers you want the
  17. server to divide. Each element is structure, with these
  18. members:
  19. KEY: "dividend"
  20. VALUE: floating point number. The dividend.
  21. KEY: "divisor"
  22. VALUE: floating point number. The divisor.
  23. The result of the method is an array. It has one member for each pair of
  24. numbers in the arguments (So it is the same size as Argument 1). That
  25. member is a floating point number. It is the quotient of the numbers
  26. in the corresponding element of Argument 1.
  27. The client sends the RPC to the server running on the local system
  28. ("localhost"), HTTP Port 8080.
  29. */
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <xmlrpc-c/base.h>
  33. #include <xmlrpc-c/client.h>
  34. #include "config.h" /* information about this build environment */
  35. #define NAME "Xmlrpc-c Test Client"
  36. #define VERSION "1.0"
  37. static void
  38. dieIfFaultOccurred (xmlrpc_env * const envP) {
  39. if (envP->fault_occurred) {
  40. fprintf(stderr, "ERROR: %s (%d)\n",
  41. envP->fault_string, envP->fault_code);
  42. exit(1);
  43. }
  44. }
  45. struct ratio {
  46. double dividend;
  47. double divisor;
  48. };
  49. int
  50. main(int const argc,
  51. const char ** const argv) {
  52. const char * const serverUrl = "http://localhost:8080/RPC2";
  53. const char * const methodName = "example.divide";
  54. unsigned int const argVersion = 1;
  55. struct ratio const data[] = {{1,2},{12,3},{10,3},{89,3000}};
  56. xmlrpc_env env;
  57. xmlrpc_value * resultP;
  58. unsigned int i;
  59. xmlrpc_value * ratioArrayP;
  60. unsigned int quotientCt;
  61. if (argc-1 > 0) {
  62. fprintf(stderr, "This program has no arguments\n");
  63. exit(1);
  64. }
  65. xmlrpc_env_init(&env);
  66. xmlrpc_client_init2(&env, XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION, NULL, 0);
  67. dieIfFaultOccurred(&env);
  68. /* Build the 2nd method argument: the array of ratios */
  69. ratioArrayP = xmlrpc_array_new(&env);
  70. dieIfFaultOccurred(&env);
  71. for (i = 0; i < 4; ++i) {
  72. xmlrpc_value * dividendP;
  73. xmlrpc_value * divisorP;
  74. xmlrpc_value * ratioP;
  75. dividendP = xmlrpc_double_new(&env, data[i].dividend);
  76. dieIfFaultOccurred(&env);
  77. divisorP = xmlrpc_double_new(&env, data[i].divisor);
  78. dieIfFaultOccurred(&env);
  79. ratioP = xmlrpc_struct_new(&env);
  80. dieIfFaultOccurred(&env);
  81. xmlrpc_struct_set_value(&env, ratioP, "DIVIDEND", dividendP);
  82. dieIfFaultOccurred(&env);
  83. xmlrpc_struct_set_value(&env, ratioP, "DIVISOR", divisorP);
  84. dieIfFaultOccurred(&env);
  85. xmlrpc_array_append_item(&env, ratioArrayP, ratioP);
  86. dieIfFaultOccurred(&env);
  87. xmlrpc_DECREF(ratioP);
  88. xmlrpc_DECREF(divisorP);
  89. xmlrpc_DECREF(dividendP);
  90. }
  91. /* Make the call */
  92. resultP = xmlrpc_client_call(&env, serverUrl, methodName, "(iA)",
  93. (xmlrpc_int32) argVersion, ratioArrayP);
  94. dieIfFaultOccurred(&env);
  95. /* Print out the quotients returned */
  96. quotientCt = xmlrpc_array_size(&env, resultP);
  97. dieIfFaultOccurred(&env);
  98. for (i = 0; i < quotientCt; ++i) {
  99. xmlrpc_value * quotientP;
  100. xmlrpc_double quotient;
  101. xmlrpc_array_read_item(&env, resultP, i, &quotientP);
  102. dieIfFaultOccurred(&env);
  103. xmlrpc_read_double(&env, quotientP, &quotient);
  104. dieIfFaultOccurred(&env);
  105. printf("Server says quotient %u is %f\n", i, quotient);
  106. xmlrpc_DECREF(quotientP);
  107. }
  108. xmlrpc_DECREF(resultP);
  109. xmlrpc_env_clean(&env);
  110. xmlrpc_client_cleanup();
  111. return 0;
  112. }