2
#ifndef _XMLRPCSERVER_H_
3
#define _XMLRPCSERVER_H_
5
// XmlRpc++ Copyright (c) 2002-2008 by Chris Morley
8
# pragma warning(disable:4786) // identifier was truncated in debug info
15
#include "XmlRpcDispatch.h"
16
#include "XmlRpcSource.h"
21
// An abstract class supporting XML RPC methods
22
class XmlRpcServerMethod;
24
// Class representing connections to specific clients
25
class XmlRpcServerConnection;
27
// Class representing argument and result values
31
//! A class to handle XML RPC requests
32
class XmlRpcServer : public XmlRpcSource {
34
//! Create a server object.
37
virtual ~XmlRpcServer();
39
//! Specify whether introspection is enabled or not. Default is not enabled.
40
void enableIntrospection(bool enabled=true);
42
//! Add a command to the RPC server
43
void addMethod(XmlRpcServerMethod* method);
45
//! Remove a command from the RPC server
46
void removeMethod(XmlRpcServerMethod* method);
48
//! Remove a command from the RPC server by name
49
void removeMethod(const std::string& methodName);
51
//! Look up a method by name
52
XmlRpcServerMethod* findMethod(const std::string& name) const;
54
//! Create a socket, bind to the specified port, and
55
//! set it in listen mode to make it available for clients.
56
//! @param port The port to bind and listen on (zero to choose an arbitrary port)
57
bool bindAndListen(int port, int backlog = 5);
59
//! Get the port number this server is listening on.
60
int getPort(void) const;
62
//! Process client requests for the specified time (in seconds)
63
void work(double timeSeconds);
65
//! Temporarily stop processing client requests and exit the work() method.
68
//! Close all connections with clients and the socket file descriptor
71
//! Introspection support
72
void listMethods(XmlRpcValue& result);
75
//! Parses the request xml, runs the method, generates the response (header+xml).
76
//! Returns a fault response if an error occurs during method execution.
77
virtual std::string executeRequest(std::string const& request);
80
// XmlRpcSource interface implementation
82
//! Handle client connection requests
83
virtual unsigned handleEvent(unsigned eventType);
85
//! Remove a connection from the dispatcher
86
virtual void removeConnection(XmlRpcServerConnection*);
91
static const char METHODNAME_TAG[];
92
static const char PARAMS_TAG[];
93
static const char PARAM_TAG[];
95
static const std::string SYSTEM_MULTICALL;
96
static const std::string METHODNAME;
97
static const std::string PARAMS;
99
static const std::string FAULTCODE;
100
static const std::string FAULTSTRING;
103
//! Accept a client connection request
104
virtual void acceptConnection();
106
//! Create a new connection object for processing requests from a specific client.
107
//! If the client is not authorized to connect, close the socket and return 0.
108
virtual XmlRpcServerConnection* createConnection(XmlRpcSocket::Socket socket);
110
//! Hand off a new connection object to a dispatcher.
111
virtual void dispatchConnection(XmlRpcServerConnection* sc);
114
//! Parse the methodName and parameters from the request.
115
//! @returns the methodName
116
std::string parseRequest(std::string const& request, XmlRpcValue& params);
118
//! Execute a named method with the specified params.
119
bool executeMethod(const std::string& methodName, XmlRpcValue& params, XmlRpcValue& result);
121
//! Execute multiple calls and return the results in an array.
122
//! System.multicall implementation
123
bool executeMulticall(const std::string& methodName, XmlRpcValue& params, XmlRpcValue& result);
125
//! Construct a response from the result XML.
126
std::string generateResponse(std::string const& resultXml);
128
//! Construct a fault response.
129
std::string generateFaultResponse(std::string const& msg, int errorCode = -1);
131
//! Return the appropriate headers for the response.
132
std::string generateHeader(std::string const& body);
136
//! Whether the introspection API is supported by this server
137
bool _introspectionEnabled;
140
XmlRpcDispatch _disp;
142
//! Collection of methods. This could be a set keyed on method name if we wanted...
143
typedef std::map< std::string, XmlRpcServerMethod* > MethodMap;
145
//! Registered RPC methods.
148
//! List all registered RPC methods (only available if introspection is enabled)
149
XmlRpcServerMethod* _listMethods;
151
//! Return help string for a specified method (only available if introspection is enabled)
152
XmlRpcServerMethod* _methodHelp;
155
} // namespace XmlRpc
157
#endif //_XMLRPCSERVER_H_