xmlrpc_sample_add_server.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <cassert>
  2. #include <stdexcept>
  3. #include <iostream>
  4. #ifdef WIN32
  5. # include <windows.h>
  6. #else
  7. # include <unistd.h>
  8. #endif
  9. #include <xmlrpc-c/base.hpp>
  10. #include <xmlrpc-c/registry.hpp>
  11. #include <xmlrpc-c/server_abyss.hpp>
  12. using namespace std;
  13. #ifdef WIN32
  14. #define SLEEP(seconds) SleepEx(seconds * 1000);
  15. #else
  16. #define SLEEP(seconds) sleep(seconds);
  17. #endif
  18. class sampleAddMethod : public xmlrpc_c::method {
  19. public:
  20. sampleAddMethod() {
  21. // signature and help strings are documentation -- the client
  22. // can query this information with a system.methodSignature and
  23. // system.methodHelp RPC.
  24. this->_signature = "i:ii";
  25. // method's result and two arguments are integers
  26. this->_help = "This method adds two integers together";
  27. }
  28. void
  29. execute(xmlrpc_c::paramList const& paramList,
  30. xmlrpc_c::value * const retvalP) {
  31. int const addend(paramList.getInt(0));
  32. int const adder(paramList.getInt(1));
  33. paramList.verifyEnd(2);
  34. *retvalP = xmlrpc_c::value_int(addend + adder);
  35. // Sometimes, make it look hard (so client can see what it's like
  36. // to do an RPC that takes a while).
  37. if (adder == 1)
  38. SLEEP(2);
  39. }
  40. };
  41. int
  42. main(int const,
  43. const char ** const) {
  44. try {
  45. xmlrpc_c::registry myRegistry;
  46. xmlrpc_c::methodPtr const sampleAddMethodP(new sampleAddMethod);
  47. myRegistry.addMethod("sample.add", sampleAddMethodP);
  48. xmlrpc_c::serverAbyss myAbyssServer(
  49. xmlrpc_c::serverAbyss::constrOpt()
  50. .registryP(&myRegistry)
  51. .portNumber(8080));
  52. myAbyssServer.run();
  53. // xmlrpc_c::serverAbyss.run() never returns
  54. assert(false);
  55. } catch (exception const& e) {
  56. cerr << "Something failed. " << e.what() << endl;
  57. }
  58. return 0;
  59. }