Small refactor to Config paths

This commit is contained in:
Vinnie Falco
2013-09-11 16:13:48 -07:00
parent 99afec18c5
commit 03b8ae742e
9 changed files with 583 additions and 308 deletions

View File

@@ -229,11 +229,13 @@ public:
m_validators->addStrings (strings);
}
#if 0
{
String const& localValidatorsPath (getConfig().localValidatorsPath);
if (localValidatorsPath != String::empty)
m_validators->addFile (localValidatorsPath);
}
#endif
}
//--------------------------------------------------------------------------

View File

@@ -515,21 +515,24 @@ public:
{
if (pConfig.empty ())
{
WriteLog (lsINFO, UniqueNodeList) << VALIDATORS_FILE_NAME " path not specified.";
WriteLog (lsINFO, UniqueNodeList) << Config::Helpers::getValidatorsFileName() <<
" path not specified.";
return false;
}
if (!boost::filesystem::exists (pConfig))
{
WriteLog (lsWARNING, UniqueNodeList) << str (boost::format (VALIDATORS_FILE_NAME " not found: %s") % pConfig);
WriteLog (lsWARNING, UniqueNodeList) << Config::Helpers::getValidatorsFileName() <<
" not found: " << pConfig;
return false;
}
if (!boost::filesystem::is_regular_file (pConfig))
{
WriteLog (lsWARNING, UniqueNodeList) << str (boost::format (VALIDATORS_FILE_NAME " not regular file: %s") % pConfig);
WriteLog (lsWARNING, UniqueNodeList) << Config::Helpers::getValidatorsFileName() <<
" not regular file: " << pConfig;
return false;
}
@@ -538,7 +541,8 @@ public:
if (!ifsDefault)
{
WriteLog (lsFATAL, UniqueNodeList) << str (boost::format (VALIDATORS_FILE_NAME " failed to open: %s") % pConfig);
WriteLog (lsFATAL, UniqueNodeList) << Config::Helpers::getValidatorsFileName() <<
" failed to open: " << pConfig;
return false;
}
@@ -550,7 +554,8 @@ public:
if (ifsDefault.bad ())
{
WriteLog (lsFATAL, UniqueNodeList) << str (boost::format ("Failed to read: %s") % pConfig);
WriteLog (lsFATAL, UniqueNodeList) << Config::Helpers::getValidatorsFileName() <<
"Failed to read: " << pConfig;
return false;
}
@@ -2002,7 +2007,10 @@ private:
if (!bReject)
{
WriteLog (lsTRACE, UniqueNodeList) << "Fetch '" VALIDATORS_FILE_NAME "' complete.";
WriteLog (lsTRACE, UniqueNodeList) <<
"Fetch '" <<
Config::Helpers::getValidatorsFileName () <<
"' complete.";
if (!err)
{

View File

@@ -16,18 +16,26 @@
#define DEFAULT_FEE_OFFER DEFAULT_FEE_DEFAULT
#define DEFAULT_FEE_OPERATION 1
// VFALCO TODO Convert this to a SharedSingleton to prevent exit leaks
//
Config& getConfig ()
{
static Config config;
return config;
}
//------------------------------------------------------------------------------
Config::Config ()
: m_rpcPort (5001)
, SSL_CONTEXT (boost::asio::ssl::context::sslv23)
{
//--------------------------------------------------------------------------
//
// VFALCO NOTE Clean member area
//
peerListeningPort = SYSTEM_PEER_PORT;
peerPROXYListeningPort = 0;
//
//
//
//--------------------------------------------------------------------------
//
// Defaults
//
@@ -86,21 +94,6 @@ Config::Config ()
ELB_SUPPORT = false;
RUN_STANDALONE = false;
START_UP = NORMAL;
//--------------------------------------------------------------------------
//
// VFALCO NOTE Clean area
//
peerListeningPort = SYSTEM_PEER_PORT;
peerPROXYListeningPort = 0;
//
//
//
//--------------------------------------------------------------------------
}
void Config::setup (const std::string& strConf, bool bTestNet, bool bQuiet)
@@ -118,13 +111,15 @@ void Config::setup (const std::string& strConf, bool bTestNet, bool bQuiet)
QUIET = bQuiet;
NODE_SIZE = 0;
// TESTNET forces a "testnet-" prefix on the conf file and db directory.
strDbPath = TESTNET ? "testnet-db" : "db";
strConfFile = boost::str (boost::format (TESTNET ? "testnet-%s" : "%s")
% (strConf.empty () ? CONFIG_FILE_NAME : strConf));
// VFALCO NOTE TESTNET forces a "testnet-" prefix on the conf
// file and db directory, unless --conf is specified
// in which case there is no forced prefix.
strDbPath = Helpers::getDatabaseDirName (TESTNET);
strConfFile = strConf.empty () ? Helpers::getConfigFileName (TESTNET) : strConf;
VALIDATORS_BASE = Helpers::getValidatorsFileName (TESTNET);
VALIDATORS_BASE = boost::str (boost::format (TESTNET ? "testnet-%s" : "%s")
% VALIDATORS_FILE_NAME);
VALIDATORS_URI = boost::str (boost::format ("/%s") % VALIDATORS_BASE);
if (TESTNET)
@@ -526,7 +521,6 @@ void Config::load ()
if (SectionSingleB (secConfig, SECTION_VALIDATORS_FILE, strTemp))
{
localValidatorsPath = strTemp;
VALIDATORS_FILE = strTemp;
}
@@ -572,6 +566,164 @@ int Config::getSize (SizedItemName item)
return -1;
}
//------------------------------------------------------------------------------
//
// VFALCO NOTE Clean members area
//
Config& getConfig ()
{
static Config config;
return config;
}
//------------------------------------------------------------------------------
/* The location of the configuration file is checked as follows,
in order of descending priority:
1. In the path specified by --conf command line option if present
(which is relative to the current directory)
2. In the current user's "home" directory
3. In the same directory as the rippled executable
4. In the "current" directory defined by the process which launched rippled
*/
File Config::findConfigFile (String commandLineLocation, bool forTestNetwork)
{
File file (File::nonexistent ());
#if 0
// Highest priority goes to commandLineLocation
//
if (file == File::nonexistent() && commandLineLocation != String::empty)
{
// If commandLineLocation is a full path,
// this will just assign the full path to file.
//
file = File::getCurrentDirectory().getChildFile (commandLineLocation);
if (! file.existsAsFile ())
file = File::nonexistent ();
}
// Next, we will look in the user's home directory
//
#else
#if 0
// VFALCO NOTE This is the original legacy code...
std::string strConfFile;
// Determine the config and data directories.
// If the config file is found in the current working directory,
// use the current working directory as the config directory and
// that with "db" as the data directory.
{
String s;
if (forTestNetwork)
s += "testnet-";
if (commandLineLocation != String::empty)
s += Helpers::getConfigFileName (forTestNetwork);
strConfFile = boost::str (boost::format (forTestNetwork ? "testnet-%s" : "%s")
% (strConf.empty () ? Helpers::getConfigFileName() : strConf));
VALIDATORS_BASE = boost::str (boost::format (TESTNET ? "testnet-%s" : "%s")
% VALIDATORS_FILE_NAME);
VALIDATORS_URI = boost::str (boost::format ("/%s") % VALIDATORS_BASE);
if (TESTNET)
{
SIGN_TRANSACTION = HashPrefix::txSignTestnet;
SIGN_VALIDATION = HashPrefix::validationTestnet;
SIGN_PROPOSAL = HashPrefix::proposalTestnet;
}
else
{
SIGN_TRANSACTION = HashPrefix::txSign;
SIGN_VALIDATION = HashPrefix::validation;
SIGN_PROPOSAL = HashPrefix::proposal;
}
if (TESTNET)
Base58::setCurrentAlphabet (Base58::getTestnetAlphabet ());
if (!strConf.empty ())
{
// --conf=<path> : everything is relative that file.
CONFIG_FILE = strConfFile;
CONFIG_DIR = boost::filesystem::absolute (CONFIG_FILE);
CONFIG_DIR.remove_filename ();
DATA_DIR = CONFIG_DIR / strDbPath;
}
else
{
CONFIG_DIR = boost::filesystem::current_path ();
CONFIG_FILE = CONFIG_DIR / strConfFile;
DATA_DIR = CONFIG_DIR / strDbPath;
if (exists (CONFIG_FILE)
// Can we figure out XDG dirs?
|| (!getenv ("HOME") && (!getenv ("XDG_CONFIG_HOME") || !getenv ("XDG_DATA_HOME"))))
{
// Current working directory is fine, put dbs in a subdir.
nothing ();
}
else
{
// Construct XDG config and data home.
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
std::string strHome = strGetEnv ("HOME");
std::string strXdgConfigHome = strGetEnv ("XDG_CONFIG_HOME");
std::string strXdgDataHome = strGetEnv ("XDG_DATA_HOME");
if (strXdgConfigHome.empty ())
{
// $XDG_CONFIG_HOME was not set, use default based on $HOME.
strXdgConfigHome = boost::str (boost::format ("%s/.config") % strHome);
}
if (strXdgDataHome.empty ())
{
// $XDG_DATA_HOME was not set, use default based on $HOME.
strXdgDataHome = boost::str (boost::format ("%s/.local/share") % strHome);
}
CONFIG_DIR = boost::str (boost::format ("%s/" SYSTEM_NAME) % strXdgConfigHome);
CONFIG_FILE = CONFIG_DIR / strConfFile;
DATA_DIR = boost::str (boost::format ("%s/" SYSTEM_NAME) % strXdgDataHome);
boost::filesystem::create_directories (CONFIG_DIR, ec);
if (ec)
throw std::runtime_error (boost::str (boost::format ("Can not create %s") % CONFIG_DIR));
}
}
#endif
#endif
return file;
}
//------------------------------------------------------------------------------
File Config::getConfigDir () const
{
return File (String (CONFIG_FILE.native ().c_str ())).getParentDirectory ();
}
File Config::getDatabaseDir () const
{
return File (String (DATA_DIR.native ().c_str ()));
}
//------------------------------------------------------------------------------
void Config::setRpcIpAndOptionalPort (std::string const& newAddress)
{
String const s (newAddress.c_str ());
@@ -632,3 +784,6 @@ Config::Role Config::getAdminRole (Json::Value const& params, std::string const&
return role;
}
//
//
//------------------------------------------------------------------------------

View File

@@ -4,8 +4,8 @@
*/
//==============================================================================
#ifndef RIPPLE_CONFIG_H
#define RIPPLE_CONFIG_H
#ifndef RIPPLE_CORE_CONFIG_H_INCLUDED
#define RIPPLE_CORE_CONFIG_H_INCLUDED
// VFALCO TODO Replace these with beast "unsigned long long" generators
// VFALCO NOTE Apparently these are used elsewhere. Make them constants in the config
@@ -19,10 +19,7 @@
// VFALCO NOTE Set this to 1 to enable code which is unnecessary
#define ENABLE_INSECURE 0
#define CONFIG_FILE_NAME SYSTEM_NAME "d.cfg" // rippled.cfg
#define DEFAULT_VALIDATORS_SITE ""
#define VALIDATORS_FILE_NAME "validators.txt"
const int DOMAIN_BYTES_MAX = 256;
const int PUBLIC_BYTES_MAX = 33; // Maximum bytes for an account public key.
@@ -77,18 +74,210 @@ struct SizedItem
class Config
{
public:
// Configuration parameters
bool QUIET;
bool TESTNET;
//--------------------------------------------------------------------------
//
// VFALCO NOTE To tame this "Config" beast I am breaking it up into
// individual sections related to a specific area of the
// program. For example, listening port configuration. Or
// node database configuration. Each class has its own
// default constructor, and load function for reading in
// settings from the parsed config file data.
//
// Clean member area. Please follow this style for modifying
// or adding code in the file.
struct Helpers
{
// This replaces CONFIG_FILE_NAME
static char const* getConfigFileName (bool forTestNetwork = false)
{
return forTestNetwork ? "testnet-rippled.cfg" : "rippled.cfg";
}
static char const* getDatabaseDirName (bool forTestNetwork = false)
{
return forTestNetwork ? "testnet-db" : "db";
}
static char const* getValidatorsFileName (bool forTestNetwork = false)
{
return forTestNetwork ? "testnet-validators.txt" : "validators.txt";
}
};
/** The result of performing a load on the parsed config file data.
This type is convertible to `bool`.
A value of `true` indicates an error occurred,
while `false` indicates no error.
*/
class Error : public SafeBool <Error>
{
public:
Error () noexcept
: m_what (String::empty)
, m_fileName ("")
, m_lineNumber (0)
{
}
Error (String what, char const* fileName, int lineNumber) noexcept
: m_what (what)
, m_fileName (fileName)
, m_lineNumber (lineNumber)
{
}
bool asBoolean () const noexcept
{
return m_what != String::empty;
}
String what () const noexcept
{
return m_what;
}
char const* fileName () const
{
return m_fileName;
}
int lineNumber () const
{
return m_lineNumber;
}
private:
String m_what;
char const* m_fileName;
int m_lineNumber;
};
/** Listening socket settings. */
struct DoorSettings
{
/** Create a default set of door (listening socket) settings. */
DoorSettings ();
/** Load settings from the configuration file. */
//Error load (ParsedConfigFile const& file);
};
//--------------------------------------------------------------------------
/** Determine the location of the config file.
This searches the location provided on the command line first,
followed by the platform specific "user's home" directory.
@param commandLineLocation an optional location passed from the command line.
@param forTestNetwork Whether or not we are operating on the test network (DEPRECATATED)
*/
static File findConfigFile (
String commandLineLocation = String::empty,
bool forTestNetwork = false);
//--------------------------------------------------------------------------
// Settings related to the configuration file location and directories
/** Returns the directory from which the configuration file was loaded. */
File getConfigDir () const;
/** Returns the directory in which the current database files are located. */
File getDatabaseDir () const;
// LEGACY FIELDS, REMOVE ASAP
boost::filesystem::path CONFIG_FILE; // used by UniqueNodeList
private:
boost::filesystem::path CONFIG_DIR;
public:
// VFALCO TODO Make this private and fix callers to go through getDatabaseDir()
boost::filesystem::path DATA_DIR;
//--------------------------------------------------------------------------
// Settings related to validators
boost::filesystem::path CONFIG_FILE;
boost::filesystem::path CONFIG_DIR;
boost::filesystem::path DATA_DIR;
boost::filesystem::path DEBUG_LOGFILE;
boost::filesystem::path VALIDATORS_FILE; // As specifed in rippled.cfg.
//--------------------------------------------------------------------------
// Settings related to RPC
/** Get the client or server RPC IP address.
@note The string may not always be in a valid parsable state.
@return A string representing the address.
*/
std::string getRpcIP () const { return m_rpcIP; }
/** Get the client or server RPC port number.
@note The port number may be invalid (out of range or zero)
@return The RPC port number.
*/
int getRpcPort () const { return m_rpcPort; }
/** Set the client or server RPC IP and optional port.
@note The string is not syntax checked.
@param newAddress A string in the format <ip-address>[':'<port-number>]
*/
void setRpcIpAndOptionalPort (std::string const& newAddress);
/** Set the client or server RPC IP.
@note The string is not syntax-checked.
@param newIP A string representing the IP address to use.
*/
void setRpcIP (std::string const& newIP) { m_rpcIP = newIP; }
/** Set the client or server RPC port number.
@note The port number is not range checked.
@param newPort The RPC port number to use.
*/
void setRpcPort (int newPort) { m_rpcPort = newPort; }
/** Convert the RPC/port combination to a readable string.
*/
String const getRpcAddress ()
{
String s;
s << m_rpcIP.c_str () << ":" << m_rpcPort;
return s;
}
/** Determine the level of administrative permission to grant.
*/
enum Role
{
GUEST,
USER,
ADMIN,
FORBID
};
Role getAdminRole (Json::Value const& params, std::string const& strRemoteIp) const;
/** Listening port number for peer connections. */
int peerListeningPort;
/** PROXY listening port number
If this is not zero, it indicates an additional port number on
which we should accept incoming Peer connections that will also
require a PROXY handshake.
The PROXY Protocol:
http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt
*/
int peerPROXYListeningPort;
/** List of Validators entries from rippled.cfg */
std::vector <std::string> validators;
private:
std::string m_rpcIP;
int m_rpcPort; // VFALCO TODO This should be a short.
public:
//--------------------------------------------------------------------------
/** Parameters for the main NodeStore database.
This is 1 or more strings of the form <key>=<value>
@@ -111,39 +300,24 @@ public:
StringPairArray ephemeralNodeDatabase;
/** Parameters for importing an old database in to the current node database.
If this is not empty, then it specifies the key/value parameters for
another node database from which to import all data into the current
node database specified by @ref nodeDatabase.
The format of this string is in the form:
<key>'='<value>['|'<key>'='value]
@see parseDelimitedKeyValueString
*/
StringPairArray importNodeDatabase;
// Listening port number for peer connections.
//
int peerListeningPort;
/** PROXY listening port number
If this is not zero, it indicates an additional port number on
which we should accept incoming Peer connections that will also
require a PROXY handshake.
The PROXY Protocol:
http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt
*/
int peerPROXYListeningPort;
/** List of Validators entries from rippled.cfg */
std::vector <std::string> validators;
/** Path to local validators.txt file from rippled.cfg */
String localValidatorsPath;
//
//--------------------------------------------------------------------------
public:
// Configuration parameters
bool QUIET;
bool TESTNET;
boost::filesystem::path DEBUG_LOGFILE;
bool ELB_SUPPORT; // Support Amazon ELB
@@ -217,71 +391,6 @@ public:
std::string WEBSOCKET_SSL_CHAIN;
std::string WEBSOCKET_SSL_KEY;
//----------------------------------------------------------------------------
//
// VFALCO NOTE Please follow this style for modifying or adding code in the file.
//
public:
/** Get the client or server RPC IP address.
@note The string may not always be in a valid parsable state.
@return A string representing the address.
*/
std::string getRpcIP () const { return m_rpcIP; }
/** Get the client or server RPC port number.
@note The port number may be invalid (out of range or zero)
@return The RPC port number.
*/
int getRpcPort () const { return m_rpcPort; }
/** Set the client or server RPC IP and optional port.
@note The string is not syntax checked.
@param newAddress A string in the format <ip-address>[':'<port-number>]
*/
void setRpcIpAndOptionalPort (std::string const& newAddress);
/** Set the client or server RPC IP.
@note The string is not syntax-checked.
@param newIP A string representing the IP address to use.
*/
void setRpcIP (std::string const& newIP) { m_rpcIP = newIP; }
/** Set the client or server RPC port number.
@note The port number is not range checked.
@param newPort The RPC port number to use.
*/
void setRpcPort (int newPort) { m_rpcPort = newPort; }
/** Convert the RPC/port combination to a readable string.
*/
String const getRpcAddress ()
{
String s;
s << m_rpcIP.c_str () << ":" << m_rpcPort;
return s;
}
/** Determine the level of administrative permission to grant.
*/
enum Role
{
GUEST,
USER,
ADMIN,
FORBID
};
Role getAdminRole (Json::Value const& params, std::string const& strRemoteIp) const;
private:
std::string m_rpcIP;
// VFALCO TODO This should be a short.
int m_rpcPort;
//
//----------------------------------------------------------------------------
public:
// RPC parameters
std::vector<std::string> RPC_ADMIN_ALLOW;
std::string RPC_ADMIN_PASSWORD;
@@ -295,7 +404,6 @@ public:
std::string RPC_SSL_CERT;
std::string RPC_SSL_CHAIN;
std::string RPC_SSL_KEY;
//----------------------------------------------------------------------------
// Path searching
int PATH_SEARCH_OLD;

View File

@@ -28,7 +28,7 @@
namespace ripple
{
#include "functional/ripple_Config.cpp"
#include "functional/Config.cpp"
# include "functional/ripple_LoadFeeTrack.h" // private
#include "functional/ripple_LoadFeeTrack.cpp"
#include "functional/ripple_Job.cpp"

View File

@@ -26,7 +26,7 @@ namespace ripple
// Order matters
# include "functional/ripple_ConfigSections.h"
#include "functional/ripple_Config.h"
#include "functional/Config.h"
#include "functional/ripple_ILoadFeeTrack.h"
# include "functional/ripple_LoadEvent.h"
# include "functional/ripple_LoadMonitor.h"