Finish modifying this code to use the JsonCpp library.

This commit is contained in:
JoelKatz
2011-12-19 08:02:16 -08:00
parent 0421268daa
commit c5509b8b00
2 changed files with 45 additions and 45 deletions

View File

@@ -1,16 +1,18 @@
#include <iostream>
#include <boost/bind.hpp>
#include "json/value.h"
#include "json/reader.h"
#include "RPCServer.h" #include "RPCServer.h"
#include "RequestParser.h" #include "RequestParser.h"
#include "HttpReply.h" #include "HttpReply.h"
#include <boost/bind.hpp>
//#include <boost/log/trivial.hpp>
#include "Application.h" #include "Application.h"
#include <iostream>
#include "RPC.h" #include "RPC.h"
#include "Conversion.h" #include "Conversion.h"
using namespace std;
/* /*
Just read from wire until the entire request is in. 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() void RPCServer::connected()
{ {
//BOOST_LOG_TRIVIAL(info) << "RPC request"; //BOOST_LOG_TRIVIAL(info) << "RPC request";
cout << "RPC request" << endl; std::cout << "RPC request" << std::endl;
mSocket.async_read_some(boost::asio::buffer(mReadBuffer), mSocket.async_read_some(boost::asio::buffer(mReadBuffer),
boost::bind(&RPCServer::handle_read, shared_from_this(), 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); // mReplyStr=handleRequest(mIncomingRequest.mBody);
sendReply(); sendReply();
}else if(!result) }
else if(!result)
{ // bad request { // bad request
cout << "bad request" << endl; std::cout << "bad request" << std::endl;
}else }
else
{ // not done keep reading { // not done keep reading
mSocket.async_read_some(boost::asio::buffer(mReadBuffer), mSocket.async_read_some(boost::asio::buffer(mReadBuffer),
boost::bind(&RPCServer::handle_read, shared_from_this(), boost::bind(&RPCServer::handle_read, shared_from_this(),
boost::asio::placeholders::error, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)); 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(const std::string& requestStr)
std::string RPCServer::handleRequest(std::string& requestStr)
{ {
cout << "handleRequest " << requestStr << endl; std::cout << "handleRequest " << requestStr << std::endl;
Value id = json_spirit::Value::null; Json::Value id;
// Parse request // Parse request
Value valRequest; Json::Value valRequest;
if(!read_string(requestStr, valRequest) || valRequest.type() != obj_type) Json::Reader reader;
if(!reader.parse(requestStr, valRequest) || valRequest.isNull() || !valRequest.isObject())
return(HTTPReply(400, "")); return(HTTPReply(400, ""));
const Object& request = valRequest.get_obj();
// Parse id now so errors from here on will have the id // Parse id now so errors from here on will have the id
id = find_value(request, "id"); id = valRequest["id"];
// Parse method // Parse method
Value valMethod = find_value(request, "method"); Json::Value valMethod = valRequest["method"];
if (valMethod.type() == null_type) if (valMethod.isNull())
return(HTTPReply(400, "")); return(HTTPReply(400, ""));
if (valMethod.type() != str_type) if (!valMethod.isString())
return(HTTPReply(400, "")); return(HTTPReply(400, ""));
string strMethod = valMethod.get_str(); std::string strMethod = valMethod.asString();
// Parse params // Parse params
Value valParams = find_value(request, "params"); Json::Value valParams = valRequest["params"];
Array params; if (valParams.isNull())
if (valParams.type() == array_type) valParams = Json::Value(Json::arrayValue);
params = valParams.get_array(); else if(!valParams.isArray())
else if (valParams.type() == null_type)
params = Array();
else
return(HTTPReply(400, "")); return(HTTPReply(400, ""));
Json::Value result=doCommand(strMethod, valParams);
std::string strReply = JSONRPCReply(result, Json::Value(), id);
Value result=doCommand(strMethod,params);
string strReply = JSONRPCReply(result, Value::null, id);
return( HTTPReply(200, strReply) ); 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") if(command== "stop")
{ {
@@ -121,23 +119,22 @@ Value RPCServer::doCommand(std::string& command, Array& params)
{ {
if(params.size()==2) if(params.size()==2)
{ {
uint160 hanko=humanTo160(params[0].get_str()); uint160 hanko=humanTo160(params[0u].asString());
vector<unsigned char> pubKey; std::vector<unsigned char> pubKey;
humanToPK(params[1].get_str(),pubKey); humanToPK(params[1u].asString(),pubKey);
theApp->getUNL().addNode(hanko,pubKey); theApp->getUNL().addNode(hanko,pubKey);
return "adding node"; return "adding node";
}else return "invalid params"; }else return "invalid params";
} }
if(command=="getUNL") if(command=="getUNL")
{ {
string str; std::string str;
theApp->getUNL().dumpUNL(str); theApp->getUNL().dumpUNL(str);
return(str.c_str()); return(str.c_str());
} }
return "unknown command"; return "unknown command";
} }
#endif
void RPCServer::sendReply() void RPCServer::sendReply()
{ {
@@ -152,4 +149,4 @@ void RPCServer::sendReply()
void RPCServer::handle_write(const boost::system::error_code& /*error*/) void RPCServer::handle_write(const boost::system::error_code& /*error*/)
{ {
} }

View File

@@ -1,10 +1,13 @@
#include "HttpRequest.h"
#include "RequestParser.h"
#include <boost/array.hpp> #include <boost/array.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp> #include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "json/value.h"
#include "HttpRequest.h"
#include "RequestParser.h"
class RPCServer : public boost::enable_shared_from_this<RPCServer> class RPCServer : public boost::enable_shared_from_this<RPCServer>
{ {
boost::asio::ip::tcp::socket mSocket; boost::asio::ip::tcp::socket mSocket;
@@ -21,10 +24,10 @@ class RPCServer : public boost::enable_shared_from_this<RPCServer>
void handle_read(const boost::system::error_code& e, std::size_t bytes_transferred); 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(); void sendReply();
// json_spirit::Value doCommand(std::string& command,json_spirit::Array& params); Json::Value doCommand(const std::string& command, Json::Value& params);
public: public:
typedef boost::shared_ptr<RPCServer> pointer; typedef boost::shared_ptr<RPCServer> pointer;