HelloServer.cpp
Go to the documentation of this file.
1 // HelloServer.cpp : Simple XMLRPC server example. Usage: HelloServer serverPort
2 //
3 #include "xmlrpcpp/XmlRpc.h"
4 
5 #include <iostream>
6 #include <stdlib.h>
7 
8 using namespace XmlRpc;
9 
10 // The server
12 
13 // No arguments, result is "Hello".
14 class Hello : public XmlRpcServerMethod
15 {
16 public:
17  Hello(XmlRpcServer* s) : XmlRpcServerMethod("Hello", s) {}
18 
19  void execute(XmlRpcValue& params, XmlRpcValue& result)
20  {
21  result = "Hello";
22  }
23 
24  std::string help() { return std::string("Say hello"); }
25 
26 } hello(&s); // This constructor registers the method with the server
27 
28 
29 // One argument is passed, result is "Hello, " + arg.
31 {
32 public:
33  HelloName(XmlRpcServer* s) : XmlRpcServerMethod("HelloName", s) {}
34 
35  void execute(XmlRpcValue& params, XmlRpcValue& result)
36  {
37  std::string resultString = "Hello, ";
38  resultString += std::string(params[0]);
39  result = resultString;
40  }
41 } helloName(&s);
42 
43 
44 // A variable number of arguments are passed, all doubles, result is their sum.
45 class Sum : public XmlRpcServerMethod
46 {
47 public:
48  Sum(XmlRpcServer* s) : XmlRpcServerMethod("Sum", s) {}
49 
50  void execute(XmlRpcValue& params, XmlRpcValue& result)
51  {
52  int nArgs = params.size();
53  double sum = 0.0;
54  for (int i=0; i<nArgs; ++i)
55  sum += double(params[i]);
56  result = sum;
57  }
58 } sum(&s);
59 
60 
61 int main(int argc, char* argv[])
62 {
63  if (argc != 2) {
64  std::cerr << "Usage: HelloServer serverPort\n";
65  return -1;
66  }
67  int port = atoi(argv[1]);
68 
70 
71  // Create the server socket on the specified port
72  s.bindAndListen(port);
73 
74  // Enable introspection
75  s.enableIntrospection(true);
76 
77  // Wait for requests indefinitely
78  s.work(-1.0);
79 
80  return 0;
81 }
82 
Definition: XmlRpc.h:39
HelloName(XmlRpcServer *s)
Definition: HelloServer.cpp:33
RPC method arguments and results are represented by Values.
Definition: XmlRpcValue.h:24
int size() const
Return the size for string, base64, array, and struct values.
void work(double msTime)
Process client requests for the specified time.
bool bindAndListen(int port, int backlog=5)
XmlRpcServer s
Definition: HelloServer.cpp:11
int main(int argc, char *argv[])
Definition: HelloServer.cpp:61
Sum(XmlRpcServer *s)
Definition: HelloServer.cpp:48
Abstract class representing a single RPC method.
Hello(XmlRpcServer *s)
Definition: HelloServer.cpp:17
void setVerbosity(int level)
Sets log message verbosity. This is short for XmlRpcLogHandler::setVerbosity(level) ...
Definition: XmlRpcUtil.cpp:76
void enableIntrospection(bool enabled=true)
Specify whether introspection is enabled or not. Default is not enabled.
void execute(XmlRpcValue &params, XmlRpcValue &result)
Execute the method. Subclasses must provide a definition for this method.
Definition: HelloServer.cpp:19
A class to handle XML RPC requests.
Definition: XmlRpcServer.h:37
void execute(XmlRpcValue &params, XmlRpcValue &result)
Execute the method. Subclasses must provide a definition for this method.
Definition: HelloServer.cpp:50
std::string help()
Definition: HelloServer.cpp:24
void execute(XmlRpcValue &params, XmlRpcValue &result)
Execute the method. Subclasses must provide a definition for this method.
Definition: HelloServer.cpp:35


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley
autogenerated on Sun Aug 26 2018 03:03:28