From 83d871832b7fe7464b95635d02fa2e30abb0647e Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Mon, 14 Mar 2011 15:44:25 +0100 Subject: [PATCH] Replaced the times-to-ping parameter to simplify the code, now it pings just one time and uses a status on/off approach to check if the ping was performed. - also BoostPinger::create_echo_request() method is const and self sufficient, use parameter instead of changing the SequenceNumber member variable --- src/ping/boostpinger.cpp | 51 +++++++++++++++++++++++++------------------ src/ping/boostpinger.h | 21 ++++++++++------- src/ping/pinger.h | 6 +---- src/ping/pingscheduler.cpp | 3 +- 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/ping/boostpinger.cpp b/src/ping/boostpinger.cpp index 008a336..53df900 100644 --- a/src/ping/boostpinger.cpp +++ b/src/ping/boostpinger.cpp @@ -34,10 +34,8 @@ BoostPinger::BoostPinger( TimeSent( microsec_clock::universal_time() ), ReplyBuffer(), RepliesCount( 0 ), - TimesToPingTotal( 0 ), EchoReplyTimeoutInSec( echo_reply_timeout_in_sec ), - MinTimesToPing( 1 ), - MaxTimesToPing( numeric_limits::max() ) + PingerStatus( PingStatus_NotSent ) { } @@ -46,28 +44,20 @@ BoostPinger::~BoostPinger() } /** - * Pings a given destination address + * 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. + * after the ping requested has finished or timed-out. */ -void BoostPinger::ping( - const string &destination, - const uint times_to_ping -) +void BoostPinger::ping( const string &destination ) { BOOST_ASSERT( !destination.empty() ); - BOOST_ASSERT( ( MinTimesToPing <= times_to_ping ) && ( times_to_ping <= MaxTimesToPing ) ); icmp::resolver::query query( icmp::v4(), destination, "" ); DestinationEndpoint = *Resolver.resolve( query ); - TimesToPingTotal = times_to_ping; - start_pinger(); } @@ -86,10 +76,11 @@ void BoostPinger::stop_pinger() void BoostPinger::start_send() { - IcmpPacket icmp_echo_request_packet = create_echo_request(); + ++SequenceNumber; - uint times_already_pinged = SequenceNumber; - if ( times_already_pinged <= TimesToPingTotal ) + IcmpPacket icmp_echo_request_packet = create_echo_request( SequenceNumber ); + + if ( PingerStatus == PingStatus_NotSent ) { send_echo_request( icmp_echo_request_packet ); } @@ -99,17 +90,22 @@ void BoostPinger::start_send() } } -IcmpPacket BoostPinger::create_echo_request() +IcmpPacket BoostPinger::create_echo_request( + const uint16_t sequence_number +) const { IcmpData icmp_data( "ping-message" ); - SequenceNumber++; IcmpHeader::IcmpType type = IcmpHeader::EchoRequest; uint8_t code = 0; uint16_t identifier = get_identifier(); IcmpChecksumCalculator calculator( icmp_data.begin(), icmp_data.end() ); - uint16_t checksum = calculator.compute( type, code, identifier, SequenceNumber ); - IcmpHeader icmp_header( type, code, checksum, identifier, SequenceNumber ); + uint16_t checksum = calculator.compute( + type, code, identifier, sequence_number + ); + IcmpHeader icmp_header( + type, code, checksum, identifier, sequence_number + ); return IcmpPacket( icmp_header, icmp_data ); } @@ -143,8 +139,12 @@ void BoostPinger::schedule_timeout_echo_reply() void BoostPinger::handle_timeout_echo_reply() { if ( RepliesCount == 0 ) + { cout << "Request timed out" << endl; + set_ping_status( PingStatus_FailureTimeout ); + } + schedule_next_echo_request(); } @@ -186,11 +186,15 @@ void BoostPinger::handle_receive_echo_reply( const size_t &bytes_transferred ) { // If this is the first reply, interrupt the echo reply timeout. if ( RepliesCount == 0 ) + { Timer.cancel(); + } ++RepliesCount; print_echo_reply( icmp_packet, bytes_transferred ); + + set_ping_status( PingStatus_SuccessReply ); } start_receive(); @@ -218,6 +222,11 @@ void BoostPinger::print_echo_reply( << " time=" << time << " ms" << endl; } +void BoostPinger::set_ping_status( BoostPinger::PingStatus ping_status ) +{ + PingerStatus = ping_status; +} + uint16_t BoostPinger::get_identifier() const { return static_cast ( ::getpid() ); diff --git a/src/ping/boostpinger.h b/src/ping/boostpinger.h index 8420acd..e57c0fe 100644 --- a/src/ping/boostpinger.h +++ b/src/ping/boostpinger.h @@ -20,17 +20,21 @@ public: ); virtual ~BoostPinger(); - void ping( - const std::string &destination, - const uint times_to_ping - ); + void ping( const std::string &destination ); + +private: + enum PingStatus { + PingStatus_NotSent, + PingStatus_SuccessReply, + PingStatus_FailureTimeout, + }; private: void start_pinger(); void stop_pinger(); void start_send(); - IcmpPacket create_echo_request(); + IcmpPacket create_echo_request( const uint16_t sequence_number ) const; void send_echo_request( const IcmpPacket &icmp_packet ); void schedule_timeout_echo_reply(); void handle_timeout_echo_reply(); @@ -43,6 +47,8 @@ private: const std::size_t &bytes_transferred ) const; + void set_ping_status( BoostPinger::PingStatus ping_status ); + uint16_t get_identifier() const; private: @@ -55,11 +61,8 @@ private: boost::posix_time::ptime TimeSent; boost::asio::streambuf ReplyBuffer; uint RepliesCount; - uint TimesToPingTotal; uint EchoReplyTimeoutInSec; - - const uint MinTimesToPing; - const uint MaxTimesToPing; + BoostPinger::PingStatus PingerStatus; }; diff --git a/src/ping/pinger.h b/src/ping/pinger.h index 5703937..3d1035c 100644 --- a/src/ping/pinger.h +++ b/src/ping/pinger.h @@ -1,7 +1,6 @@ #ifndef PINGER_H #define PINGER_H -#include #include //----------------------------------------------------------------------------- @@ -14,10 +13,7 @@ public: Pinger(); virtual ~Pinger(); - virtual void ping( - const std::string &destination, - const uint times_to_ping - ) = 0; + virtual void ping( const std::string &destination ) = 0; }; diff --git a/src/ping/pingscheduler.cpp b/src/ping/pingscheduler.cpp index 600cbe0..86b2a44 100644 --- a/src/ping/pingscheduler.cpp +++ b/src/ping/pingscheduler.cpp @@ -53,8 +53,7 @@ void PingScheduler::ping( const string &destination ) io_service io_service; uint echo_reply_timeout_in_sec = 5; // TODO configurable: this is the timeout to WAIT FOR the ping before considering a timeout 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 ); + pinger.ping( destination ); update_ping_statistics(); -- 1.7.1