From 4982ffdf74478c5a3329e37e1f9c4cf7c805cd1a Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Wed, 16 Jan 2013 15:05:44 -0800 Subject: [PATCH] Add support for calling RPC command at startup from config file. --- src/cpp/ripple/Application.cpp | 1 + src/cpp/ripple/CallRPC.cpp | 8 ++++++-- src/cpp/ripple/Config.cpp | 29 +++++++++++++++++++++++------ src/cpp/ripple/Config.h | 7 +++++-- src/cpp/ripple/main.cpp | 18 +++++++++++++++++- 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/cpp/ripple/Application.cpp b/src/cpp/ripple/Application.cpp index 5be04fc9b4..ec034eedfc 100644 --- a/src/cpp/ripple/Application.cpp +++ b/src/cpp/ripple/Application.cpp @@ -18,6 +18,7 @@ #include SETUP_LOG(); + LogPartition TaggedCachePartition("TaggedCache"); Application* theApp = NULL; diff --git a/src/cpp/ripple/CallRPC.cpp b/src/cpp/ripple/CallRPC.cpp index 424f64a731..ae963cd7d9 100644 --- a/src/cpp/ripple/CallRPC.cpp +++ b/src/cpp/ripple/CallRPC.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -13,6 +14,7 @@ #include "../json/value.h" #include "../json/reader.h" +#include "Application.h" #include "RPC.h" #include "Log.h" #include "RPCErr.h" @@ -54,8 +56,10 @@ std::string EncodeBase64(const std::string& s) Json::Value RPCParser::parseAsIs(const Json::Value& jvParams) { Json::Value v(Json::objectValue); + if (jvParams.isArray() && (jvParams.size() > 0)) v["params"] = jvParams; + return v; } @@ -656,7 +660,7 @@ int commandLineRPC(const std::vector& vCmd) return nRet; } -Json::Value callRPC(const std::string& strIp, const int iPort, const std::string& strUsername, const std::string& strPassword, const std::string& strPath, const std::string& strMethod, const Json::Value& params) +Json::Value callRPC(const std::string& strIp, const int iPort, const std::string& strUsername, const std::string& strPassword, const std::string& strPath, const std::string& strMethod, const Json::Value& jvParams) { // Connect to localhost if (!theConfig.QUIET) @@ -684,7 +688,7 @@ Json::Value callRPC(const std::string& strIp, const int iPort, const std::string // Log(lsDEBUG) << "requesting" << std::endl; // Send request - std::string strRequest = JSONRPCRequest(strMethod, params, Json::Value(1)); + std::string strRequest = JSONRPCRequest(strMethod, jvParams, Json::Value(1)); // cLog(lsDEBUG) << "send request " << strMethod << " : " << strRequest << std::endl; std::string strPost = createHTTPPost(strPath, strRequest, mapRequestHeaders); diff --git a/src/cpp/ripple/Config.cpp b/src/cpp/ripple/Config.cpp index 129f25965f..a886e27fab 100644 --- a/src/cpp/ripple/Config.cpp +++ b/src/cpp/ripple/Config.cpp @@ -1,17 +1,18 @@ // // TODO: Check permissions on config file before using it. // +#include +#include +#include +#include +#include +#include + #include "Config.h" #include "utils.h" #include "HashPrefixes.h" -#include -#include -#include -#include -#include - #define SECTION_ACCOUNT_PROBE_MAX "account_probe_max" #define SECTION_CLUSTER_NODES "cluster_nodes" #define SECTION_DATABASE_PATH "database_path" @@ -36,6 +37,7 @@ #define SECTION_RPC_ALLOW_REMOTE "rpc_allow_remote" #define SECTION_RPC_IP "rpc_ip" #define SECTION_RPC_PORT "rpc_port" +#define SECTION_RPC_STARTUP "rpc_startup" #define SECTION_SNTP "sntp_servers" #define SECTION_VALIDATORS_FILE "validators_file" #define SECTION_VALIDATION_QUORUM "validation_quorum" @@ -268,6 +270,21 @@ void Config::load() SNTP_SERVERS = *smtTmp; } + smtTmp = sectionEntries(secConfig, SECTION_RPC_STARTUP); + if (smtTmp) + { + BOOST_FOREACH(const std::string& strJson, *smtTmp) + { + Json::Reader jrReader; + Json::Value jvCommand; + + if (!jrReader.parse(strJson, jvCommand)) + throw std::runtime_error(boost::str(boost::format("Couldn't parse ["SECTION_RPC_STARTUP"] command: %s") % strJson)); + + RPC_STARTUP.push_back(jvCommand); + } + } + if (sectionSingleB(secConfig, SECTION_DATABASE_PATH, DATABASE_PATH)) DATA_DIR = DATABASE_PATH; diff --git a/src/cpp/ripple/Config.h b/src/cpp/ripple/Config.h index 92f83c8dbd..b24df2ed16 100644 --- a/src/cpp/ripple/Config.h +++ b/src/cpp/ripple/Config.h @@ -1,13 +1,15 @@ #ifndef __CONFIG__ #define __CONFIG__ +#include +#include + #include "types.h" #include "RippleAddress.h" #include "ParseSection.h" #include "SerializedTypes.h" -#include -#include +#include "../json/value.h" #define ENABLE_INSECURE 0 // 1, to enable unnecessary features. @@ -110,6 +112,7 @@ public: std::string RPC_USER; std::string RPC_PASSWORD; bool RPC_ALLOW_REMOTE; + std::vector RPC_STARTUP; // Validation RippleAddress VALIDATION_SEED, VALIDATION_PUB, VALIDATION_PRIV; diff --git a/src/cpp/ripple/main.cpp b/src/cpp/ripple/main.cpp index d9e8281451..e0646428f0 100644 --- a/src/cpp/ripple/main.cpp +++ b/src/cpp/ripple/main.cpp @@ -9,8 +9,9 @@ #include "Application.h" #include "CallRPC.h" #include "Config.h" -#include "utils.h" #include "Log.h" +#include "RPCHandler.h" +#include "utils.h" namespace po = boost::program_options; @@ -26,6 +27,21 @@ void setupServer() void startServer() { + // + // Execute start up rpc commands. + // + BOOST_FOREACH(const Json::Value& jvCommand, theConfig.RPC_STARTUP) + { + if (!theConfig.QUIET) + cerr << "Startup RPC: " << jvCommand << endl; + + RPCHandler rhHandler(&theApp->getOPs()); + + std::cerr << "Result: " + << rhHandler.doCommand(jvCommand, RPCHandler::ADMIN) + << std::endl; + } + theApp->run(); // Blocks till we get a stop RPC. }