1 2 #ifndef _XMLRPCSERVER_H_ 3 #define _XMLRPCSERVER_H_ 4 // 5 // XmlRpc++ Copyright (c) 2002-2003 by Chris Morley 6 // 7 #if defined(_MSC_VER) 8 # pragma warning(disable:4786) // identifier was truncated in debug info 9 #endif 10 11 #ifndef MAKEDEPEND 12 # include <map> 13 # include <string> 14 #endif 15 16 #include "XmlRpcDispatch.h" 17 #include "XmlRpcSource.h" 18 19 namespace XmlRpc { 20 21 22 // An abstract class supporting XML RPC methods 23 class XmlRpcServerMethod; 24 25 // Class representing connections to specific clients 26 class XmlRpcServerConnection; 27 28 // Class representing argument and result values 29 class XmlRpcValue; 30 31 32 //! A class to handle XML RPC requests 33 class XmlRpcServer : public XmlRpcSource { 34 public: 35 //! Create a server object. 36 XmlRpcServer(); 37 //! Destructor. 38 virtual ~XmlRpcServer(); 39 40 //! Specify whether introspection is enabled or not. Default is not enabled. 41 void enableIntrospection(bool enabled=true); 42 43 //! Add a command to the RPC server 44 void addMethod(XmlRpcServerMethod* method); 45 46 //! Remove a command from the RPC server 47 void removeMethod(XmlRpcServerMethod* method); 48 49 //! Remove a command from the RPC server by name 50 void removeMethod(const std::string& methodName); 51 52 //! Look up a method by name 53 XmlRpcServerMethod* findMethod(const std::string& name) const; 54 55 //! Create a socket, bind to the specified port, and 56 //! set it in listen mode to make it available for clients. 57 //! @param port The port to bind and listen on (zero to choose an arbitrary port) 58 //! @param bind_ip The IP to bind and listen on ("" to listen on ANY interface) 59 bool bindAndListen(int port, const std::string& bind_ip, int backlog = 5); 60 61 //! Get the port number this server is listening on. 62 int getPort(void) const; 63 64 //! Process client requests for the specified time 65 void work(double msTime); 66 67 //! Temporarily stop processing client requests and exit the work() method. 68 void exit(); 69 70 //! Close all connections with clients and the socket file descriptor 71 void shutdown(); 72 73 //! Introspection support 74 void listMethods(XmlRpcValue& result); 75 76 77 //! Parses the request xml, runs the method, generates the response (header+xml). 78 //! Returns a fault response if an error occurs during method execution. 79 virtual std::string executeRequest(std::string const& request); 80 81 82 // XmlRpcSource interface implementation 83 84 //! Handle client connection requests 85 virtual unsigned handleEvent(unsigned eventType); 86 87 //! Remove a connection from the dispatcher 88 virtual void removeConnection(XmlRpcServerConnection*); 89 90 protected: 91 92 // Static data 93 static const char METHODNAME_TAG[]; 94 static const char PARAMS_TAG[]; 95 static const char PARAMS_ETAG[]; 96 static const char PARAM_TAG[]; 97 static const char PARAM_ETAG[]; 98 99 static const std::string SYSTEM_MULTICALL; 100 static const std::string METHODNAME; 101 static const std::string PARAMS; 102 103 static const std::string FAULTCODE; 104 static const std::string FAULTSTRING; 105 106 107 //! Accept a client connection request 108 virtual void acceptConnection(); 109 110 //! Create a new connection object for processing requests from a specific client. 111 //! If the client is not authorized to connect, close the socket and return 0. 112 virtual XmlRpcServerConnection* createConnection(int socket); 113 114 //! Hand off a new connection object to a dispatcher. 115 virtual void dispatchConnection(XmlRpcServerConnection* sc); 116 117 118 //! Parse the methodName and parameters from the request. 119 //! @returns the methodName 120 std::string parseRequest(std::string const& request, XmlRpcValue& params); 121 122 //! Execute a named method with the specified params. 123 bool executeMethod(const std::string& methodName, XmlRpcValue& params, XmlRpcValue& result); 124 125 //! Execute multiple calls and return the results in an array. 126 //! System.multicall implementation 127 bool executeMulticall(const std::string& methodName, XmlRpcValue& params, XmlRpcValue& result); 128 129 //! Construct a response from the result XML. 130 std::string generateResponse(std::string const& resultXml); 131 132 //! Construct a fault response. 133 std::string generateFaultResponse(std::string const& msg, int errorCode = -1); 134 135 //! Return the appropriate headers for the response. 136 std::string generateHeader(std::string const& body); 137 138 139 140 //! Whether the introspection API is supported by this server 141 bool _introspectionEnabled; 142 143 //! Event dispatcher 144 XmlRpcDispatch _disp; 145 146 //! Collection of methods. This could be a set keyed on method name if we wanted... 147 typedef std::map< std::string, XmlRpcServerMethod* > MethodMap; 148 149 //! Registered RPC methods. 150 MethodMap _methods; 151 152 //! List all registered RPC methods (only available if introspection is enabled) 153 XmlRpcServerMethod* _listMethods; 154 155 //! Return help string for a specified method (only available if introspection is enabled) 156 XmlRpcServerMethod* _methodHelp; 157 158 }; 159 } // namespace XmlRpc 160 161 #endif //_XMLRPCSERVER_H_ 162