mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-26 14:05:51 +00:00
Instantiate an RPCHandler per request.
RPCHandlers are pretty light objects and this allows us to pass in parameters like the InfoSub object without locking or adding more parameters to the RPC methods.
This commit is contained in:
@@ -93,6 +93,13 @@ Json::Value RPCHandler::rpcError(int iError)
|
|||||||
RPCHandler::RPCHandler(NetworkOPs* netOps)
|
RPCHandler::RPCHandler(NetworkOPs* netOps)
|
||||||
{
|
{
|
||||||
mNetOps=netOps;
|
mNetOps=netOps;
|
||||||
|
mInfoSub=NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
RPCHandler::RPCHandler(NetworkOPs* netOps, InfoSub* infoSub)
|
||||||
|
{
|
||||||
|
mNetOps=netOps;
|
||||||
|
mInfoSub=infoSub;
|
||||||
}
|
}
|
||||||
|
|
||||||
int RPCHandler::getParamCount(const Json::Value& params)
|
int RPCHandler::getParamCount(const Json::Value& params)
|
||||||
@@ -1299,7 +1306,7 @@ Json::Value RPCHandler::doLogRotate(const Json::Value& params)
|
|||||||
return Log::rotateLog();
|
return Log::rotateLog();
|
||||||
}
|
}
|
||||||
|
|
||||||
Json::Value RPCHandler::doCommand(const std::string& command, Json::Value& params, int role, InfoSub* sub)
|
Json::Value RPCHandler::doCommand(const std::string& command, Json::Value& params, int role)
|
||||||
{
|
{
|
||||||
cLog(lsTRACE) << "RPC:" << command;
|
cLog(lsTRACE) << "RPC:" << command;
|
||||||
cLog(lsTRACE) << "RPC params:" << params;
|
cLog(lsTRACE) << "RPC params:" << params;
|
||||||
@@ -1376,7 +1383,7 @@ Json::Value RPCHandler::doCommand(const std::string& command, Json::Value& param
|
|||||||
{
|
{
|
||||||
return rpcError(rpcNO_PERMISSION);
|
return rpcError(rpcNO_PERMISSION);
|
||||||
}
|
}
|
||||||
else if (commandsA[i].mEvented && sub == NULL)
|
else if (commandsA[i].mEvented && mInfoSub == NULL)
|
||||||
{
|
{
|
||||||
return rpcError(rpcNO_EVENTS);
|
return rpcError(rpcNO_EVENTS);
|
||||||
}
|
}
|
||||||
@@ -1406,11 +1413,6 @@ Json::Value RPCHandler::doCommand(const std::string& command, Json::Value& param
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (sub != NULL)
|
|
||||||
{
|
|
||||||
isCurrent = sub;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return (this->*(commandsA[i].dfpFunc))(params);
|
return (this->*(commandsA[i].dfpFunc))(params);
|
||||||
}
|
}
|
||||||
@@ -1993,16 +1995,16 @@ Json::Value RPCHandler::doSubscribe(const Json::Value& jvRequest)
|
|||||||
|
|
||||||
if(streamName=="server")
|
if(streamName=="server")
|
||||||
{
|
{
|
||||||
mNetOps->subServer(isCurrent, jvResult);
|
mNetOps->subServer(mInfoSub, jvResult);
|
||||||
}else if(streamName=="ledger")
|
}else if(streamName=="ledger")
|
||||||
{
|
{
|
||||||
mNetOps->subLedger(isCurrent, jvResult);
|
mNetOps->subLedger(mInfoSub, jvResult);
|
||||||
}else if(streamName=="transactions")
|
}else if(streamName=="transactions")
|
||||||
{
|
{
|
||||||
mNetOps->subTransactions(isCurrent);
|
mNetOps->subTransactions(mInfoSub);
|
||||||
}else if(streamName=="rt_transactions")
|
}else if(streamName=="rt_transactions")
|
||||||
{
|
{
|
||||||
mNetOps->subRTTransactions(isCurrent);
|
mNetOps->subRTTransactions(mInfoSub);
|
||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
jvResult["error"] = str(boost::format("Unknown stream: %s") % streamName);
|
jvResult["error"] = str(boost::format("Unknown stream: %s") % streamName);
|
||||||
@@ -2025,10 +2027,10 @@ Json::Value RPCHandler::doSubscribe(const Json::Value& jvRequest)
|
|||||||
{
|
{
|
||||||
BOOST_FOREACH(const RippleAddress& naAccountID, usnaAccoundIds)
|
BOOST_FOREACH(const RippleAddress& naAccountID, usnaAccoundIds)
|
||||||
{
|
{
|
||||||
isCurrent->insertSubAccountInfo(naAccountID);
|
mInfoSub->insertSubAccountInfo(naAccountID);
|
||||||
}
|
}
|
||||||
|
|
||||||
mNetOps->subAccount(isCurrent, usnaAccoundIds, true);
|
mNetOps->subAccount(mInfoSub, usnaAccoundIds, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2043,10 +2045,10 @@ Json::Value RPCHandler::doSubscribe(const Json::Value& jvRequest)
|
|||||||
{
|
{
|
||||||
BOOST_FOREACH(const RippleAddress& naAccountID, usnaAccoundIds)
|
BOOST_FOREACH(const RippleAddress& naAccountID, usnaAccoundIds)
|
||||||
{
|
{
|
||||||
isCurrent->insertSubAccountInfo(naAccountID);
|
mInfoSub->insertSubAccountInfo(naAccountID);
|
||||||
}
|
}
|
||||||
|
|
||||||
mNetOps->subAccount(isCurrent, usnaAccoundIds, false);
|
mNetOps->subAccount(mInfoSub, usnaAccoundIds, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2067,16 +2069,16 @@ Json::Value RPCHandler::doUnsubscribe(const Json::Value& jvRequest)
|
|||||||
|
|
||||||
if(streamName=="server")
|
if(streamName=="server")
|
||||||
{
|
{
|
||||||
mNetOps->unsubServer(isCurrent);
|
mNetOps->unsubServer(mInfoSub);
|
||||||
}else if(streamName=="ledger")
|
}else if(streamName=="ledger")
|
||||||
{
|
{
|
||||||
mNetOps->unsubLedger(isCurrent);
|
mNetOps->unsubLedger(mInfoSub);
|
||||||
}else if(streamName=="transactions")
|
}else if(streamName=="transactions")
|
||||||
{
|
{
|
||||||
mNetOps->unsubTransactions(isCurrent);
|
mNetOps->unsubTransactions(mInfoSub);
|
||||||
}else if(streamName=="rt_transactions")
|
}else if(streamName=="rt_transactions")
|
||||||
{
|
{
|
||||||
mNetOps->unsubRTTransactions(isCurrent);
|
mNetOps->unsubRTTransactions(mInfoSub);
|
||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
jvResult["error"] = str(boost::format("Unknown stream: %s") % streamName);
|
jvResult["error"] = str(boost::format("Unknown stream: %s") % streamName);
|
||||||
@@ -2099,10 +2101,10 @@ Json::Value RPCHandler::doUnsubscribe(const Json::Value& jvRequest)
|
|||||||
{
|
{
|
||||||
BOOST_FOREACH(const RippleAddress& naAccountID, usnaAccoundIds)
|
BOOST_FOREACH(const RippleAddress& naAccountID, usnaAccoundIds)
|
||||||
{
|
{
|
||||||
isCurrent->insertSubAccountInfo(naAccountID);
|
mInfoSub->insertSubAccountInfo(naAccountID);
|
||||||
}
|
}
|
||||||
|
|
||||||
mNetOps->unsubAccount(isCurrent, usnaAccoundIds,true);
|
mNetOps->unsubAccount(mInfoSub, usnaAccoundIds,true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2117,10 +2119,10 @@ Json::Value RPCHandler::doUnsubscribe(const Json::Value& jvRequest)
|
|||||||
{
|
{
|
||||||
BOOST_FOREACH(const RippleAddress& naAccountID, usnaAccoundIds)
|
BOOST_FOREACH(const RippleAddress& naAccountID, usnaAccoundIds)
|
||||||
{
|
{
|
||||||
isCurrent->insertSubAccountInfo(naAccountID);
|
mInfoSub->insertSubAccountInfo(naAccountID);
|
||||||
}
|
}
|
||||||
|
|
||||||
mNetOps->unsubAccount(isCurrent, usnaAccoundIds,false);
|
mNetOps->unsubAccount(mInfoSub, usnaAccoundIds,false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class NetworkOPs;
|
|||||||
class RPCHandler
|
class RPCHandler
|
||||||
{
|
{
|
||||||
NetworkOPs* mNetOps;
|
NetworkOPs* mNetOps;
|
||||||
InfoSub* isCurrent;
|
InfoSub* mInfoSub;
|
||||||
|
|
||||||
typedef Json::Value (RPCHandler::*doFuncPtr)(const Json::Value ¶ms);
|
typedef Json::Value (RPCHandler::*doFuncPtr)(const Json::Value ¶ms);
|
||||||
enum {
|
enum {
|
||||||
@@ -157,8 +157,9 @@ public:
|
|||||||
enum { GUEST, USER, ADMIN };
|
enum { GUEST, USER, ADMIN };
|
||||||
|
|
||||||
RPCHandler(NetworkOPs* netOps);
|
RPCHandler(NetworkOPs* netOps);
|
||||||
|
RPCHandler(NetworkOPs* netOps, InfoSub* infoSub);
|
||||||
|
|
||||||
Json::Value doCommand(const std::string& command, Json::Value& params, int role, InfoSub* sub = NULL);
|
Json::Value doCommand(const std::string& command, Json::Value& params, int role);
|
||||||
Json::Value rpcError(int iError);
|
Json::Value rpcError(int iError);
|
||||||
|
|
||||||
Json::Value handleJSONSubmit(const Json::Value& jvRequest);
|
Json::Value handleJSONSubmit(const Json::Value& jvRequest);
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ SETUP_LOG();
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
RPCServer::RPCServer(boost::asio::io_service& io_service , NetworkOPs* nopNetwork)
|
RPCServer::RPCServer(boost::asio::io_service& io_service , NetworkOPs* nopNetwork)
|
||||||
: mNetOps(nopNetwork), mRPCHandler(nopNetwork), mSocket(io_service)
|
: mNetOps(nopNetwork), mSocket(io_service)
|
||||||
{
|
{
|
||||||
|
|
||||||
mRole = RPCHandler::GUEST;
|
mRole = RPCHandler::GUEST;
|
||||||
@@ -142,6 +142,8 @@ std::string RPCServer::handleRequest(const std::string& requestStr)
|
|||||||
else if (!valParams.isArray())
|
else if (!valParams.isArray())
|
||||||
return(HTTPReply(400, "params unparseable"));
|
return(HTTPReply(400, "params unparseable"));
|
||||||
|
|
||||||
|
RPCHandler mRPCHandler(mNetOps);
|
||||||
|
|
||||||
cLog(lsTRACE) << valParams;
|
cLog(lsTRACE) << valParams;
|
||||||
Json::Value result = mRPCHandler.doCommand(strMethod, valParams,mRole);
|
Json::Value result = mRPCHandler.doCommand(strMethod, valParams,mRole);
|
||||||
cLog(lsTRACE) << result;
|
cLog(lsTRACE) << result;
|
||||||
|
|||||||
@@ -22,9 +22,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
||||||
NetworkOPs* mNetOps;
|
NetworkOPs* mNetOps;
|
||||||
RPCHandler mRPCHandler;
|
|
||||||
|
|
||||||
boost::asio::ip::tcp::socket mSocket;
|
boost::asio::ip::tcp::socket mSocket;
|
||||||
|
|
||||||
|
|||||||
@@ -45,16 +45,16 @@ Json::Value WSConnection::invokeCommand(Json::Value& jvRequest)
|
|||||||
return jvResult;
|
return jvResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RPCHandler mRPCHandler(&mNetwork, this);
|
||||||
Json::Value jvResult(Json::objectValue);
|
Json::Value jvResult(Json::objectValue);
|
||||||
|
|
||||||
// Regular RPC command
|
// Regular RPC command
|
||||||
jvResult["result"] = theApp->getRPCHandler().doCommand(
|
jvResult["result"] = mRPCHandler.doCommand(
|
||||||
jvRequest["command"].asString(),
|
jvRequest["command"].asString(),
|
||||||
jvRequest.isMember("params")
|
jvRequest.isMember("params")
|
||||||
? jvRequest["params"]
|
? jvRequest["params"]
|
||||||
: jvRequest,
|
: jvRequest,
|
||||||
mHandler->getPublic() ? RPCHandler::GUEST : RPCHandler::ADMIN,
|
mHandler->getPublic() ? RPCHandler::GUEST : RPCHandler::ADMIN);
|
||||||
this);
|
|
||||||
|
|
||||||
// Currently we will simply unwrap errors returned by the RPC
|
// Currently we will simply unwrap errors returned by the RPC
|
||||||
// API, in the future maybe we can make the responses
|
// API, in the future maybe we can make the responses
|
||||||
|
|||||||
Reference in New Issue
Block a user