asynch_client.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*=============================================================================
  2. asynch_client.cpp
  3. ===============================================================================
  4. This is an example of an XML-RPC client that uses XML-RPC for C/C++
  5. (Xmlrpc-c).
  6. In particular, it does multiple RPCs asynchronously, running
  7. simultaneously.
  8. =============================================================================*/
  9. #include <cassert>
  10. #include <cstdlib>
  11. #include <string>
  12. #include <iostream>
  13. #include <xmlrpc-c/girerr.hpp>
  14. #include <xmlrpc-c/base.hpp>
  15. #include <xmlrpc-c/client.hpp>
  16. using namespace std;
  17. int
  18. main(int argc, char **) {
  19. if (argc-1 > 0) {
  20. cerr << "This program has no arguments" << endl;
  21. exit(1);
  22. }
  23. try {
  24. xmlrpc_c::clientXmlTransport_curl myTransport;
  25. xmlrpc_c::client_xml myClient(&myTransport);
  26. string const methodName("sample.add");
  27. xmlrpc_c::paramList sampleAddParms1;
  28. sampleAddParms1.add(xmlrpc_c::value_int(3));
  29. sampleAddParms1.add(xmlrpc_c::value_int(1));
  30. xmlrpc_c::rpcPtr rpc1P(methodName, sampleAddParms1);
  31. xmlrpc_c::paramList sampleAddParms2;
  32. sampleAddParms2.add(xmlrpc_c::value_int(5));
  33. sampleAddParms2.add(xmlrpc_c::value_int(7));
  34. xmlrpc_c::rpcPtr rpc2P(methodName, sampleAddParms2);
  35. string const serverUrl("http://localhost:8080/RPC2");
  36. xmlrpc_c::carriageParm_curl0 myCarriageParm(serverUrl);
  37. rpc1P->start(&myClient, &myCarriageParm);
  38. rpc2P->start(&myClient, &myCarriageParm);
  39. cout << "Two RPCs started. Waiting for them to finish." << endl;
  40. myClient.finishAsync(xmlrpc_c::timeout()); // infinite timeout
  41. assert(rpc1P->isFinished());
  42. assert(rpc2P->isFinished());
  43. int const sum1(xmlrpc_c::value_int(rpc1P->getResult()));
  44. int const sum2(xmlrpc_c::value_int(rpc2P->getResult()));
  45. cout << "Result of RPC 1 (sum of 3 and 1): " << sum1 << endl;
  46. cout << "Result of RPC 2 (sum of 5 and 7): " << sum2 << endl;
  47. } catch (exception const& e) {
  48. cerr << "Client threw error: " << e.what() << endl;
  49. } catch (...) {
  50. cerr << "Client threw unexpected error." << endl;
  51. }
  52. return 0;
  53. }