1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
// HelloServer.cpp : Simple XMLRPC server example. Usage: HelloServer serverPort
//
#include "XmlRpc.h"
#include <iostream>
#include <stdlib.h>
using namespace XmlRpc;
// The server
XmlRpcServer s;
// No arguments, result is "Hello".
class Hello : public XmlRpcServerMethod
{
public:
Hello(XmlRpcServer* s) : XmlRpcServerMethod("Hello", s) {}
void execute(XmlRpcValue& params, XmlRpcValue& result)
{
result = "Hello";
}
std::string help() { return std::string("Say hello"); }
} hello(&s); // This constructor registers the method with the server
// One argument is passed, result is "Hello, " + arg.
class HelloName : public XmlRpcServerMethod
{
public:
HelloName(XmlRpcServer* s) : XmlRpcServerMethod("HelloName", s) {}
void execute(XmlRpcValue& params, XmlRpcValue& result)
{
std::string resultString = "Hello, ";
resultString += std::string(params[0]);
result = resultString;
}
} helloName(&s);
// A variable number of arguments are passed, all doubles, result is their sum.
class Sum : public XmlRpcServerMethod
{
public:
Sum(XmlRpcServer* s) : XmlRpcServerMethod("Sum", s) {}
void execute(XmlRpcValue& params, XmlRpcValue& result)
{
int nArgs = params.size();
double sum = 0.0;
for (int i=0; i<nArgs; ++i)
sum += double(params[i]);
result = sum;
}
} sum(&s);
int main(int argc, char* argv[])
{
if (argc != 2) {
std::cerr << "Usage: HelloServer serverPort\n";
return -1;
}
int port = atoi(argv[1]);
XmlRpc::setVerbosity(5);
// Create the server socket on the specified port
s.bindAndListen(port);
// Enable introspection
s.enableIntrospection(true);
// Wait for requests indefinitely
s.work(-1.0);
return 0;
}
|