From 1ba1e0439044d35894a7f5732b22878405f5eea0 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Mon, 20 Jan 2020 16:43:27 -0400 Subject: [PATCH] Separate json SN parsing definition/declaration No changes to code here (committing this separately to make the changes in the following commit easier to follow). --- llarp/rpc/rpc.cpp | 83 ++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/llarp/rpc/rpc.cpp b/llarp/rpc/rpc.cpp index c218f4f1d..dc1bb16a3 100644 --- a/llarp/rpc/rpc.cpp +++ b/llarp/rpc/rpc.cpp @@ -110,46 +110,7 @@ namespace llarp } bool - HandleJSONResult(const nlohmann::json& result) override - { - PubkeyList_t keys; - if(not result.is_object()) - { - LogWarn("Invalid result: not an object"); - handler({}, false); - return false; - } - const auto itr = result.find("service_node_states"); - if(itr == result.end()) - { - LogWarn("Invalid result: no service_node_states member"); - handler({}, false); - return false; - } - if(not itr.value().is_array()) - { - LogWarn("Invalid result: service_node_states is not an array"); - handler({}, false); - return false; - } - for(const auto item : itr.value()) - { - if(not item.is_object()) - continue; - if(not item.value("active", false)) - continue; - if(not item.value("funded", false)) - continue; - const std::string pk = item.value("pubkey_ed25519", ""); - if(pk.empty()) - continue; - PubKey k; - if(k.FromString(pk)) - keys.emplace_back(std::move(k)); - } - handler(keys, not keys.empty()); - return true; - } + HandleJSONResult(const nlohmann::json& result) override; void HandleError() override @@ -253,6 +214,48 @@ namespace llarp ~CallerImpl() = default; }; + bool + GetServiceNodeListHandler::HandleJSONResult(const nlohmann::json& result) + { + PubkeyList_t keys; + if(not result.is_object()) + { + LogWarn("Invalid result: not an object"); + handler({}, false); + return false; + } + const auto itr = result.find("service_node_states"); + if(itr == result.end()) + { + LogWarn("Invalid result: no service_node_states member"); + handler({}, false); + return false; + } + if(not itr.value().is_array()) + { + LogWarn("Invalid result: service_node_states is not an array"); + handler({}, false); + return false; + } + for(const auto item : itr.value()) + { + if(not item.is_object()) + continue; + if(not item.value("active", false)) + continue; + if(not item.value("funded", false)) + continue; + const std::string pk = item.value("pubkey_ed25519", ""); + if(pk.empty()) + continue; + PubKey k; + if(k.FromString(pk)) + keys.emplace_back(std::move(k)); + } + handler(keys, not keys.empty()); + return true; + } + void CallerHandler::PopulateReqHeaders(abyss::http::Headers_t& hdr) {