xmlrpc_loop_server.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* A simple standalone XML-RPC server based on Abyss that contains a
  2. simple one-thread request processing loop.
  3. xmlrpc_sample_add_server.cpp is a server that does the same thing, but
  4. does it by running a full Abyss daemon in the background, so it has
  5. less control over how the requests are served.
  6. */
  7. #include <cassert>
  8. #include <iostream>
  9. #include <xmlrpc-c/base.hpp>
  10. #include <xmlrpc-c/registry.hpp>
  11. #include <xmlrpc-c/server_abyss.hpp>
  12. using namespace std;
  13. class sampleAddMethod : public xmlrpc_c::method {
  14. public:
  15. sampleAddMethod() {
  16. // signature and help strings are documentation -- the client
  17. // can query this information with a system.methodSignature and
  18. // system.methodHelp RPC.
  19. this->_signature = "i:ii"; // method's arguments, result are integers
  20. this->_help = "This method adds two integers together";
  21. }
  22. void
  23. execute(xmlrpc_c::paramList const& paramList,
  24. xmlrpc_c::value * const retvalP) {
  25. int const addend(paramList.getInt(0));
  26. int const adder(paramList.getInt(1));
  27. paramList.verifyEnd(2);
  28. *retvalP = xmlrpc_c::value_int(addend + adder);
  29. }
  30. };
  31. int
  32. main(int const,
  33. const char ** const) {
  34. try {
  35. xmlrpc_c::registry myRegistry;
  36. xmlrpc_c::methodPtr const sampleAddMethodP(new sampleAddMethod);
  37. myRegistry.addMethod("sample.add", sampleAddMethodP);
  38. xmlrpc_c::serverAbyss myAbyssServer(
  39. xmlrpc_c::serverAbyss::constrOpt()
  40. .registryP(&myRegistry)
  41. .portNumber(8080)
  42. .logFileName("/tmp/xmlrpc_log"));
  43. while (true) {
  44. cout << "Waiting for next RPC..." << endl;
  45. myAbyssServer.runOnce();
  46. /* This waits for the next connection, accepts it, reads the
  47. HTTP POST request, executes the indicated RPC, and closes
  48. the connection.
  49. */
  50. }
  51. } catch (exception const& e) {
  52. cerr << "Something failed. " << e.what() << endl;
  53. }
  54. return 0;
  55. }