pstream_serial_server.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* A simple standalone RPC server based on an Xmlrpc-c packet socket.
  2. This program expects the invoker to provide a socket in listen mode
  3. as Standard Input.
  4. This is not an XML-RPC server, because it uses a simple packet socket
  5. instead of HTTP. See xmlrpc_sample_add_server.cpp for an example of
  6. an XML-RPC server.
  7. The advantage of this example over XML-RPC is that it has a connection
  8. concept. The client can be connected indefinitely and the server gets
  9. notified when the client terminates, even if it gets aborted by its OS.
  10. Here's an example of running this:
  11. $ socketexec -listen -local_port=8080 ./pstream_serial_server
  12. */
  13. #ifndef WIN32
  14. #include <unistd.h>
  15. #endif
  16. #include <cassert>
  17. #include <iostream>
  18. #include <signal.h>
  19. #include <xmlrpc-c/base.hpp>
  20. #include <xmlrpc-c/registry.hpp>
  21. #include <xmlrpc-c/server_pstream.hpp>
  22. using namespace std;
  23. class sampleAddMethod : public xmlrpc_c::method {
  24. public:
  25. sampleAddMethod() {
  26. // signature and help strings are documentation -- the client
  27. // can query this information with a system.methodSignature and
  28. // system.methodHelp RPC.
  29. this->_signature = "i:ii"; // method's arguments are two integers
  30. this->_help = "This method adds two integers together";
  31. }
  32. void
  33. execute(xmlrpc_c::paramList const& paramList,
  34. xmlrpc_c::value * const retvalP) {
  35. int const addend(paramList.getInt(0));
  36. int const adder(paramList.getInt(1));
  37. paramList.verifyEnd(2);
  38. *retvalP = xmlrpc_c::value_int(addend + adder);
  39. }
  40. };
  41. int
  42. main(int const,
  43. const char ** const) {
  44. // It's a good idea to disable SIGPIPE signals; if client closes his end
  45. // of the pipe/socket, we'd rather see a failure to send a response than
  46. // get killed by the OS.
  47. signal(SIGPIPE, SIG_IGN);
  48. try {
  49. xmlrpc_c::registry myRegistry;
  50. xmlrpc_c::methodPtr const sampleAddMethodP(new sampleAddMethod);
  51. myRegistry.addMethod("sample.add", sampleAddMethodP);
  52. xmlrpc_c::serverPstream server(
  53. xmlrpc_c::serverPstream::constrOpt()
  54. .socketFd(STDIN_FILENO)
  55. .registryP(&myRegistry));
  56. server.runSerial();
  57. } catch (exception const& e) {
  58. cerr << "Something threw an error: " << e.what() << endl;
  59. }
  60. return 0;
  61. }