1 | /************************************************************************* |
2 | * libjson-rpc-cpp |
3 | ************************************************************************* |
4 | * @file abstractprotocolhandler.h |
5 | * @date 10/23/2014 |
6 | * @author Peter Spiess-Knafl <dev@spiessknafl.at> |
7 | * @license See attached LICENSE.txt |
8 | ************************************************************************/ |
9 | |
10 | #ifndef JSONRPC_CPP_ABSTRACTPROTOCOLHANDLER_H |
11 | #define JSONRPC_CPP_ABSTRACTPROTOCOLHANDLER_H |
12 | |
13 | #include "iclientconnectionhandler.h" |
14 | #include "iprocedureinvokationhandler.h" |
15 | #include <jsonrpccpp/common/procedure.h> |
16 | #include <map> |
17 | #include <string> |
18 | |
19 | #define KEY_REQUEST_METHODNAME "method" |
20 | #define KEY_REQUEST_ID "id" |
21 | #define KEY_REQUEST_PARAMETERS "params" |
22 | #define KEY_RESPONSE_ERROR "error" |
23 | #define KEY_RESPONSE_RESULT "result" |
24 | |
25 | namespace jsonrpc { |
26 | |
27 | class AbstractProtocolHandler : public IProtocolHandler { |
28 | public: |
29 | AbstractProtocolHandler(IProcedureInvokationHandler &handler); |
30 | virtual ~AbstractProtocolHandler(); |
31 | |
32 | void HandleRequest(const std::string &request, std::string &retValue); |
33 | |
34 | virtual void AddProcedure(const Procedure &procedure); |
35 | |
36 | virtual void HandleJsonRequest(const Json::Value &request, Json::Value &response) = 0; |
37 | virtual bool ValidateRequestFields(const Json::Value &val) = 0; |
38 | virtual void WrapResult(const Json::Value &request, Json::Value &response, Json::Value &retValue) = 0; |
39 | virtual void WrapError(const Json::Value &request, int code, const std::string &message, Json::Value &result) = 0; |
40 | virtual procedure_t GetRequestType(const Json::Value &request) = 0; |
41 | |
42 | protected: |
43 | IProcedureInvokationHandler &handler; |
44 | std::map<std::string, Procedure> procedures; |
45 | |
46 | void ProcessRequest(const Json::Value &request, Json::Value &retValue); |
47 | int ValidateRequest(const Json::Value &val); |
48 | }; |
49 | |
50 | } // namespace jsonrpc |
51 | |
52 | #endif // JSONRPC_CPP_ABSTRACTPROTOCOLHANDLER_H |
53 | |