Make a simple way to extend the RPC command set without modifying core server code.

This makes it easy to add self-contained modules that can be controlled and is also
useful for one-off and troubleshooting commands.
This commit is contained in:
JoelKatz
2013-01-13 22:46:42 -08:00
parent fc78cb38ed
commit 5fa43cc770
4 changed files with 80 additions and 1 deletions

View File

@@ -2432,6 +2432,13 @@ Json::Value RPCHandler::doRpcCommand(const std::string& strMethod, Json::Value&
return jvResult;
}
Json::Value RPCHandler::doInternal(Json::Value jvRequest)
{ // Used for debug or special-purpose RPC commands
if (!jvRequest.isMember("internal_command"))
return rpcError(rpcINVALID_PARAMS);
return RPCInternalHandler::runHandler(jvRequest["internal_command"].asString(), jvRequest["params"]);
}
Json::Value RPCHandler::doCommand(Json::Value& jvRequest, int iRole)
{
if (!jvRequest.isMember("command"))
@@ -2460,6 +2467,7 @@ Json::Value RPCHandler::doCommand(Json::Value& jvRequest, int iRole)
{ "account_tx", &RPCHandler::doAccountTransactions, false, optNetwork },
{ "connect", &RPCHandler::doConnect, true, optNone },
{ "get_counts", &RPCHandler::doGetCounts, true, optNone },
{ "internal", &RPCHandler::doInternal, true, optNone },
{ "ledger", &RPCHandler::doLedger, false, optNetwork },
{ "ledger_accept", &RPCHandler::doLedgerAccept, true, optCurrent },
{ "ledger_closed", &RPCHandler::doLedgerClosed, false, optClosed },
@@ -2571,4 +2579,29 @@ Json::Value RPCHandler::doCommand(Json::Value& jvRequest, int iRole)
}
}
RPCInternalHandler* RPCInternalHandler::sHeadHandler = NULL;
RPCInternalHandler::RPCInternalHandler(const std::string& name, handler_t Handler) : mName(name), mHandler(Handler)
{
mNextHandler = sHeadHandler;
sHeadHandler = this;
}
Json::Value RPCInternalHandler::runHandler(const std::string& name, const Json::Value& params)
{
RPCInternalHandler* h = sHeadHandler;
while (h != NULL)
{
if (name == h->mName)
{
cLog(lsWARNING) << "Internal command " << name << ": " << params;
Json::Value ret = h->mHandler(params);
cLog(lsWARNING) << "Internal command returns: " << ret;
return ret;
}
h = h->mNextHandler;
}
return rpcError(rpcBAD_SYNTAX);
}
// vim:ts=4