From: Guilherme Maciel Ferreira Date: Wed, 9 Mar 2011 09:40:34 +0000 (+0100) Subject: The echo reply timeout can be configured through parameter to BoostPinger X-Git-Tag: v1.0~151 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=e08ab6c914f967f0fad363ba4c24984848227c58;p=pingcheck The echo reply timeout can be configured through parameter to BoostPinger --- diff --git a/src/ping/boostpinger.cpp b/src/ping/boostpinger.cpp index 29ab2d2..d694101 100644 --- a/src/ping/boostpinger.cpp +++ b/src/ping/boostpinger.cpp @@ -6,6 +6,7 @@ #include "icmpchecksumcalculator.h" #include "icmpdata.h" #include "icmpheader.h" +#include "icmppacket.h" #include "ipv4header.h" #include "boostpinger.h" @@ -20,7 +21,8 @@ using namespace boost::posix_time; //----------------------------------------------------------------------------- BoostPinger::BoostPinger( - boost::asio::io_service &io_service + boost::asio::io_service &io_service, + const uint echo_reply_timeout_in_sec ) : IoService( io_service ), Resolver( io_service ), @@ -32,6 +34,7 @@ BoostPinger::BoostPinger( ReplyBuffer(), RepliesCount( 0 ), TimesToPingTotal( 0 ), + EchoReplyTimeoutInSec( echo_reply_timeout_in_sec ), MinTimesToPing( 1 ), MaxTimesToPing( numeric_limits::max() ) { @@ -41,8 +44,15 @@ BoostPinger::~BoostPinger() { } -/* - * TODO document that this method blocks (is synchronous) +/** + * Pings a given destination address + * + * @param destination The address of the host where to ping. + * + * @param times_to_ping The amount of times the destination host will be pinged. + * + * @note This method is synchronous, i.e. this method blocks and returns only + * after the amount of pings requested has finished or timed out. */ void BoostPinger::ping( const string &destination, @@ -120,8 +130,7 @@ void BoostPinger::schedule_timeout_echo_reply() { // Wait up to N seconds for a reply. RepliesCount = 0; - const uint echo_reply_timeout_in_sec = 5; // TODO configurable: this is the timeout to WAIT FOR the ping before considering a timeout - Timer.expires_at( TimeSent + seconds( echo_reply_timeout_in_sec ) ); + Timer.expires_at( TimeSent + seconds( EchoReplyTimeoutInSec ) ); Timer.async_wait( boost::bind( &BoostPinger::handle_timeout, this ) ); } @@ -197,10 +206,10 @@ void BoostPinger::print_echo_reply( uint time = (now - TimeSent).total_milliseconds(); cout << bytes_received << " bytes " - << "from " << source_address - << ": icmp_seq=" << sequence_number - << " ttl=" << ttl - << " time=" << time << " ms" << endl; + << "from " << source_address + << ": icmp_seq=" << sequence_number + << " ttl=" << ttl + << " time=" << time << " ms" << endl; } uint16_t BoostPinger::get_identifier() const diff --git a/src/ping/boostpinger.h b/src/ping/boostpinger.h index 44e3794..d20f53b 100644 --- a/src/ping/boostpinger.h +++ b/src/ping/boostpinger.h @@ -2,10 +2,10 @@ #define BOOSTPINGER_H #include - -#include "icmppacket.h" #include "pinger.h" +class IcmpPacket; + //----------------------------------------------------------------------------- // BoostPinger //----------------------------------------------------------------------------- @@ -13,7 +13,10 @@ class BoostPinger : public Pinger { public: - BoostPinger( boost::asio::io_service &io_service ); + BoostPinger( + boost::asio::io_service &io_service, + const uint echo_reply_timeout_in_sec + ); virtual ~BoostPinger(); void ping( @@ -52,6 +55,7 @@ private: boost::asio::streambuf ReplyBuffer; uint RepliesCount; uint TimesToPingTotal; + uint EchoReplyTimeoutInSec; const uint MinTimesToPing; const uint MaxTimesToPing; diff --git a/src/ping/pingscheduler.cpp b/src/ping/pingscheduler.cpp index 5a85fac..810eed6 100644 --- a/src/ping/pingscheduler.cpp +++ b/src/ping/pingscheduler.cpp @@ -45,9 +45,10 @@ void PingScheduler::ping( const string &destination ) { BOOST_ASSERT( !destination.empty() ); - uint times_to_ping = 1; // TODO configurable: this must be automatically selected + uint echo_reply_timeout_in_sec = 5; // TODO configurable: this is the timeout to WAIT FOR the ping before considering a timeout boost::asio::io_service io_service; - BoostPinger pinger( io_service ); + BoostPinger pinger( io_service, echo_reply_timeout_in_sec ); + uint times_to_ping = 1; // TODO configurable: this must be automatically selected pinger.ping( destination, times_to_ping ); update_ping_statistics();