aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Garzik <jgarzik@exmulti.com>2012-06-27 13:47:02 -0400
committerJeff Garzik <jgarzik@redhat.com>2012-07-03 22:53:30 -0400
commitc6494d82fa31913eb075b222293e87ae40c9f9b2 (patch)
tree0c529ea0e87943bc05d4755f40ff50e51f3689d3
parent38986093045f252753e35b3d9d4c3e2ad0c82af3 (diff)
RPC: break out high level JSON-RPC req/resp into their own functions
This prepares for JSON-RPC 2.0 batches.
-rw-r--r--src/bitcoinrpc.cpp85
1 files changed, 55 insertions, 30 deletions
diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp
index 51690243bc..134c3be93f 100644
--- a/src/bitcoinrpc.cpp
+++ b/src/bitcoinrpc.cpp
@@ -2552,7 +2552,7 @@ string JSONRPCRequest(const string& strMethod, const Array& params, const Value&
return write_string(Value(request), false) + "\n";
}
-string JSONRPCReply(const Value& result, const Value& error, const Value& id)
+Object JSONRPCReplyObj(const Value& result, const Value& error, const Value& id)
{
Object reply;
if (error.type() != null_type)
@@ -2561,6 +2561,12 @@ string JSONRPCReply(const Value& result, const Value& error, const Value& id)
reply.push_back(Pair("result", result));
reply.push_back(Pair("error", error));
reply.push_back(Pair("id", id));
+ return reply;
+}
+
+string JSONRPCReply(const Value& result, const Value& error, const Value& id)
+{
+ Object reply = JSONRPCReplyObj(result, error, id);
return write_string(Value(reply), false) + "\n";
}
@@ -2905,6 +2911,47 @@ void ThreadRPCServer2(void* parg)
StopRequests();
}
+class JSONRequest
+{
+public:
+ Value id;
+ string strMethod;
+ Array params;
+
+ JSONRequest() { id = Value::null; }
+ void parse(const Value& valRequest);
+};
+
+void JSONRequest::parse(const Value& valRequest)
+{
+ // Parse request
+ if (valRequest.type() != obj_type)
+ throw JSONRPCError(-32600, "Invalid Request object");
+ const Object& request = valRequest.get_obj();
+
+ // Parse id now so errors from here on will have the id
+ id = find_value(request, "id");
+
+ // Parse method
+ Value valMethod = find_value(request, "method");
+ if (valMethod.type() == null_type)
+ throw JSONRPCError(-32600, "Missing method");
+ if (valMethod.type() != str_type)
+ throw JSONRPCError(-32600, "Method must be a string");
+ strMethod = valMethod.get_str();
+ if (strMethod != "getwork" && strMethod != "getmemorypool")
+ printf("ThreadRPCServer method=%s\n", strMethod.c_str());
+
+ // Parse params
+ Value valParams = find_value(request, "params");
+ if (valParams.type() == array_type)
+ params = valParams.get_array();
+ else if (valParams.type() == null_type)
+ params = Array();
+ else
+ throw JSONRPCError(-32600, "Params must be an array");
+}
+
static CCriticalSection cs_THREAD_RPCHANDLER;
void ThreadRPCServer3(void* parg)
@@ -2954,52 +3001,30 @@ void ThreadRPCServer3(void* parg)
if (mapHeaders["connection"] == "close")
fRun = false;
- Value id = Value::null;
+ JSONRequest jreq;
try
{
// Parse request
Value valRequest;
if (!read_string(strRequest, valRequest) || valRequest.type() != obj_type)
throw JSONRPCError(-32700, "Parse error");
- const Object& request = valRequest.get_obj();
-
- // Parse id now so errors from here on will have the id
- id = find_value(request, "id");
-
- // Parse method
- Value valMethod = find_value(request, "method");
- if (valMethod.type() == null_type)
- throw JSONRPCError(-32600, "Missing method");
- if (valMethod.type() != str_type)
- throw JSONRPCError(-32600, "Method must be a string");
- string strMethod = valMethod.get_str();
- if (strMethod != "getwork" && strMethod != "getmemorypool")
- printf("ThreadRPCServer method=%s\n", strMethod.c_str());
-
- // 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
- throw JSONRPCError(-32600, "Params must be an array");
- Value result = tableRPC.execute(strMethod, params);
+ jreq.parse(valRequest);
+
+ Value result = tableRPC.execute(jreq.strMethod, jreq.params);
// Send reply
- string strReply = JSONRPCReply(result, Value::null, id);
+ string strReply = JSONRPCReply(result, Value::null, jreq.id);
conn->stream() << HTTPReply(200, strReply, fRun) << std::flush;
}
catch (Object& objError)
{
- ErrorReply(conn->stream(), objError, id);
+ ErrorReply(conn->stream(), objError, jreq.id);
break;
}
catch (std::exception& e)
{
- ErrorReply(conn->stream(), JSONRPCError(-32700, e.what()), id);
+ ErrorReply(conn->stream(), JSONRPCError(-32700, e.what()), jreq.id);
break;
}
}