Uses the ping analyzer to evaluate the ping statistics after each ping.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 15 Mar 2011 10:55:50 +0000 (11:55 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 15 Mar 2011 11:29:13 +0000 (12:29 +0100)
src/ping/pingscheduler.cpp
src/ping/pingscheduler.h

index 86b2a44..d0956d8 100644 (file)
@@ -25,8 +25,11 @@ PingScheduler::PingScheduler(
     Timer( io_service ),
     TimeSentLastPing( microsec_clock::universal_time() ),
     IpList( ping_address ),
-    PingIntervalInSec( ping_interval_in_sec )
+    PingIntervalInSec( ping_interval_in_sec ),
+    Analyzer( ping_address, 50 ) // 50% max acceptable failure
 {
+    uint resolved_ip_count = IpList.get_resolved_ip_count();
+    Analyzer.set_resolved_ip_count( resolved_ip_count );
 }
 
 PingScheduler::~PingScheduler()
@@ -35,32 +38,34 @@ PingScheduler::~PingScheduler()
 
 void PingScheduler::start_pinging()
 {
-    // try cyclic the hosts in the list
-    // if we get back to the front of the list, analyze the result
-    //      if this X% fail is greater than the threshold, mark the host as down
-    //      otherwise, the host is up
-    // advice the external system about the host situation
-
     string destination_ip = IpList.get_next_ip();
 
-    ping( destination_ip );
+    bool ping_success = ping( destination_ip );
+
+    update_ping_statistics( ping_success );
+
+    update_ping_elapsed_time();
+
+    schedule_next_ping();
 }
 
-void PingScheduler::ping( const string &destination )
+bool PingScheduler::ping( const string &destination )
 {
     BOOST_ASSERT( !destination.empty() );
 
     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 );
-    pinger.ping( destination );
 
-    update_ping_statistics();
+    return pinger.ping( destination );
+}
 
-    schedule_next_ping();
+void PingScheduler::update_ping_statistics( const bool ping_success )
+{
+    Analyzer.update_ping_statistics( ping_success );
 }
 
-void PingScheduler::update_ping_statistics()
+void PingScheduler::update_ping_elapsed_time()
 {
     ptime now = microsec_clock::universal_time();
     cerr << "- Time elapsed since last ping: "
index c5ed595..e33b977 100644 (file)
@@ -7,6 +7,7 @@
 #include <boost/shared_ptr.hpp>
 
 #include "dns/dnsresolver.h"
+#include "ping/pinganalyzer.h"
 
 //-----------------------------------------------------------------------------
 // PingScheduler
@@ -25,8 +26,9 @@ public:
     void start_pinging();
 
 private:
-    void ping( const std::string &destination );
-    void update_ping_statistics();
+    bool ping( const std::string &destination );
+    void update_ping_statistics( const bool ping_success );
+    void update_ping_elapsed_time();
     void schedule_next_ping();
     void handle_next_ping();
 
@@ -36,6 +38,7 @@ private:
     boost::posix_time::ptime TimeSentLastPing;
     DnsResolver IpList;
     const uint PingIntervalInSec;
+    PingAnalyzer Analyzer;
 
 };