From c5509b8b00c0e189d5e57eb757c9ac9b9a88c864 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 19 Dec 2011 08:02:16 -0800 Subject: [PATCH] Finish modifying this code to use the JsonCpp library. --- RPCServer.cpp | 79 +++++++++++++++++++++++++-------------------------- RPCServer.h | 11 ++++--- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/RPCServer.cpp b/RPCServer.cpp index d4a8c638f4..c215e870e4 100644 --- a/RPCServer.cpp +++ b/RPCServer.cpp @@ -1,16 +1,18 @@ +#include + +#include + +#include "json/value.h" +#include "json/reader.h" + #include "RPCServer.h" #include "RequestParser.h" #include "HttpReply.h" -#include -//#include #include "Application.h" -#include #include "RPC.h" #include "Conversion.h" -using namespace std; - /* Just read from wire until the entire request is in. */ @@ -23,7 +25,7 @@ RPCServer::RPCServer(boost::asio::io_service& io_service) void RPCServer::connected() { //BOOST_LOG_TRIVIAL(info) << "RPC request"; - cout << "RPC request" << endl; + std::cout << "RPC request" << std::endl; mSocket.async_read_some(boost::asio::buffer(mReadBuffer), boost::bind(&RPCServer::handle_read, shared_from_this(), @@ -44,17 +46,20 @@ void RPCServer::handle_read(const boost::system::error_code& e, { // mReplyStr=handleRequest(mIncomingRequest.mBody); sendReply(); - }else if(!result) + } + else if(!result) { // bad request - cout << "bad request" << endl; - }else + std::cout << "bad request" << std::endl; + } + else { // not done keep reading mSocket.async_read_some(boost::asio::buffer(mReadBuffer), boost::bind(&RPCServer::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } - }else if(e != boost::asio::error::operation_aborted) + } + else if(e != boost::asio::error::operation_aborted) { } @@ -62,50 +67,43 @@ void RPCServer::handle_read(const boost::system::error_code& e, -#if 0 -std::string RPCServer::handleRequest(std::string& requestStr) +std::string RPCServer::handleRequest(const std::string& requestStr) { - cout << "handleRequest " << requestStr << endl; - Value id = json_spirit::Value::null; + std::cout << "handleRequest " << requestStr << std::endl; + Json::Value id; // Parse request - Value valRequest; - if(!read_string(requestStr, valRequest) || valRequest.type() != obj_type) + Json::Value valRequest; + Json::Reader reader; + if(!reader.parse(requestStr, valRequest) || valRequest.isNull() || !valRequest.isObject()) return(HTTPReply(400, "")); - const Object& request = valRequest.get_obj(); - // Parse id now so errors from here on will have the id - id = find_value(request, "id"); + id = valRequest["id"]; // Parse method - Value valMethod = find_value(request, "method"); - if (valMethod.type() == null_type) + Json::Value valMethod = valRequest["method"]; + if (valMethod.isNull()) return(HTTPReply(400, "")); - if (valMethod.type() != str_type) + if (!valMethod.isString()) return(HTTPReply(400, "")); - string strMethod = valMethod.get_str(); + std::string strMethod = valMethod.asString(); // Parse params - Value valParams = find_value(request, "params"); - Array params; - if (valParams.type() == array_type) - params = valParams.get_array(); - else if (valParams.type() == null_type) - params = Array(); - else + Json::Value valParams = valRequest["params"]; + if (valParams.isNull()) + valParams = Json::Value(Json::arrayValue); + else if(!valParams.isArray()) return(HTTPReply(400, "")); - - - Value result=doCommand(strMethod,params); - string strReply = JSONRPCReply(result, Value::null, id); + Json::Value result=doCommand(strMethod, valParams); + std::string strReply = JSONRPCReply(result, Json::Value(), id); return( HTTPReply(200, strReply) ); } -Value RPCServer::doCommand(std::string& command, Array& params) +Json::Value RPCServer::doCommand(const std::string& command, Json::Value& params) { if(command== "stop") { @@ -121,23 +119,22 @@ Value RPCServer::doCommand(std::string& command, Array& params) { if(params.size()==2) { - uint160 hanko=humanTo160(params[0].get_str()); - vector pubKey; - humanToPK(params[1].get_str(),pubKey); + uint160 hanko=humanTo160(params[0u].asString()); + std::vector pubKey; + humanToPK(params[1u].asString(),pubKey); theApp->getUNL().addNode(hanko,pubKey); return "adding node"; }else return "invalid params"; } if(command=="getUNL") { - string str; + std::string str; theApp->getUNL().dumpUNL(str); return(str.c_str()); } return "unknown command"; } -#endif void RPCServer::sendReply() { @@ -152,4 +149,4 @@ void RPCServer::sendReply() void RPCServer::handle_write(const boost::system::error_code& /*error*/) { -} \ No newline at end of file +} diff --git a/RPCServer.h b/RPCServer.h index 2ba57a2fc0..54ab5985a7 100644 --- a/RPCServer.h +++ b/RPCServer.h @@ -1,10 +1,13 @@ -#include "HttpRequest.h" -#include "RequestParser.h" #include #include #include #include +#include "json/value.h" + +#include "HttpRequest.h" +#include "RequestParser.h" + class RPCServer : public boost::enable_shared_from_this { boost::asio::ip::tcp::socket mSocket; @@ -21,10 +24,10 @@ class RPCServer : public boost::enable_shared_from_this void handle_read(const boost::system::error_code& e, std::size_t bytes_transferred); - std::string handleRequest(std::string& requestStr); + std::string handleRequest(const std::string& requestStr); void sendReply(); -// json_spirit::Value doCommand(std::string& command,json_spirit::Array& params); + Json::Value doCommand(const std::string& command, Json::Value& params); public: typedef boost::shared_ptr pointer;