initial load of upstream version 1.06.32
[xmlrpc-c] / examples / cpp / xmlrpc_loop_server.cpp
1 /* A simple standalone XML-RPC server based on Abyss that contains a
2    simple one-thread request processing loop.  
3
4    xmlrpc_sample_add_server.cpp is a server that does the same thing, but
5    does it by running a full Abyss daemon in the background, so it has
6    less control over how the requests are served.
7 */
8
9 #include <cassert>
10 #include <iostream>
11
12 #include <xmlrpc-c/base.hpp>
13 #include <xmlrpc-c/registry.hpp>
14 #include <xmlrpc-c/server_abyss.hpp>
15
16 using namespace std;
17
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";  // method's arguments are two integers
25         this->_help = "This method adds two integers together";
26     }
27     void
28     execute(xmlrpc_c::paramList const& paramList,
29             xmlrpc_c::value *   const  retvalP) {
30         
31         int const addend(paramList.getInt(0));
32         int const adder(paramList.getInt(1));
33         
34         paramList.verifyEnd(2);
35         
36         *retvalP = xmlrpc_c::value_int(addend + adder);
37     }
38 };
39
40
41
42 int 
43 main(int           const, 
44      const char ** const) {
45
46     xmlrpc_c::registry myRegistry;
47
48     xmlrpc_c::methodPtr const sampleAddMethodP(new sampleAddMethod);
49
50     myRegistry.addMethod("sample.add", sampleAddMethodP);
51
52     xmlrpc_c::serverAbyss myAbyssServer(
53         myRegistry,
54         8080,              // TCP port on which to listen
55         "/tmp/xmlrpc_log"  // Log file
56         );
57
58     while (true) {
59         cout << "Waiting for next RPC..." << endl;
60
61         myAbyssServer.runOnce();
62             /* This waits for the next connection, accepts it, reads the
63                HTTP POST request, executes the indicated RPC, and closes
64                the connection.
65             */
66     }
67     return 0;
68 }