From f1284dcbaa750c1ffb517ed8e2e6a0a051a36057 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Wed, 7 Mar 2012 07:14:19 -0300 Subject: [PATCH] Configuration: added the ping protocol fallback count. - This item tells how many times it must ping using one protocol before use the other. --- src/config/host.cpp | 19 ++++++++++ src/config/host.h | 5 +++ src/config/option/hostpingprotocoloption.cpp | 47 +++++++++++++++++++------ src/config/option/hostpingprotocoloption.h | 6 ++- 4 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/config/host.cpp b/src/config/host.cpp index 5bb0bd9..15b8179 100644 --- a/src/config/host.cpp +++ b/src/config/host.cpp @@ -38,6 +38,7 @@ Host::Host( string address ) : Address( address ), Port( 0 ), ProtocolList(), + ProtocolFallbackCount( 0 ), IntervalInSec( 0 ) { } @@ -106,6 +107,24 @@ void Host::set_ping_protocol_list( const PingProtocolList &ping_protocol_list ) } /** + * @return The amount of pings to perform before change the protocol. + */ +int Host::get_ping_protocol_fallback_count() const +{ + return ProtocolFallbackCount; +} + +/** + * @param protocol_fallback_count The amount of pings to perform before change the protocol. + */ +void Host::set_ping_protocol_fallback_count( const int protocol_fallback_count ) +{ + BOOST_ASSERT( ( 0 <= protocol_fallback_count ) && ( protocol_fallback_count < numeric_limits::max() ) ); + + this->ProtocolFallbackCount = protocol_fallback_count; +} + +/** * @return The interval between each ping to the host. */ int Host::get_interval_in_sec() const diff --git a/src/config/host.h b/src/config/host.h index 8e5969f..58586b1 100644 --- a/src/config/host.h +++ b/src/config/host.h @@ -51,6 +51,9 @@ public: PingProtocolList get_ping_protocol_list() const; void set_ping_protocol_list( const PingProtocolList &ping_protocol ); + int get_ping_protocol_fallback_count() const; + void set_ping_protocol_fallback_count( const int protocol_fallback_count ); + int get_interval_in_sec() const; void set_interval_in_sec( const int interval_in_sec ); @@ -61,6 +64,8 @@ private: uint16_t Port; /// The list of protocols used to ping PingProtocolList ProtocolList; + /// The amount of pings to perform before change the protocol. + int ProtocolFallbackCount; /// The interval between each ping to the host int IntervalInSec; diff --git a/src/config/option/hostpingprotocoloption.cpp b/src/config/option/hostpingprotocoloption.cpp index aac7025..6f1e08d 100644 --- a/src/config/option/hostpingprotocoloption.cpp +++ b/src/config/option/hostpingprotocoloption.cpp @@ -26,6 +26,7 @@ #include #include +#include #include @@ -42,7 +43,7 @@ HostPingProtocolOption::HostPingProtocolOption() : HostConfigurationOption( "host.ping-protocol", value< vector >(), - "Defines which protocol will be used to ping the destination." + "Defines which protocols will be used to ping the destination." ) { } @@ -77,8 +78,11 @@ bool HostPingProtocolOption::parse( BOOST_ASSERT( !protocols_string.empty() ); HostItem host_item = *hosts_list_iterator; - PingProtocolList host_protocols = parse_protocol_list( protocols_string ); + PingProtocolList host_protocols; + int protocol_fallback_count = 0; + parse_protocol_list( protocols_string, &host_protocols, &protocol_fallback_count ); host_item->set_ping_protocol_list( host_protocols ); + host_item->set_ping_protocol_fallback_count( protocol_fallback_count ); ++hosts_list_iterator; GlobalLogger.info() << get_command_string() << "=" @@ -95,13 +99,16 @@ bool HostPingProtocolOption::parse( return parsed_success; } -PingProtocolList HostPingProtocolOption::parse_protocol_list( - const std::string &protocol_list_string +void HostPingProtocolOption::parse_protocol_list( + const std::string &protocol_list_string, + PingProtocolList *protocol_list, + int *protocol_fallback_count + ) const { BOOST_ASSERT( !protocol_list_string.empty() ); - PingProtocolList protocol_list; + size_t protocol_index = 0; string protocol_string; istringstream iss( protocol_list_string ); @@ -109,15 +116,31 @@ PingProtocolList HostPingProtocolOption::parse_protocol_list( { BOOST_ASSERT( !protocol_string.empty()); - PingProtocol protocol = get_ping_protocol_from_string( protocol_string ); - protocol_list.push_back( protocol ); + // The second item in the list is the amount of pings to perform before change from the + // first protocol to the second (e.g. ICMP,10,ICMPv6). + if ( protocol_index == 1 ) + { + try + { + *protocol_fallback_count = boost::lexical_cast< int >( protocol_string ); + } + catch ( boost::bad_lexical_cast const &ex ) + { + GlobalLogger.error() << "Error: input string was not valid" << endl; + } + } + else + { + PingProtocol protocol = get_ping_protocol_from_string( protocol_string ); + protocol_list->push_back( protocol ); - GlobalLogger.debug() << "- " << protocol_string; + GlobalLogger.debug() << "- " << protocol_string; - BOOST_ASSERT( ( PingProtocol_First <= protocol ) && ( protocol <= PingProtocol_Last ) ); - } + BOOST_ASSERT( ( PingProtocol_First <= protocol ) && ( protocol <= PingProtocol_Last ) ); + } - BOOST_ASSERT( 0 < protocol_list.size() ); + protocol_index++; + } - return protocol_list; + BOOST_ASSERT( 0 < protocol_list->size() ); } diff --git a/src/config/option/hostpingprotocoloption.h b/src/config/option/hostpingprotocoloption.h index a920318..3171992 100644 --- a/src/config/option/hostpingprotocoloption.h +++ b/src/config/option/hostpingprotocoloption.h @@ -45,8 +45,10 @@ public: ); private: - PingProtocolList parse_protocol_list( - const std::string &protocol_list_string + void parse_protocol_list( + const std::string &protocol_list_string, + PingProtocolList *protocol_list, + int *protocol_fallback_count ) const; }; -- 1.7.1