xmlrpc_sample_add_server_cgi.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* A CGI script that effects a simple XML-RPC server, written in C++.
  2. See the identically named C program source code for hints on running
  3. this example.
  4. */
  5. #include <cassert>
  6. #include <iostream>
  7. #include <xmlrpc-c/base.hpp>
  8. #include <xmlrpc-c/registry.hpp>
  9. #include <xmlrpc-c/server_cgi.hpp>
  10. using namespace std;
  11. class sampleAddMethod : public xmlrpc_c::method {
  12. public:
  13. sampleAddMethod() {
  14. // signature and help strings are documentation -- the client
  15. // can query this information with a system.methodSignature and
  16. // system.methodHelp RPC.
  17. this->_signature = "i:ii"; // method's arguments, result are integers
  18. this->_help = "This method adds two integers together";
  19. }
  20. void
  21. execute(xmlrpc_c::paramList const& paramList,
  22. xmlrpc_c::value * const retvalP) {
  23. int const addend(paramList.getInt(0));
  24. int const adder(paramList.getInt(1));
  25. paramList.verifyEnd(2);
  26. *retvalP = xmlrpc_c::value_int(addend + adder);
  27. }
  28. };
  29. int
  30. main(int const,
  31. const char ** const) {
  32. try {
  33. xmlrpc_c::registry myRegistry;
  34. xmlrpc_c::methodPtr const sampleAddMethodP(new sampleAddMethod);
  35. myRegistry.addMethod("sample.add", sampleAddMethodP);
  36. xmlrpc_c::serverCgi myServer(
  37. xmlrpc_c::serverCgi::constrOpt()
  38. .registryP(&myRegistry));
  39. myServer.processCall();
  40. } catch (exception const& e) {
  41. cerr << "Something failed. " << e.what() << endl;
  42. }
  43. return 0;
  44. }