From 1a977b421aa3fc2e253b1300b2f88ba239fd8d94 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Fri, 23 Sep 2011 19:02:28 -0300 Subject: [PATCH] Bring aboard hosts section configuration option classes. --- src/config/option/hostconfigurationoption.cpp | 94 +++++++++++++++++++++++ src/config/option/hostconfigurationoption.h | 74 ++++++++++++++++++ src/config/option/hostnameoption.cpp | 101 +++++++++++++++++++++++++ src/config/option/hostnameoption.h | 52 +++++++++++++ src/config/option/hostpingintervaloption.cpp | 99 ++++++++++++++++++++++++ src/config/option/hostpingintervaloption.h | 52 +++++++++++++ src/config/option/hostpingprotocoloption.cpp | 100 ++++++++++++++++++++++++ src/config/option/hostpingprotocoloption.h | 52 +++++++++++++ src/config/option/hostportoption.cpp | 99 ++++++++++++++++++++++++ src/config/option/hostportoption.h | 52 +++++++++++++ 10 files changed, 775 insertions(+), 0 deletions(-) create mode 100644 src/config/option/hostconfigurationoption.cpp create mode 100644 src/config/option/hostconfigurationoption.h create mode 100644 src/config/option/hostnameoption.cpp create mode 100644 src/config/option/hostnameoption.h create mode 100644 src/config/option/hostpingintervaloption.cpp create mode 100644 src/config/option/hostpingintervaloption.h create mode 100644 src/config/option/hostpingprotocoloption.cpp create mode 100644 src/config/option/hostpingprotocoloption.h create mode 100644 src/config/option/hostportoption.cpp create mode 100644 src/config/option/hostportoption.h diff --git a/src/config/option/hostconfigurationoption.cpp b/src/config/option/hostconfigurationoption.cpp new file mode 100644 index 0000000..f08ecee --- /dev/null +++ b/src/config/option/hostconfigurationoption.cpp @@ -0,0 +1,94 @@ +/* + 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/hostconfigurationoption.h" + +#include +#include + +#include +#include + +#include + +#include "config/option/hostpingintervaloption.h" +#include "config/option/hostpingprotocoloption.h" +#include "config/option/hostportoption.h" +#include "config/option/hostnameoption.h" + +using namespace std; +using boost::program_options::value; +using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// HostConfigurationOption +//----------------------------------------------------------------------------- + +const size_t invalid_host_count = std::numeric_limits::max(); + +HostConfigurationOption::HostConfigurationOption( + const char *command_string, + const char *command_description +) : + ConfigurationOption( command_string, command_description ), + HostsCount( invalid_host_count ) +{ +} + +HostConfigurationOption::HostConfigurationOption( + const char *command_string, + const boost::program_options::value_semantic *semantic, + const char *command_description +) : + ConfigurationOption( command_string, semantic, command_description ), + HostsCount( invalid_host_count ) +{ +} + +HostConfigurationOption::~HostConfigurationOption() +{ +} + +/** + * @brief Get the amount of hosts parsed. + * + * @return The amount of host sections parsed. + */ +size_t HostConfigurationOption::get_hosts_count() const +{ + // ensure set_hosts_count was called first + BOOST_ASSERT( HostsCount != invalid_host_count ); + + return HostsCount; +} + +/** + * @brief Set the amount of hosts parsed. + * + * @param hosts_count The amount of host sections parsed. + */ +void HostConfigurationOption::set_hosts_count( const size_t hosts_count ) +{ + // ensure we do not use the "invalid flag" accidently + BOOST_ASSERT( hosts_count != invalid_host_count ); + + HostsCount = hosts_count; +} diff --git a/src/config/option/hostconfigurationoption.h b/src/config/option/hostconfigurationoption.h new file mode 100644 index 0000000..021194c --- /dev/null +++ b/src/config/option/hostconfigurationoption.h @@ -0,0 +1,74 @@ +/* + 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 HOST_CONFIGURATION_OPTION_H +#define HOST_CONFIGURATION_OPTION_H + +#include + +#include + +#include + +#include "config/option/configurationoption.h" + +//----------------------------------------------------------------------------- +// HostConfigurationOption +//----------------------------------------------------------------------------- + +/** + * @brief This class represents the "host" section configuration option. + */ +class HostConfigurationOption : public ConfigurationOption +{ +public: + + HostConfigurationOption( + const char *command_string, + const char *command_description + ); + HostConfigurationOption( + const char *command_string, + const boost::program_options::value_semantic *semantic, + const char *command_description + ); + virtual ~HostConfigurationOption(); + + std::size_t get_hosts_count() const; + void set_hosts_count( const std::size_t hosts_count ); + +private: + std::size_t HostsCount; + +}; + +//----------------------------------------------------------------------------- +// HostConfigurationOptionItem +//----------------------------------------------------------------------------- + +typedef boost::shared_ptr< HostConfigurationOption > HostConfigurationOptionItem; + +//----------------------------------------------------------------------------- +// HostConfigurationOptionList +//----------------------------------------------------------------------------- + +typedef std::vector< HostConfigurationOptionItem > HostConfigurationOptionList; + +#endif // HOST_CONFIGURATION_OPTION_H diff --git a/src/config/option/hostnameoption.cpp b/src/config/option/hostnameoption.cpp new file mode 100644 index 0000000..101861b --- /dev/null +++ b/src/config/option/hostnameoption.cpp @@ -0,0 +1,101 @@ +/* + 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/hostnameoption.h" + +#include +#include + +#include +#include + +#include + +using namespace std; +using boost::program_options::value; +using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// HostNameOption +//----------------------------------------------------------------------------- + +HostNameOption::HostNameOption() : + HostConfigurationOption( + "host.name", + value< vector >(), + "Host address." + ) +{ +} + +HostNameOption::~HostNameOption() +{ +} + +bool HostNameOption::validate( const variables_map& vm ) const +{ + string command_name = get_command_string(); + bool is_valid = ( vm.count( command_name ) > 0 ); + + return is_valid; +} + +bool HostNameOption::parse( + const variables_map& vm, + Configuration *configuration +) +{ + size_t hosts_names_count = 0; + bool parsed_success = false; + + // [host] name + if ( vm.count( get_command_string() ) > 0 ) + { + HostList hosts_list; + + vector hosts_names = vm[ get_command_string() ].as< vector > (); + BOOST_FOREACH( string host_name, hosts_names ) + { + BOOST_ASSERT( !host_name.empty() ); + + HostItem host_item( new Host( host_name ) ); + hosts_list.push_back( host_item ); + + GlobalLogger.info() << get_command_string() << "=" + << host_name << endl; + } + + configuration->set_hosts( hosts_list ); + + hosts_names_count = hosts_names.size(); + parsed_success = true; + } + + set_hosts_count( hosts_names_count ); + + // TODO receive the host_down_limit by parameter, use a std::map to find this configuration inside the Options container + // TODO BOOST_ASSERT( hosts_names_count >= static_cast( host_down_limit ) ); +// BOOST_ASSERT( hosts_names_count == host_port_count ); +// BOOST_ASSERT( hosts_names_count == host_ping_protocol_count ); +// BOOST_ASSERT( hosts_names_count == hosts_interval_count ); + + return parsed_success; +} diff --git a/src/config/option/hostnameoption.h b/src/config/option/hostnameoption.h new file mode 100644 index 0000000..ba76169 --- /dev/null +++ b/src/config/option/hostnameoption.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 HOST_NAME_OPTION_H +#define HOST_NAME_OPTION_H + +#include + +#include "config/option/hostconfigurationoption.h" + +//----------------------------------------------------------------------------- +// HostNameOption +//----------------------------------------------------------------------------- + +/** + * @brief This class represents the "[host] name" section configuration option. + */ +class HostNameOption : public HostConfigurationOption +{ +public: + HostNameOption(); + virtual ~HostNameOption(); + + virtual bool validate( + const boost::program_options::variables_map &vm + ) const; + + virtual bool parse( + const boost::program_options::variables_map &vm, + Configuration *configuration + ); + +}; + +#endif // HOST_NAME_OPTION_H diff --git a/src/config/option/hostpingintervaloption.cpp b/src/config/option/hostpingintervaloption.cpp new file mode 100644 index 0000000..a1b6ecc --- /dev/null +++ b/src/config/option/hostpingintervaloption.cpp @@ -0,0 +1,99 @@ +/* + 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/hostpingintervaloption.h" + +#include + +#include +#include + +#include + +using namespace std; +using boost::program_options::value; +using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// HostPingIntervalOption +//----------------------------------------------------------------------------- + +HostPingIntervalOption::HostPingIntervalOption() : + HostConfigurationOption( + "host.interval", + value< vector >(),//->default_value( 60 ), // 60 seconds + "Interval between each ping to the host." + ) +{ +} + +HostPingIntervalOption::~HostPingIntervalOption() +{ +} + +bool HostPingIntervalOption::validate( const variables_map& vm ) const +{ + bool is_valid = ( vm.count( this->get_command_string() ) > 0 ); + + return is_valid; +} + +bool HostPingIntervalOption::parse( + const variables_map& vm, + Configuration *configuration +) +{ + size_t hosts_interval_count = 0; + bool parsed_success = false; + + // [host] interval + if ( vm.count( get_command_string() ) > 0 ) + { + HostList hosts_list = configuration->get_hosts(); + HostList::iterator hosts_it = hosts_list.begin(); + + vector hosts_intervals = vm[ get_command_string() ].as< vector >(); + BOOST_FOREACH( int host_interval_in_sec, hosts_intervals ) + { + BOOST_ASSERT( 0 < host_interval_in_sec ); + + HostItem host_item = *hosts_it; + host_item->set_interval_in_sec( host_interval_in_sec ); + ++hosts_it; + + GlobalLogger.info() << get_command_string() << "=" + << host_interval_in_sec << endl; + } + + hosts_interval_count = hosts_intervals.size(); + parsed_success = true; + } + + set_hosts_count( hosts_interval_count ); + + // TODO receive the host_down_limit by parameter, use a std::map to find this configuration inside the Options container + // TODO BOOST_ASSERT( hosts_names_count >= static_cast( host_down_limit ) ); +// BOOST_ASSERT( hosts_names_count == host_port_count ); +// BOOST_ASSERT( hosts_names_count == host_ping_protocol_count ); +// BOOST_ASSERT( hosts_names_count == hosts_interval_count ); + + return parsed_success; +} diff --git a/src/config/option/hostpingintervaloption.h b/src/config/option/hostpingintervaloption.h new file mode 100644 index 0000000..39a4329 --- /dev/null +++ b/src/config/option/hostpingintervaloption.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 HOST_PING_INTERVAL_OPTION_H +#define HOST_PING_INTERVAL_OPTION_H + +#include + +#include "config/option/hostconfigurationoption.h" + +//----------------------------------------------------------------------------- +// HostPingIntervalOption +//----------------------------------------------------------------------------- + +/** + * @brief This class represents the "[host] interval" section configuration option. + */ +class HostPingIntervalOption : public HostConfigurationOption +{ +public: + HostPingIntervalOption(); + virtual ~HostPingIntervalOption(); + + virtual bool validate( + const boost::program_options::variables_map &vm + ) const; + + virtual bool parse( + const boost::program_options::variables_map &vm, + Configuration *configuration + ); + +}; + +#endif // HOST_PING_INTERVAL_OPTION_H diff --git a/src/config/option/hostpingprotocoloption.cpp b/src/config/option/hostpingprotocoloption.cpp new file mode 100644 index 0000000..5993aaa --- /dev/null +++ b/src/config/option/hostpingprotocoloption.cpp @@ -0,0 +1,100 @@ +/* + 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/hostpingprotocoloption.h" + +#include + +#include +#include + +#include + +using namespace std; +using boost::program_options::value; +using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// HostPingProtocolOption +//----------------------------------------------------------------------------- + +HostPingProtocolOption::HostPingProtocolOption() : + HostConfigurationOption( + "host.ping-protocol", + value< vector >(), + "Defines which protocol will be used to ping the destination." + ) +{ +} + +HostPingProtocolOption::~HostPingProtocolOption() +{ +} + +bool HostPingProtocolOption::validate( const variables_map& vm ) const +{ + bool is_valid = ( vm.count( this->get_command_string() ) > 0 ); + + return is_valid; +} + +bool HostPingProtocolOption::parse( + const variables_map& vm, + Configuration *configuration +) +{ + size_t host_ping_protocol_count = 0; + bool parsed_success = false; + + // [host] ping-protocol + if ( vm.count( get_command_string() ) > 0 ) + { + HostList hosts_list = configuration->get_hosts(); + HostList::iterator hosts_it = hosts_list.begin(); + + vector hosts_protocols = vm[ get_command_string() ].as< vector >(); + BOOST_FOREACH( string protocol_string, hosts_protocols ) + { + BOOST_ASSERT( !protocol_string.empty() ); + + HostItem host_item = *hosts_it; + PingProtocol host_protocol = get_ping_protocol_from_string( protocol_string ); + host_item->set_ping_protocol( host_protocol ); + ++hosts_it; + + GlobalLogger.info() << get_command_string() << "=" + << protocol_string << endl; + } + + host_ping_protocol_count = hosts_protocols.size(); + parsed_success = true; + } + + set_hosts_count( host_ping_protocol_count ); + + // TODO receive the host_down_limit by parameter, use a std::map to find this configuration inside the Options container + // TODO BOOST_ASSERT( hosts_names_count >= static_cast( host_down_limit ) ); +// BOOST_ASSERT( hosts_names_count == host_port_count ); +// BOOST_ASSERT( hosts_names_count == host_ping_protocol_count ); +// BOOST_ASSERT( hosts_names_count == hosts_interval_count ); + + return parsed_success; +} diff --git a/src/config/option/hostpingprotocoloption.h b/src/config/option/hostpingprotocoloption.h new file mode 100644 index 0000000..0a500f4 --- /dev/null +++ b/src/config/option/hostpingprotocoloption.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 HOST_PING_PROTOCOL_OPTION_H +#define HOST_PING_PROTOCOL_OPTION_H + +#include + +#include "config/option/hostconfigurationoption.h" + +//----------------------------------------------------------------------------- +// HostPingProtocolOption +//----------------------------------------------------------------------------- + +/** + * @brief This class represents the "[host] port" section configuration option. + */ +class HostPingProtocolOption : public HostConfigurationOption +{ +public: + HostPingProtocolOption(); + virtual ~HostPingProtocolOption(); + + virtual bool validate( + const boost::program_options::variables_map &vm + ) const; + + virtual bool parse( + const boost::program_options::variables_map &vm, + Configuration *configuration + ); + +}; + +#endif // HOST_PING_PROTOCOL_OPTION_H diff --git a/src/config/option/hostportoption.cpp b/src/config/option/hostportoption.cpp new file mode 100644 index 0000000..87c7894 --- /dev/null +++ b/src/config/option/hostportoption.cpp @@ -0,0 +1,99 @@ +/* + 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/hostportoption.h" + +#include +#include + +#include +#include + +#include + +using namespace std; +using boost::program_options::value; +using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// HostPortOption +//----------------------------------------------------------------------------- + +HostPortOption::HostPortOption() : + HostConfigurationOption( + "host.port", + value< vector >(),//->default_value( 80 ), // HTTP port;, + "Host port number." + ) +{ +} + +HostPortOption::~HostPortOption() +{ +} + +bool HostPortOption::validate( const variables_map& vm ) const +{ + bool is_valid = ( vm.count( this->get_command_string() ) > 0 ); + + return is_valid; +} + +bool HostPortOption::parse( + const variables_map& vm, + Configuration *configuration +) +{ + size_t host_port_count = 0; + bool parsed_success = false; + + // [host] port + if ( vm.count( get_command_string() ) > 0 ) + { + HostList hosts_list = configuration->get_hosts(); + HostList::iterator hosts_it = hosts_list.begin(); + + vector hosts_ports = vm[ get_command_string() ].as< vector >(); + BOOST_FOREACH( int host_port, hosts_ports ) + { + BOOST_ASSERT( ( 0 <= host_port ) && ( host_port <= numeric_limits::max() ) ); + + HostItem host_item = *hosts_it; + host_item->set_port( static_cast(host_port) ); + ++hosts_it; + + GlobalLogger.info() << get_command_string() << "=" << host_port << endl; + } + + host_port_count = hosts_ports.size(); + parsed_success = true; + } + + set_hosts_count( host_port_count ); + + // TODO receive the host_down_limit by parameter, use a std::map to find this configuration inside the Options container + // TODO BOOST_ASSERT( hosts_names_count >= static_cast( host_down_limit ) ); +// BOOST_ASSERT( hosts_names_count == host_port_count ); +// BOOST_ASSERT( hosts_names_count == host_ping_protocol_count ); +// BOOST_ASSERT( hosts_names_count == hosts_interval_count ); + + return parsed_success; +} diff --git a/src/config/option/hostportoption.h b/src/config/option/hostportoption.h new file mode 100644 index 0000000..9665949 --- /dev/null +++ b/src/config/option/hostportoption.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 HOST_PORT_OPTION_H +#define HOST_PORT_OPTION_H + +#include + +#include "config/option/hostconfigurationoption.h" + +//----------------------------------------------------------------------------- +// HostPortOption +//----------------------------------------------------------------------------- + +/** + * @brief This class represents the "[host] port" section configuration option. + */ +class HostPortOption : public HostConfigurationOption +{ +public: + HostPortOption(); + virtual ~HostPortOption(); + + virtual bool validate( + const boost::program_options::variables_map &vm + ) const; + + virtual bool parse( + const boost::program_options::variables_map &vm, + Configuration *configuration + ); + +}; + +#endif // HOST_PORT_OPTION_H -- 1.7.1