From 14d14bd7b26917f23919dd5a2135b8525fb096a4 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Tue, 22 Feb 2011 15:51:35 +0100 Subject: [PATCH] Improved configuration classes abstraction and source code - Replaced magic strings by named constants - Renamed ConfigurationReader::read() to ConfigurationReader::parse() according to the interface provided by other methods - Added a method to fill the Configuration object - Sorry for sending too many changes in the same commit =( --- src/config/configuration.cpp | 25 ++++++----- src/config/configuration.h | 13 ++++-- src/config/configurationreader.cpp | 86 ++++++++++++++++++++++++------------ src/config/configurationreader.h | 28 ++++++++++- src/main.cpp | 2 +- 5 files changed, 106 insertions(+), 48 deletions(-) diff --git a/src/config/configuration.cpp b/src/config/configuration.cpp index 9e1ec28..3b682d6 100644 --- a/src/config/configuration.cpp +++ b/src/config/configuration.cpp @@ -1,12 +1,16 @@ -#include - #include "configuration.h" using namespace std; -using namespace boost::program_options; + +//----------------------------------------------------------------------------- +// Configuration +//----------------------------------------------------------------------------- Configuration::Configuration() : - limit_to_notify( 0 ) + config_file_name( "" ), + limit_to_notify( 0 ), + MIN_LIMIT_TO_NOTIFY( 0 ), + MAX_LIMIT_TO_NOTIFY( 50 ) { } @@ -14,15 +18,14 @@ Configuration::~Configuration() { } -bool Configuration::read( variables_map vm ) +string Configuration::get_config_file_name() const { - if ( vm.count( "limit-to-notify" ) ) - { - limit_to_notify = vm[ "limit-to-notify" ].as (); - cout << "limit-to-notify:" << limit_to_notify << endl; - } + return config_file_name; +} - return true; +void Configuration::set_config_file_name( std::string config_file_name ) +{ + this->config_file_name = config_file_name; } uint32_t Configuration::get_limit_to_notify() const diff --git a/src/config/configuration.h b/src/config/configuration.h index 78c9c2f..63c78fb 100644 --- a/src/config/configuration.h +++ b/src/config/configuration.h @@ -3,10 +3,10 @@ #include #include -#include -#include -#include "host.h" +//----------------------------------------------------------------------------- +// Configuration +//----------------------------------------------------------------------------- class Configuration { @@ -14,14 +14,19 @@ public: Configuration(); virtual ~Configuration(); - bool read( boost::program_options::variables_map vm ); + std::string get_config_file_name() const; + void set_config_file_name( std::string config_file_name ); uint32_t get_limit_to_notify() const; void set_limit_to_notify( uint32_t limit_to_notify ); private: + std::string config_file_name; uint32_t limit_to_notify; + const uint32_t MIN_LIMIT_TO_NOTIFY; + const uint32_t MAX_LIMIT_TO_NOTIFY; + }; #endif /* CONFIGURATION_H */ diff --git a/src/config/configurationreader.cpp b/src/config/configurationreader.cpp index 648a806..9fbaea1 100644 --- a/src/config/configurationreader.cpp +++ b/src/config/configurationreader.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #include "configurationreader.h" @@ -7,9 +7,24 @@ using namespace std; using namespace boost::program_options; +//----------------------------------------------------------------------------- +// ConfigurationReader +//----------------------------------------------------------------------------- + ConfigurationReader::ConfigurationReader() : configuration(), - default_config_file_name( "pingcheck.cfg" ) + VERSION_CMD_STR( "version" ), + VERSION_CMD_DESC( "Print the version string and exit." ), + HELP_CMD_STR( "help" ), + HELP_CMD_DESC( "Print this help and exit." ), + DEFAULT_CONFIG_FILE_NAME( "pingcheck.cfg" ), + CONFIG_FILE_CMD_STR( "config-file" ), + CONFIG_FILE_CMD_DESC( "Name of the configuration file." ), + DEFAULT_LIMIT_TO_NOTIFY( 4 ), + LIMIT_TO_NOTIFY_CMD_STR( "limit-to-notify" ), + LIMIT_TO_NOTIFY_CMD_DESC( "Limit of host that have to be down in order to notify." ), + HOST_CMD_STR( "host" ), + HOST_CMD_DESC( "Host address" ) { } @@ -17,21 +32,13 @@ ConfigurationReader::~ConfigurationReader() { } -// A helper function to simplify the main part. -template - ostream& operator<<( ostream& os, const vector& v ) - { - copy( v.begin(), v.end(), ostream_iterator ( cout, " " ) ); - return os; - } - options_description ConfigurationReader::get_generic_options() const { options_description options( "Generic options" ); options.add_options() - ( "version,v", "Print the version string and exit." ) - ( "help,h", "Print this help and exit." ) - ( "config-file", value()->default_value( default_config_file_name ), "Name of the configuration file." ); + ( VERSION_CMD_STR.c_str(), VERSION_CMD_DESC.c_str() ) + ( HELP_CMD_STR.c_str(), HELP_CMD_DESC.c_str() ) + ( CONFIG_FILE_CMD_STR.c_str(), value()->default_value( DEFAULT_CONFIG_FILE_NAME ), CONFIG_FILE_CMD_DESC.c_str() ); return options; } @@ -40,8 +47,8 @@ options_description ConfigurationReader::get_configuration_options() const { options_description options( "Configuration" ); options.add_options() - ( "limit-to-notify", value()->default_value( 4 ), "Limit of host that have to be down in order to notify." ) - ( "host", value< vector >(), "Host address" ); + ( LIMIT_TO_NOTIFY_CMD_STR.c_str(), value()->default_value( DEFAULT_LIMIT_TO_NOTIFY ), LIMIT_TO_NOTIFY_CMD_DESC.c_str() ) + ( HOST_CMD_STR.c_str(), value< vector >(), HOST_CMD_DESC.c_str() ); return options; } @@ -64,24 +71,25 @@ bool ConfigurationReader::parse_command_line( visible.add( generic ).add( config ); positional_options_description p; - p.add( "host", -1 ); + p.add( HOST_CMD_STR.c_str(), -1 ); store( command_line_parser( argc, argv ). options( cmdline_options ). positional( p ).run(), vm ); notify( vm ); - if ( vm.count( "help" ) ) + if ( vm.count( HELP_CMD_STR ) ) { cout << visible << endl; return false; } - if ( vm.count( "version" ) ) + if ( vm.count( VERSION_CMD_STR ) ) { - cout << "pingcheck version " << VERSION_STRING << endl; + cout << PROJECT_NAME << " version " << VERSION_STRING << endl; return false; } + } catch ( exception& e ) { @@ -94,11 +102,9 @@ bool ConfigurationReader::parse_command_line( bool ConfigurationReader::parse_configuration_file( variables_map& vm ) { - string config_file_name; - if ( vm.count( "config-file" ) ) - { - config_file_name = vm[ "config-file" ].as (); - } + string config_file_name = ""; + if ( fill_configuration( vm ) ) + config_file_name = configuration.get_config_file_name(); return parse_configuration_file( config_file_name, vm ); } @@ -127,7 +133,10 @@ bool ConfigurationReader::parse_configuration_file( return true; } -bool ConfigurationReader::read( const int argc, char* argv[] ) +bool ConfigurationReader::parse( + const int argc, + char* argv[] +) { variables_map vm; bool command_line_parsed = parse_command_line( argc, argv, vm ); @@ -136,15 +145,34 @@ bool ConfigurationReader::read( const int argc, char* argv[] ) bool parser_success = command_line_parsed && configuration_file_parsed; if (parser_success) { - return configuration.read( vm ); + return fill_configuration( vm ); } - else + + return false; +} + +bool ConfigurationReader::fill_configuration( const variables_map& vm ) +{ + if ( vm.count( CONFIG_FILE_CMD_STR ) ) { - return false; + string config_file_name = vm[ CONFIG_FILE_CMD_STR ].as (); + configuration.set_config_file_name( config_file_name ); + + cout << CONFIG_FILE_CMD_STR << "=" << config_file_name << endl; } + + if ( vm.count( LIMIT_TO_NOTIFY_CMD_STR ) ) + { + uint32_t limit_to_notify = vm[ LIMIT_TO_NOTIFY_CMD_STR ].as (); + configuration.set_limit_to_notify( limit_to_notify ); + + cout << LIMIT_TO_NOTIFY_CMD_STR << "=" << limit_to_notify << endl; + } + + return true; } -Configuration ConfigurationReader::getConfiguration() const +Configuration ConfigurationReader::get_configuration() const { return configuration; } diff --git a/src/config/configurationreader.h b/src/config/configurationreader.h index 969cfe2..f8ff57c 100644 --- a/src/config/configurationreader.h +++ b/src/config/configurationreader.h @@ -6,18 +6,26 @@ #include "configuration.h" +//----------------------------------------------------------------------------- +// ConfigurationReader +//----------------------------------------------------------------------------- + class ConfigurationReader { public: ConfigurationReader(); virtual ~ConfigurationReader(); - bool read( const int argc, char* argv[] ); - Configuration getConfiguration() const; + bool parse( + const int argc, + char* argv[] + ); + Configuration get_configuration() const; private: boost::program_options::options_description get_generic_options() const; boost::program_options::options_description get_configuration_options() const; + bool parse_command_line( const int argc, char* argv[], @@ -28,10 +36,24 @@ private: const std::string& config_file_name, boost::program_options::variables_map& vm ); + bool fill_configuration( const boost::program_options::variables_map& vm ); private: Configuration configuration; - const std::string default_config_file_name; + + const std::string VERSION_CMD_STR; + const std::string VERSION_CMD_DESC; + const std::string HELP_CMD_STR; + const std::string HELP_CMD_DESC; + const std::string DEFAULT_CONFIG_FILE_NAME; + const std::string CONFIG_FILE_CMD_STR; + const std::string CONFIG_FILE_CMD_DESC; + const uint32_t DEFAULT_LIMIT_TO_NOTIFY; + const std::string LIMIT_TO_NOTIFY_CMD_STR; + const std::string LIMIT_TO_NOTIFY_CMD_DESC; + const std::string HOST_CMD_STR; + const std::string HOST_CMD_DESC; + }; #endif /* CONFIGURATIONREADER_H */ diff --git a/src/main.cpp b/src/main.cpp index 7613610..526587d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,7 @@ int main( int argc, char* argv[] ) { // sends the program command line to be parsed by the configuration reader ConfigurationReader config_reader; - bool read_success = config_reader.read( argc, argv ); + bool read_success = config_reader.parse( argc, argv ); if ( read_success ) { try -- 1.7.1