1/*************************************************************************
2 * libjson-rpc-cpp
3 *************************************************************************
4 * @file rpcprotocolserver12.cpp
5 * @date 10/25/2014
6 * @author Peter Spiess-Knafl <dev@spiessknafl.at>
7 * @license See attached LICENSE.txt
8 ************************************************************************/
9
10#include "rpcprotocolserver12.h"
11#include <jsonrpccpp/common/jsonparser.h>
12
13using namespace jsonrpc;
14using namespace std;
15
16RpcProtocolServer12::RpcProtocolServer12(IProcedureInvokationHandler &handler) : rpc1(handler), rpc2(handler) {}
17
18void RpcProtocolServer12::AddProcedure(const Procedure &procedure) {
19 this->rpc1.AddProcedure(procedure);
20 this->rpc2.AddProcedure(procedure);
21}
22
23void RpcProtocolServer12::HandleRequest(const std::string &request, std::string &retValue) {
24 Json::Value req;
25 Json::Value resp;
26 Json::StreamWriterBuilder wbuilder;
27 wbuilder["indentation"] = "";
28
29 try {
30 istringstream(request) >> req;
31 this->GetHandler(req).HandleJsonRequest(req, resp);
32 } catch (const Json::Exception &e) {
33 this->GetHandler(req).WrapError(Json::nullValue, Errors::ERROR_RPC_JSON_PARSE_ERROR, Errors::GetErrorMessage(Errors::ERROR_RPC_JSON_PARSE_ERROR), resp);
34 }
35
36 if (resp != Json::nullValue)
37 retValue = Json::writeString(wbuilder, resp);
38}
39
40AbstractProtocolHandler &RpcProtocolServer12::GetHandler(const Json::Value &request) {
41 if (request.isArray() || (request.isObject() && request.isMember("jsonrpc") && request["jsonrpc"].asString() == "2.0"))
42 return rpc2;
43 return rpc1;
44}
45