Replaced the times-to-ping parameter to simplify the code, now it pings just one...
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Mon, 14 Mar 2011 14:44:25 +0000 (15:44 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Mon, 14 Mar 2011 14:44:25 +0000 (15:44 +0100)
- also BoostPinger::create_echo_request() method is const and self sufficient, use parameter instead of changing the SequenceNumber member variable

src/ping/boostpinger.cpp
src/ping/boostpinger.h
src/ping/pinger.h
src/ping/pingscheduler.cpp

index 008a336..53df900 100644 (file)
@@ -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<uint>::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<uint16_t> ( ::getpid() );
index 8420acd..e57c0fe 100644 (file)
@@ -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;
 
 };
 
index 5703937..3d1035c 100644 (file)
@@ -1,7 +1,6 @@
 #ifndef PINGER_H
 #define PINGER_H
 
-#include <sys/types.h>
 #include <string>
 
 //-----------------------------------------------------------------------------
@@ -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;
 
 };
 
index 600cbe0..86b2a44 100644 (file)
@@ -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();