From adc78821807f7ddb73ce4592737ea0ba7ba91293 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Thu, 6 Oct 2011 07:58:32 -0300 Subject: [PATCH] Bring aboard "version", "daemon" and "config-file" configuration option classes. --- src/CMakeLists.txt | 3 + src/config/option/configfileoption.cpp | 76 ++++++++++++++++++++++++++++++++ src/config/option/configfileoption.h | 52 ++++++++++++++++++++++ src/config/option/daemonoption.cpp | 66 +++++++++++++++++++++++++++ src/config/option/daemonoption.h | 52 ++++++++++++++++++++++ src/config/option/versionoption.cpp | 69 +++++++++++++++++++++++++++++ src/config/option/versionoption.h | 52 ++++++++++++++++++++++ 7 files changed, 370 insertions(+), 0 deletions(-) create mode 100644 src/config/option/configfileoption.cpp create mode 100644 src/config/option/configfileoption.h create mode 100644 src/config/option/daemonoption.cpp create mode 100644 src/config/option/daemonoption.h create mode 100644 src/config/option/versionoption.cpp create mode 100644 src/config/option/versionoption.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5f7562a..fae4cf2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,6 +38,8 @@ set(SOURCES config/configurationoptions.cpp config/configurationreader.cpp config/host.cpp + config/option/daemonoption.cpp + config/option/configfileoption.cpp config/option/configurationoption.cpp config/option/hostconfigurationoption.cpp config/option/hostpingintervaloption.cpp @@ -51,6 +53,7 @@ set(SOURCES config/option/pingfaillimitoption.cpp config/option/sourcenetworkinterfaceoption.cpp config/option/statusnotifiercmdoption.cpp + config/option/versionoption.cpp dns/dnsresolver.cpp dns/hostaddress.cpp dns/timetolive.cpp diff --git a/src/config/option/configfileoption.cpp b/src/config/option/configfileoption.cpp new file mode 100644 index 0000000..2a0eea1 --- /dev/null +++ b/src/config/option/configfileoption.cpp @@ -0,0 +1,76 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#include "config/option/configfileoption.h" + +#include + +#include + +using namespace std; +using boost::program_options::value; +using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// ConfigFileOption +//----------------------------------------------------------------------------- + +/** + * @brief Default constructor. + */ +ConfigFileOption::ConfigFileOption() : + ConfigurationOption( + "config-file", + value()->default_value( "/etc/pingcheck.conf" ), + "Name of the configuration file." + ) +{ +} + +/** + * @brief Destructor. + */ +ConfigFileOption::~ConfigFileOption() +{ +} + +bool ConfigFileOption::validate( const variables_map & ) const +{ + return true; +} + +bool ConfigFileOption::parse( + const variables_map& vm, + Configuration *configuration +) +{ + // config-file + if ( 1 <= vm.count( get_command_string() ) ) + { + string config_file_name = vm[ get_command_string() ].as (); + configuration->set_config_file_name( config_file_name ); + + GlobalLogger.info() << get_command_string() << "=" << config_file_name << endl; + return true; + } + + return false; +} diff --git a/src/config/option/configfileoption.h b/src/config/option/configfileoption.h new file mode 100644 index 0000000..6900b43 --- /dev/null +++ b/src/config/option/configfileoption.h @@ -0,0 +1,52 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#ifndef CONFIG_FILE_OPTION_H +#define CONFIG_FILE_OPTION_H + +#include + +#include "config/option/configurationoption.h" + +//----------------------------------------------------------------------------- +// ConfigFileOption +//----------------------------------------------------------------------------- + +/** + * @brief Configuration file name option. + */ +class ConfigFileOption : public ConfigurationOption +{ +public: + ConfigFileOption(); + virtual ~ConfigFileOption(); + + virtual bool validate( + const boost::program_options::variables_map &vm + ) const; + + virtual bool parse( + const boost::program_options::variables_map &vm, + Configuration *configuration + ); + +}; + +#endif // CONFIG_FILE_OPTION_H diff --git a/src/config/option/daemonoption.cpp b/src/config/option/daemonoption.cpp new file mode 100644 index 0000000..655e7d2 --- /dev/null +++ b/src/config/option/daemonoption.cpp @@ -0,0 +1,66 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#include "config/option/daemonoption.h" + +#include + +#include + +using namespace std; +using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// DaemonOption +//----------------------------------------------------------------------------- + +/** + * @brief Default constructor. + */ +DaemonOption::DaemonOption() : + ConfigurationOption( "daemon", "Run the evil in background." ) +{ +} + +/** + * @brief Destructor. + */ +DaemonOption::~DaemonOption() +{ +} + +bool DaemonOption::validate( const variables_map & ) const +{ + return true; +} + +bool DaemonOption::parse( + const variables_map& vm, + Configuration *configuration +) +{ + // daemon + bool have_daemon = ( 1<= vm.count( get_command_string() ) ); + configuration->set_daemon( have_daemon ); + GlobalLogger.info() << get_command_string() << "=" << have_daemon << endl; + + return false; +} diff --git a/src/config/option/daemonoption.h b/src/config/option/daemonoption.h new file mode 100644 index 0000000..9254207 --- /dev/null +++ b/src/config/option/daemonoption.h @@ -0,0 +1,52 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#ifndef DAEMON_OPTION_H +#define DAEMON_OPTION_H + +#include + +#include "config/option/configurationoption.h" + +//----------------------------------------------------------------------------- +// DaemonOption +//----------------------------------------------------------------------------- + +/** + * @brief Daemon option. + */ +class DaemonOption : public ConfigurationOption +{ +public: + DaemonOption(); + virtual ~DaemonOption(); + + virtual bool validate( + const boost::program_options::variables_map &vm + ) const; + + virtual bool parse( + const boost::program_options::variables_map &vm, + Configuration *configuration + ); + +}; + +#endif // DAEMON_OPTION_H diff --git a/src/config/option/versionoption.cpp b/src/config/option/versionoption.cpp new file mode 100644 index 0000000..7523baa --- /dev/null +++ b/src/config/option/versionoption.cpp @@ -0,0 +1,69 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#include "config/option/versionoption.h" + +#include + +#include + +using namespace std; +using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// VersionOption +//----------------------------------------------------------------------------- + +/** + * @brief Default constructor. + */ +VersionOption::VersionOption() : + ConfigurationOption( "version", "Print the version string and exit." ) +{ +} + +/** + * @brief Destructor. + */ +VersionOption::~VersionOption() +{ +} + +bool VersionOption::validate( const variables_map & ) const +{ + return true; +} + +bool VersionOption::parse( + const variables_map& vm, + Configuration * +) +{ + // version + if ( 1 <= vm.count( get_command_string() ) ) + { + // TODO GlobalOutput::print( version ) + cout << PROJECT_NAME << " version " << VERSION_STRING << endl; + return true; + } + + return false; +} diff --git a/src/config/option/versionoption.h b/src/config/option/versionoption.h new file mode 100644 index 0000000..2868d3b --- /dev/null +++ b/src/config/option/versionoption.h @@ -0,0 +1,52 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#ifndef VERSION_OPTION_H +#define VERSION_OPTION_H + +#include + +#include "config/option/configurationoption.h" + +//----------------------------------------------------------------------------- +// VersionOption +//----------------------------------------------------------------------------- + +/** + * @brief Version option. + */ +class VersionOption : public ConfigurationOption +{ +public: + VersionOption(); + virtual ~VersionOption(); + + virtual bool validate( + const boost::program_options::variables_map &vm + ) const; + + virtual bool parse( + const boost::program_options::variables_map &vm, + Configuration *configuration + ); + +}; + +#endif // VERSION_OPTION_H -- 1.7.1