From 46531881f06af5666d62a74836b1101cdcb779d9 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Wed, 7 Mar 2012 07:24:51 -0300 Subject: [PATCH] Perform N pings using one protocol, then after those N pings, switch to the second protocol, if it available. --- src/host/pingerfactory.cpp | 6 +++++- src/host/pingerfactory.h | 1 + src/host/pingrotate.cpp | 17 ++++++++++++++--- src/host/pingrotate.h | 11 ++++++++--- src/host/pingscheduler.cpp | 3 +++ src/host/pingscheduler.h | 1 + src/main.cpp | 2 ++ 7 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/host/pingerfactory.cpp b/src/host/pingerfactory.cpp index 561d170..cc50336 100644 --- a/src/host/pingerfactory.cpp +++ b/src/host/pingerfactory.cpp @@ -116,6 +116,7 @@ PingerItem PingerFactory::createPinger( * @brief Create a Pinger object suitable to the given list of protocols. * * @param protocol_list A list of the available ping protocols. + * @param protocol_fallback_count The amount of pings to perform before change the protocol. * @param io_serv The @c io_service object. * @param network_interface The network interface name from where the ping * packet will be sent. @@ -127,11 +128,13 @@ PingerItem PingerFactory::createPinger( */ PingRotateItem PingerFactory::createPinger( const PingProtocolList &protocol_list, + const int protocol_fallback_count, io_service &io_serv, const string &network_interface, const string &destination_address, const uint16_t destination_port, const string &nameserver + ) { BOOST_ASSERT( 0 < protocol_list.size() ); @@ -146,6 +149,7 @@ PingRotateItem PingerFactory::createPinger( destination_address, destination_port, nameserver, - protocol_list + protocol_list, + protocol_fallback_count ) ); } diff --git a/src/host/pingerfactory.h b/src/host/pingerfactory.h index 05d3415..8f2b71d 100644 --- a/src/host/pingerfactory.h +++ b/src/host/pingerfactory.h @@ -45,6 +45,7 @@ public: static PingRotateItem createPinger( const PingProtocolList &protocol_list, + const int protocol_fallback_count, boost::asio::io_service &io_serv, const std::string &network_interface, const std::string &destination_address, diff --git a/src/host/pingrotate.cpp b/src/host/pingrotate.cpp index c8b3433..2c94783 100644 --- a/src/host/pingrotate.cpp +++ b/src/host/pingrotate.cpp @@ -46,6 +46,7 @@ using boost::shared_ptr; * @param nameserver Server to resolve the addresses. * @param protocol_list A list of protocols to be used to ping the * host. The protocols will be used in the order they are in the list. + * @param protocol_fallback_count The amount of pings to perform before change the protocol. */ PingRotate::PingRotate( io_service &io_serv, @@ -53,7 +54,8 @@ PingRotate::PingRotate( const string &destination_address, const uint16_t destination_port, const string &nameserver, - const PingProtocolList protocol_list + const PingProtocolList protocol_list, + const int protocol_fallback_count ) : IoService( io_serv ), NetworkInterfaceName( network_interface ), @@ -62,6 +64,8 @@ PingRotate::PingRotate( DestinationPort( destination_port ), Nameserver( nameserver ), ProtocolList( protocol_list ), + ProtocolFallbackCount( protocol_fallback_count ), + PingCount( 0 ), Ping(), PingDoneCallback() { @@ -71,7 +75,7 @@ PingRotate::PingRotate( BOOST_ASSERT( !nameserver.empty() ); BOOST_ASSERT( 0 < protocol_list.size() ); - update_ping_protocol(); + get_next_ping_protocol(); } /** @@ -91,6 +95,7 @@ PingRotate::~PingRotate() void PingRotate::ping( function ping_done_callback ) { BOOST_ASSERT( ( 0 < DestinationPort ) && ( DestinationPort < numeric_limits::max() ) ); + BOOST_ASSERT( ( 0 <= PingCount ) && ( PingCount < numeric_limits::max() ) ); set_ping_done_callback( ping_done_callback ); @@ -103,6 +108,8 @@ void PingRotate::ping( function ping_done_callback ) DestinationPort, boost::bind(&PingRotate::ping_done_handler, this, _1) ); + + PingCount++; } bool PingRotate::resolve_ping_address() @@ -154,9 +161,13 @@ void PingRotate::get_next_ping_protocol() bool PingRotate::can_change_ping_protocol() const { + BOOST_ASSERT( ( 0 <= ProtocolFallbackCount ) && ( ProtocolFallbackCount < numeric_limits::max() ) ); + BOOST_ASSERT( ( 0 <= PingCount ) && ( PingCount < numeric_limits::max() ) ); + // TODO can_change_ping_protocol() and get_next_ping_protocol() may be implemented in a Algorithm // class that can be exchanged in this class to provide an algorithm neutral class - return true; + + return ( PingCount >= ProtocolFallbackCount ); } void PingRotate::update_dns_resolver( PingProtocol current_protocol ) diff --git a/src/host/pingrotate.h b/src/host/pingrotate.h index f666d11..61b0f3c 100644 --- a/src/host/pingrotate.h +++ b/src/host/pingrotate.h @@ -18,8 +18,8 @@ on this file might be covered by the GNU General Public License. */ -#ifndef PIN_GROTATE_H -#define PIN_GROTATE_H +#ifndef PING_ROTATE_H +#define PING_ROTATE_H #include @@ -51,7 +51,8 @@ public: const std::string &destination_address, const uint16_t destination_port, const std::string &nameserver, - const PingProtocolList protocol_list + const PingProtocolList protocol_list, + const int protocol_fallback_count ); virtual ~PingRotate(); @@ -93,6 +94,10 @@ private: std::string Nameserver; /// The list of protocols to ping PingProtocolList ProtocolList; + /// The amount of pings to perform before change the protocol + const int ProtocolFallbackCount; + /// The amount of pings performed + int PingCount; /// Internal boost pinger object PingerItem Ping; /// The callback function diff --git a/src/host/pingscheduler.cpp b/src/host/pingscheduler.cpp index 6138afa..ea0b128 100644 --- a/src/host/pingscheduler.cpp +++ b/src/host/pingscheduler.cpp @@ -53,6 +53,7 @@ using I2n::Logger::GlobalLogger; * @param destination_address The remote address to ping. * @param destination_port The remote port to ping. * @param ping_protocol_list A list of protocols to use. + * @param ping_protocol_fallback_count The amount of pings to perform before change the protocol. * @param ping_interval_in_sec Amount of time between each ping. * @param ping_fail_percentage_limit Maximum amount of pings that can fail. * @param nameserver Server to resolve the addresses. @@ -63,6 +64,7 @@ PingScheduler::PingScheduler( const string &destination_address, const uint16_t destination_port, const PingProtocolList ping_protocol_list, + const int protocol_fallback_count, const long ping_interval_in_sec, const int ping_fail_percentage_limit, const string &nameserver, @@ -87,6 +89,7 @@ PingScheduler::PingScheduler( Ping = PingerFactory::createPinger( ping_protocol_list, + protocol_fallback_count, IoService, network_interface, destination_address, diff --git a/src/host/pingscheduler.h b/src/host/pingscheduler.h index 583ff44..0029297 100644 --- a/src/host/pingscheduler.h +++ b/src/host/pingscheduler.h @@ -53,6 +53,7 @@ public: const std::string &destination_address, const uint16_t destination_port, const PingProtocolList ping_protocol_list, + const int protocol_fallback_count, const long ping_interval_in_sec, const int ping_fail_percentage_limit, const std::string &nameserver, diff --git a/src/main.cpp b/src/main.cpp index 17f685f..16fc853 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -123,6 +123,7 @@ void init_pingers( string destination_address = host->get_address(); uint16_t destination_port = host->get_port(); PingProtocolList protocol_list = host->get_ping_protocol_list(); + int protocol_fallback_count = host->get_ping_protocol_fallback_count(); int ping_interval_in_sec = host->get_interval_in_sec(); PingSchedulerItem scheduler( new PingScheduler( @@ -130,6 +131,7 @@ void init_pingers( destination_address, destination_port, protocol_list, + protocol_fallback_count, ping_interval_in_sec, ping_fail_limit, nameserver, -- 1.7.1