From: Guilherme Maciel Ferreira Date: Tue, 8 Mar 2011 10:14:28 +0000 (+0100) Subject: Code improvements X-Git-Tag: v1.0~156 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=2210b856e78781fe23aeb6a090279b5e07a7d110;p=pingcheck Code improvements - stratified some functionalities into other methods; - const-correctness - added variables to clarify code --- diff --git a/src/ping/boostpinger.cpp b/src/ping/boostpinger.cpp index 2d81157..c7fe9a2 100644 --- a/src/ping/boostpinger.cpp +++ b/src/ping/boostpinger.cpp @@ -72,7 +72,7 @@ void BoostPinger::stop_pinger() void BoostPinger::start_send() { - IcmpPacket icmp_echo_request_packet = create_echo_request_packet(); + IcmpPacket icmp_echo_request_packet = create_echo_request(); uint times_already_pinged = SequenceNumber; if ( times_already_pinged <= TimesToPingTotal ) @@ -85,7 +85,7 @@ void BoostPinger::start_send() } } -IcmpPacket BoostPinger::create_echo_request_packet() +IcmpPacket BoostPinger::create_echo_request() { IcmpData icmp_data( "ping-message" ); @@ -110,9 +110,15 @@ void BoostPinger::send_echo_request( const IcmpPacket &icmp_packet ) TimeSent = microsec_clock::universal_time(); Socket.send_to( request_buffer.data(), DestinationEndpoint ); - // Wait up to five seconds for a reply. + schedule_timeout_echo_reply(); +} + +void BoostPinger::schedule_timeout_echo_reply() +{ + // Wait up to N seconds for a reply. RepliesCount = 0; - Timer.expires_at( TimeSent + seconds( 5 ) ); // TODO configurable: + 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.async_wait( boost::bind( &BoostPinger::handle_timeout, this ) ); } @@ -121,8 +127,14 @@ void BoostPinger::handle_timeout() if ( RepliesCount == 0 ) cout << "Request timed out" << endl; + schedule_next_echo_request(); +} + +void BoostPinger::schedule_next_echo_request() +{ // Requests must be sent no less than one second apart. - Timer.expires_at( TimeSent + seconds( 1 ) ); + const uint echo_request_interval_in_sec = 1; + Timer.expires_at( TimeSent + seconds( echo_request_interval_in_sec ) ); Timer.async_wait( boost::bind( &BoostPinger::start_send, this ) ); } @@ -160,7 +172,6 @@ void BoostPinger::handle_receive( const size_t &bytes_transferred ) ++RepliesCount; - // Print out some information about the reply packet. print_echo_reply( icmp_packet, bytes_transferred ); } @@ -170,7 +181,7 @@ void BoostPinger::handle_receive( const size_t &bytes_transferred ) void BoostPinger::print_echo_reply( const IcmpPacket &icmp_packet, const size_t &bytes_transferred -) +) const { Ipv4Header ipv4_hdr = icmp_packet.get_ip_header(); IcmpHeader icmp_hdr = icmp_packet.get_icmp_header(); @@ -189,7 +200,7 @@ void BoostPinger::print_echo_reply( << " time=" << time << " ms" << endl; } -uint16_t BoostPinger::get_identifier() +uint16_t BoostPinger::get_identifier() const { return static_cast ( ::getpid() ); } diff --git a/src/ping/boostpinger.h b/src/ping/boostpinger.h index 46ac454..a19ee4a 100644 --- a/src/ping/boostpinger.h +++ b/src/ping/boostpinger.h @@ -26,18 +26,20 @@ private: void stop_pinger(); void start_send(); - IcmpPacket create_echo_request_packet(); + IcmpPacket create_echo_request(); void send_echo_request( const IcmpPacket &icmp_packet ); + void schedule_timeout_echo_reply(); void handle_timeout(); + void schedule_next_echo_request(); void start_receive(); void handle_receive( const std::size_t &bytes_transferred ); void print_echo_reply( const IcmpPacket &icmp_packet, const std::size_t &bytes_transferred - ); + ) const; - uint16_t get_identifier(); + uint16_t get_identifier() const; private: boost::asio::io_service &io_service;