Code improvements on BoostPinger
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Thu, 10 Mar 2011 10:22:43 +0000 (11:22 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Thu, 10 Mar 2011 14:50:19 +0000 (15:50 +0100)
- included an assert to make sure a non-empty address is sent to the socket
- better names for the handlers methods

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

index d694101..f02db05 100644 (file)
@@ -119,8 +119,11 @@ void BoostPinger::send_echo_request( const IcmpPacket &icmp_packet )
     ostream os( &request_buffer );
     os << icmp_packet;
 
-    // Send the request.
     TimeSent = microsec_clock::universal_time();
+
+    // Send the request.
+    string dest_address_string = DestinationEndpoint.address().to_string();
+    BOOST_ASSERT( !dest_address_string.empty() );
     Socket.send_to( request_buffer.data(), DestinationEndpoint );
 
     schedule_timeout_echo_reply();
@@ -131,10 +134,12 @@ void BoostPinger::schedule_timeout_echo_reply()
     // Wait up to N seconds for a reply.
     RepliesCount = 0;
     Timer.expires_at( TimeSent + seconds( EchoReplyTimeoutInSec ) );
-    Timer.async_wait( boost::bind( &BoostPinger::handle_timeout, this ) );
+    Timer.async_wait(
+            boost::bind( &BoostPinger::handle_timeout_echo_reply, this )
+    );
 }
 
-void BoostPinger::handle_timeout()
+void BoostPinger::handle_timeout_echo_reply()
 {
     if ( RepliesCount == 0 )
         cout << "Request timed out" << endl;
@@ -158,11 +163,11 @@ void BoostPinger::start_receive()
     // Wait for a reply. We prepare the buffer to receive up to 64KB.
     Socket.async_receive(
             ReplyBuffer.prepare( 65536 ),
-            boost::bind( &BoostPinger::handle_receive, this, _2 )
+            boost::bind( &BoostPinger::handle_receive_echo_reply, this, _2 )
     );
 }
 
-void BoostPinger::handle_receive( const size_t &bytes_transferred )
+void BoostPinger::handle_receive_echo_reply( const size_t &bytes_transferred )
 {
     // The actual number of bytes received is committed to the buffer so that we
     // can extract it using a std::istream object.
@@ -178,7 +183,7 @@ void BoostPinger::handle_receive( const size_t &bytes_transferred )
     // expected sequence number.
     if ( is && icmp_packet.match( IcmpHeader::EchoReply, get_identifier(), SequenceNumber ) )
     {
-        // If this is the first reply, interrupt the five second timeout.
+        // If this is the first reply, interrupt the echo reply timeout.
         if ( RepliesCount == 0 )
             Timer.cancel();
 
index d20f53b..c90dd46 100644 (file)
@@ -32,11 +32,11 @@ private:
     IcmpPacket create_echo_request();
     void send_echo_request( const IcmpPacket &icmp_packet );
     void schedule_timeout_echo_reply();
-    void handle_timeout();
+    void handle_timeout_echo_reply();
     void schedule_next_echo_request();
 
     void start_receive();
-    void handle_receive( const std::size_t &bytes_transferred );
+    void handle_receive_echo_reply( const std::size_t &bytes_transferred );
     void print_echo_reply(
             const IcmpPacket &icmp_packet,
             const std::size_t &bytes_transferred