Code improvements
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 8 Mar 2011 10:14:28 +0000 (11:14 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 8 Mar 2011 10:14:28 +0000 (11:14 +0100)
- stratified some functionalities into other methods;
- const-correctness
- added variables to clarify code

src/ping/boostpinger.cpp
src/ping/boostpinger.h

index 2d81157..c7fe9a2 100644 (file)
@@ -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<uint16_t> ( ::getpid() );
 }
index 46ac454..a19ee4a 100644 (file)
@@ -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;