The echo reply timeout can be configured through parameter to BoostPinger
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Wed, 9 Mar 2011 09:40:34 +0000 (10:40 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Wed, 9 Mar 2011 14:33:48 +0000 (15:33 +0100)
src/ping/boostpinger.cpp
src/ping/boostpinger.h
src/ping/pingscheduler.cpp

index 29ab2d2..d694101 100644 (file)
@@ -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<uint>::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
index 44e3794..d20f53b 100644 (file)
@@ -2,10 +2,10 @@
 #define BOOSTPINGER_H
 
 #include <boost/asio.hpp>
-
-#include "icmppacket.h"
 #include "pinger.h"
 
+class IcmpPacket;
+
 //-----------------------------------------------------------------------------
 // BoostPinger
 //-----------------------------------------------------------------------------
 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;
index 5a85fac..810eed6 100644 (file)
@@ -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();