00001 // HelloServer.cpp : Simple XMLRPC server example. Usage: HelloServer serverPort 00002 // 00003 #include "XmlRpc.h" 00004 00005 #include <iostream> 00006 #include <stdlib.h> 00007 00008 using namespace XmlRpc; 00009 00010 // The server 00011 XmlRpcServer s; 00012 00013 // No arguments, result is "Hello". 00014 class Hello : public XmlRpcServerMethod 00015 { 00016 public: 00017 Hello(XmlRpcServer* s) : XmlRpcServerMethod("Hello", s) {} 00018 00019 void execute(XmlRpcValue& params, XmlRpcValue& result) 00020 { 00021 result = "Hello"; 00022 } 00023 00024 std::string help() { return std::string("Say hello"); } 00025 00026 } hello(&s); // This constructor registers the method with the server 00027 00028 00029 // One argument is passed, result is "Hello, " + arg. 00030 class HelloName : public XmlRpcServerMethod 00031 { 00032 public: 00033 HelloName(XmlRpcServer* s) : XmlRpcServerMethod("HelloName", s) {} 00034 00035 void execute(XmlRpcValue& params, XmlRpcValue& result) 00036 { 00037 std::string resultString = "Hello, "; 00038 resultString += std::string(params[0]); 00039 result = resultString; 00040 } 00041 } helloName(&s); 00042 00043 00044 // A variable number of arguments are passed, all doubles, result is their sum. 00045 class Sum : public XmlRpcServerMethod 00046 { 00047 public: 00048 Sum(XmlRpcServer* s) : XmlRpcServerMethod("Sum", s) {} 00049 00050 void execute(XmlRpcValue& params, XmlRpcValue& result) 00051 { 00052 int nArgs = params.size(); 00053 double sum = 0.0; 00054 for (int i=0; i<nArgs; ++i) 00055 sum += double(params[i]); 00056 result = sum; 00057 } 00058 } sum(&s); 00059 00060 00061 int main(int argc, char* argv[]) 00062 { 00063 if (argc != 2) { 00064 std::cerr << "Usage: HelloServer serverPort\n"; 00065 return -1; 00066 } 00067 int port = atoi(argv[1]); 00068 00069 XmlRpc::setVerbosity(5); 00070 00071 // Create the server socket on the specified port 00072 s.bindAndListen(port); 00073 00074 // Enable introspection 00075 s.enableIntrospection(true); 00076 00077 // Wait for requests indefinitely 00078 s.work(-1.0); 00079 00080 return 0; 00081 } 00082