From bcdd8ab519758f39aedc59f1835f14ae668516bb Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Fri, 6 Apr 2012 22:27:14 -0700 Subject: [PATCH] Use newcoind.cfg instead of config.xml --- src/CallRPC.cpp | 6 ++--- src/Config.cpp | 58 ++++++++++++++++++++++++++++++++++-------- src/Config.h | 5 ++++ src/PeerDoor.cpp | 4 +-- src/RPCDoor.cpp | 4 +-- src/RPCServer.cpp | 9 ++++++- src/UniqueNodeList.cpp | 1 - 7 files changed, 67 insertions(+), 20 deletions(-) diff --git a/src/CallRPC.cpp b/src/CallRPC.cpp index 11a3bc297..23e776b76 100644 --- a/src/CallRPC.cpp +++ b/src/CallRPC.cpp @@ -126,7 +126,7 @@ Json::Value callRPC(const std::string& strMethod, const Json::Value& params) // Connect to localhost std::cout << "Connecting to port:" << theConfig.RPC_PORT << std::endl; - ip::tcp::endpoint endpoint( ip::address::from_string("127.0.0.1"), theConfig.RPC_PORT); + ip::tcp::endpoint endpoint(ip::address::from_string(theConfig.RPC_IP), theConfig.RPC_PORT); ip::tcp::iostream stream; stream.connect(endpoint); if(stream.fail()) @@ -142,11 +142,11 @@ Json::Value callRPC(const std::string& strMethod, const Json::Value& params) // Send request std::string strRequest = JSONRPCRequest(strMethod, params, Json::Value(1)); - std::cout << "send request " << strMethod << " : " << strRequest << std::endl; + std::cout << "send request " << strMethod << " : " << strRequest << std::endl; std::string strPost = createHTTPPost(strRequest, mapRequestHeaders); stream << strPost << std::flush; - std::cout << "post " << strPost << std::endl; + std::cout << "post " << strPost << std::endl; // Receive reply std::map mapHeaders; diff --git a/src/Config.cpp b/src/Config.cpp index 1a69b73d7..df339d23f 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -1,9 +1,17 @@ #include "Config.h" -#include "../util/pugixml.hpp" +#include "ParseSection.h" + +#include #include -using namespace pugi; +#define CONFIG_FILE_NAME "newcoind.cfg" +#define SECTION_PEER_IP "peer_ip" +#define SECTION_PEER_PORT "peer_port" +#define SECTION_RPC_IP "rpc_ip" +#define SECTION_RPC_PORT "rpc_port" +#define SECTION_VALIDATION_PASSWORD "validation_password" +#define SECTION_VALIDATION_KEY "validation_key" Config theConfig; @@ -17,7 +25,7 @@ Config::Config() PEER_PORT=6561; RPC_PORT=5001; NUMBER_CONNECTIONS=30; - + // a new ledger every 30 min LEDGER_SECONDS=(60*30); @@ -31,16 +39,42 @@ Config::Config() void Config::load() { - - xml_document doc; - xml_parse_result result = doc.load_file("config.xml"); - xml_node root=doc.child("config"); + std::ifstream ifsConfig(CONFIG_FILE_NAME, std::ios::in); - xml_node node= root.child("PEER_PORT"); - if(!node.empty()) PEER_PORT=boost::lexical_cast(node.child_value()); + if (!ifsConfig) + { + std::cerr << "Failed to open '" CONFIG_FILE_NAME "'." << std::endl; + } + else + { + std::string strConfigFile; - node= root.child("RPC_PORT"); - if(!node.empty()) RPC_PORT=boost::lexical_cast(node.child_value()); + strConfigFile.assign((std::istreambuf_iterator(ifsConfig)), + std::istreambuf_iterator()); + + if (ifsConfig.bad()) + { + std::cerr << "Failed to read '" CONFIG_FILE_NAME "'." << std::endl; + } + else + { + section secConfig = ParseSection(strConfigFile, true); + std::string strTemp; + + (void) sectionSingleB(secConfig, SECTION_PEER_IP, PEER_IP); + + if (sectionSingleB(secConfig, SECTION_PEER_PORT, strTemp)) + PEER_PORT=boost::lexical_cast(strTemp); + + (void) sectionSingleB(secConfig, SECTION_RPC_IP, RPC_IP); + + if (sectionSingleB(secConfig, SECTION_RPC_PORT, strTemp)) + RPC_PORT=boost::lexical_cast(strTemp); + + (void) sectionSingleB(secConfig, SECTION_VALIDATION_PASSWORD, VALIDATION_PASSWORD); + (void) sectionSingleB(secConfig, SECTION_VALIDATION_KEY, VALIDATION_KEY); + } + } /* node=root.child("DB_TYPE"); @@ -51,3 +85,5 @@ void Config::load() }else */ } + +// vim:ts=4 diff --git a/src/Config.h b/src/Config.h index 44bfcdb92..4e2628b9e 100644 --- a/src/Config.h +++ b/src/Config.h @@ -20,6 +20,7 @@ public: float BELIEF_PERCENT; // node networking parameters + std::string PEER_IP; int PEER_PORT; int NUMBER_CONNECTIONS; bool NODE_INBOUND; // we accept inbound connections @@ -31,10 +32,14 @@ public: std::string HANKO_PRIVATE; // RPC parameters + std::string RPC_IP; int RPC_PORT; std::string RPC_USER; std::string RPC_PASSWORD; + std::string VALIDATION_PASSWORD; + std::string VALIDATION_KEY; + // configuration parameters std::string DATA_DIR; diff --git a/src/PeerDoor.cpp b/src/PeerDoor.cpp index 2ff1b63df..5ecfc93ed 100644 --- a/src/PeerDoor.cpp +++ b/src/PeerDoor.cpp @@ -6,8 +6,8 @@ using namespace std; using namespace boost::asio::ip; -PeerDoor::PeerDoor(boost::asio::io_service& io_service) : - mAcceptor(io_service, tcp::endpoint(tcp::v4(), theConfig.PEER_PORT)) +PeerDoor::PeerDoor(boost::asio::io_service& io_service) : + mAcceptor(io_service, tcp::endpoint(address().from_string(theConfig.PEER_IP), theConfig.PEER_PORT)) { cout << "Opening peer door on port: " << theConfig.PEER_PORT << endl; startListening(); diff --git a/src/RPCDoor.cpp b/src/RPCDoor.cpp index 6d61067de..95a0aee15 100644 --- a/src/RPCDoor.cpp +++ b/src/RPCDoor.cpp @@ -7,8 +7,8 @@ using namespace std; using namespace boost::asio::ip; -RPCDoor::RPCDoor(boost::asio::io_service& io_service) : - mAcceptor(io_service, tcp::endpoint(boost::asio::ip::address_v4::loopback(), theConfig.RPC_PORT)) +RPCDoor::RPCDoor(boost::asio::io_service& io_service) : + mAcceptor(io_service, tcp::endpoint(address::from_string(theConfig.RPC_IP), theConfig.RPC_PORT)) { cout << "Opening rpc door on port: " << theConfig.RPC_PORT << endl; startListening(); diff --git a/src/RPCServer.cpp b/src/RPCServer.cpp index fa8ed5ff6..70b7ee895 100644 --- a/src/RPCServer.cpp +++ b/src/RPCServer.cpp @@ -558,7 +558,7 @@ Json::Value RPCServer::doUnlDefault(Json::Value& params) { if (!ifsDefault) { - std::cerr << "Failed to read '" VALIDATORS_FILE_NAME "'." << std::endl; + std::cerr << "Failed to open '" VALIDATORS_FILE_NAME "'." << std::endl; bNetwork = true; } @@ -566,6 +566,13 @@ Json::Value RPCServer::doUnlDefault(Json::Value& params) { { strValidators.assign((std::istreambuf_iterator(ifsDefault)), std::istreambuf_iterator()); + + if (ifsDefault.bad()) + { + std::cerr << "Failed to read '" VALIDATORS_FILE_NAME "'." << std::endl; + + bNetwork = true; + } } } diff --git a/src/UniqueNodeList.cpp b/src/UniqueNodeList.cpp index 9976b4cd4..d4b22788a 100644 --- a/src/UniqueNodeList.cpp +++ b/src/UniqueNodeList.cpp @@ -118,7 +118,6 @@ void UniqueNodeList::responseFetch(const std::string strDomain, const boost::sys std::cerr << "Error: " << err.message() << std::endl; section secSite = ParseSection(strSiteFile, true); - section::iterator it; bool bGood = !err; //