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 "RequestParser.h"
#include "HttpReply.h"
#include <boost/bind.hpp>
//#include <boost/log/trivial.hpp>
#include "Application.h"
#include <iostream>
#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<unsigned char> pubKey;
humanToPK(params[1].get_str(),pubKey);
uint160 hanko=humanTo160(params[0u].asString());
std::vector<unsigned char> 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*/)
{
}
}

View File

@@ -1,10 +1,13 @@
#include "HttpRequest.h"
#include "RequestParser.h"
#include <boost/array.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include "json/value.h"
#include "HttpRequest.h"
#include "RequestParser.h"
class RPCServer : public boost::enable_shared_from_this<RPCServer>
{
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);
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<RPCServer> pointer;