2
0

xmlrpc_inetd_server.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* A simple XML-RPC server that runs under Inetd. I.e. it lets the invoking
  2. program handle all the connection switching and simply processes one
  3. RPC on the provided connection (Standard Input) and exits.
  4. A typical example of where this would be useful is with an Inetd
  5. "super server."
  6. xmlrpc_sample_add_server.cpp is a server that does the same thing,
  7. but you give it a TCP port number and it listens for TCP connections
  8. and processes RPCs ad infinitum. xmlrpc_socket_server.c is halfway
  9. in between those -- you give it an already bound and listening
  10. socket, and it listens for TCP connections and processes RPCs ad
  11. infinitum.
  12. Here is an easy way to test this program:
  13. socketexec --accept --local_port=8080 --stdin -- ./xmlrpc_inetd_server
  14. Now run the client program 'xmlrpc_sample_add_client'. Socketexec
  15. will accept the connection that the client program requests and pass it
  16. to this program on Standard Input. This program will perform the RPC,
  17. respond to the client, then exit.
  18. */
  19. #ifndef WIN32
  20. #include <unistd.h>
  21. #endif
  22. #include <cassert>
  23. #include <xmlrpc-c/base.hpp>
  24. #include <xmlrpc-c/registry.hpp>
  25. #include <xmlrpc-c/server_abyss.hpp>
  26. using namespace std;
  27. class sampleAddMethod : public xmlrpc_c::method {
  28. public:
  29. sampleAddMethod() {
  30. // signature and help strings are documentation -- the client
  31. // can query this information with a system.methodSignature and
  32. // system.methodHelp RPC.
  33. this->_signature = "i:ii"; // method's arguments are two integers
  34. this->_help = "This method adds two integers together";
  35. }
  36. void
  37. execute(xmlrpc_c::paramList const& paramList,
  38. xmlrpc_c::value * const retvalP) {
  39. int const addend(paramList.getInt(0));
  40. int const adder(paramList.getInt(1));
  41. paramList.verifyEnd(2);
  42. *retvalP = xmlrpc_c::value_int(addend + adder);
  43. }
  44. };
  45. int
  46. main(int const,
  47. const char ** const) {
  48. xmlrpc_c::registry myRegistry;
  49. xmlrpc_c::methodPtr const sampleAddMethodP(new sampleAddMethod);
  50. myRegistry.addMethod("sample.add", sampleAddMethodP);
  51. xmlrpc_c::serverAbyss myAbyssServer(
  52. xmlrpc_c::serverAbyss::constrOpt()
  53. .registryP(&myRegistry));
  54. myAbyssServer.runConn(STDIN_FILENO);
  55. /* This reads the HTTP POST request from Standard Input and
  56. executes the indicated RPC.
  57. */
  58. return 0;
  59. }