Roshan Pius | 4e2ea81 | 2015-09-23 14:46:15 -0700 | [diff] [blame] | 1 | |
| 2 | #ifndef _XMLRPCCLIENT_H_ |
| 3 | #define _XMLRPCCLIENT_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 | |
| 12 | #ifndef MAKEDEPEND |
| 13 | # include <string> |
| 14 | #endif |
| 15 | |
| 16 | #include "XmlRpcDispatch.h" |
| 17 | #include "XmlRpcSource.h" |
| 18 | |
| 19 | namespace XmlRpc { |
| 20 | |
| 21 | // Arguments and results are represented by XmlRpcValues |
| 22 | class XmlRpcValue; |
| 23 | |
| 24 | //! A class to send XML RPC requests to a server and return the results. |
| 25 | class XmlRpcClient : public XmlRpcSource { |
| 26 | public: |
| 27 | // Static data |
| 28 | static const char REQUEST_BEGIN[]; |
| 29 | static const char REQUEST_END_METHODNAME[]; |
| 30 | static const char PARAMS_TAG[]; |
| 31 | static const char PARAMS_ETAG[]; |
| 32 | static const char PARAM_TAG[]; |
| 33 | static const char PARAM_ETAG[]; |
| 34 | static const char REQUEST_END[]; |
| 35 | // Result tags |
| 36 | static const char METHODRESPONSE_TAG[]; |
| 37 | static const char FAULT_TAG[]; |
| 38 | |
| 39 | //! Construct a client to connect to the server at the specified host:port address |
| 40 | //! @param host The name of the remote machine hosting the server |
| 41 | //! @param port The port on the remote machine where the server is listening |
| 42 | //! @param uri An optional string to be sent as the URI in the HTTP GET header |
| 43 | XmlRpcClient(const char* host, int port, const char* uri=0); |
| 44 | |
| 45 | //! Destructor |
| 46 | virtual ~XmlRpcClient(); |
| 47 | |
| 48 | //! Execute the named procedure on the remote server. |
| 49 | //! @param method The name of the remote procedure to execute |
| 50 | //! @param params An array of the arguments for the method |
| 51 | //! @param result The result value to be returned to the client |
| 52 | //! @return true if the request was sent and a result received |
| 53 | //! (although the result might be a fault). |
| 54 | //! |
| 55 | //! Currently this is a synchronous (blocking) implementation (execute |
| 56 | //! does not return until it receives a response or an error). Use isFault() |
| 57 | //! to determine whether the result is a fault response. |
| 58 | bool execute(const char* method, XmlRpcValue const& params, XmlRpcValue& result); |
| 59 | |
| 60 | //! Returns true if the result of the last execute() was a fault response. |
| 61 | bool isFault() const { return _isFault; } |
| 62 | |
| 63 | |
| 64 | // XmlRpcSource interface implementation |
| 65 | //! Close the connection |
| 66 | virtual void close(); |
| 67 | |
| 68 | //! Handle server responses. Called by the event dispatcher during execute. |
| 69 | //! @param eventType The type of event that occurred. |
| 70 | //! @see XmlRpcDispatch::EventType |
| 71 | virtual unsigned handleEvent(unsigned eventType); |
| 72 | |
| 73 | protected: |
| 74 | // Execution processing helpers |
| 75 | virtual bool doConnect(); |
| 76 | virtual bool setupConnection(); |
| 77 | |
| 78 | virtual bool generateRequest(const char* method, XmlRpcValue const& params); |
| 79 | virtual std::string generateHeader(std::string const& body); |
| 80 | virtual bool writeRequest(); |
| 81 | virtual bool readHeader(); |
| 82 | virtual bool readResponse(); |
| 83 | virtual bool parseResponse(XmlRpcValue& result); |
| 84 | |
| 85 | // Possible IO states for the connection |
| 86 | enum ClientConnectionState { NO_CONNECTION, CONNECTING, WRITE_REQUEST, READ_HEADER, READ_RESPONSE, IDLE }; |
| 87 | ClientConnectionState _connectionState; |
| 88 | |
| 89 | // Server location |
| 90 | std::string _host; |
| 91 | std::string _uri; |
| 92 | int _port; |
| 93 | |
| 94 | // The xml-encoded request, http header of response, and response xml |
| 95 | std::string _request; |
| 96 | std::string _header; |
| 97 | std::string _response; |
| 98 | |
| 99 | // Number of times the client has attempted to send the request |
| 100 | int _sendAttempts; |
| 101 | |
| 102 | // Number of bytes of the request that have been written to the socket so far |
| 103 | int _bytesWritten; |
| 104 | |
| 105 | // True if we are currently executing a request. If you want to multithread, |
| 106 | // each thread should have its own client. |
| 107 | bool _executing; |
| 108 | |
| 109 | // True if the server closed the connection |
| 110 | bool _eof; |
| 111 |
|
| 112 | // True if a fault response was returned by the server
|
| 113 | bool _isFault;
|
| 114 | |
| 115 | // Number of bytes expected in the response body (parsed from response header) |
| 116 | int _contentLength; |
| 117 | |
| 118 | // Event dispatcher |
| 119 | XmlRpcDispatch _disp; |
| 120 | |
| 121 | }; // class XmlRpcClient |
| 122 | |
| 123 | } // namespace XmlRpc |
| 124 | |
| 125 | #endif // _XMLRPCCLIENT_H_ |