PingScheduler now pings hosts IPs cyclically
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Fri, 11 Mar 2011 10:02:15 +0000 (11:02 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Fri, 11 Mar 2011 10:04:01 +0000 (11:04 +0100)
- included a DNS resolver member variable to keep track of resolved IPs
- PingScheduler's constructor takes specific arguments instead of a generic Host object, this states clear which data the PingScheduler needs

src/main.cpp
src/ping/pingscheduler.cpp
src/ping/pingscheduler.h

index 1dfbb68..8175c8d 100644 (file)
@@ -24,8 +24,12 @@ int main( int argc, char* argv[] )
         vector< PingSchedulerItem > scheduler_list;
         BOOST_FOREACH( HostItem host, hosts )
         {
+            string ping_address = (*host).get_address();
+            uint ping_interval = (*host).get_interval();
             PingSchedulerItem scheduler(
-                    new PingScheduler( io_service, *host )
+                    new PingScheduler(
+                            io_service, ping_address, ping_interval
+                    )
             );
             scheduler_list.push_back( scheduler );
         }
@@ -36,7 +40,7 @@ int main( int argc, char* argv[] )
             scheduler->start_pinging();
         }
 
-        // Main loop to handle ping requests when they were scheduled
+        // Main loop to handle scheduled ping requests
         io_service.run();
     }
 
index 810eed6..13dc633 100644 (file)
@@ -2,10 +2,12 @@
 #include <iostream>
 
 #include "boostpinger.h"
+#include "dnsresolver.h"
 
 #include "pingscheduler.h"
 
 using namespace std;
+using namespace boost;
 using namespace boost::asio;
 using namespace boost::posix_time;
 
@@ -15,12 +17,14 @@ using namespace boost::posix_time;
 
 PingScheduler::PingScheduler(
         boost::asio::io_service &io_service,
-        const Host &host
+        const string &ping_address,
+        const uint ping_interval_in_sec
 ) :
     IoService( io_service ),
     Timer( io_service ),
     TimeSentLastPing( microsec_clock::universal_time() ),
-    DestinationHost( host )
+    IpList( ping_address ),
+    PingIntervalInSec( ping_interval_in_sec )
 {
 }
 
@@ -30,23 +34,23 @@ PingScheduler::~PingScheduler()
 
 void PingScheduler::start_pinging()
 {
-    string destination = DestinationHost.get_address();
-    uint ping_set_total = 1; // TODO configurable: amount of pings each time
-    uint ping_set_count = 0;
-    while ( ping_set_count < ping_set_total )
-    {
-        ping( destination );
-
-        ping_set_count++;
-    }
+    // 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 );
 }
 
 void 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
-    boost::asio::io_service 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 );
@@ -58,17 +62,17 @@ void PingScheduler::ping( const string &destination )
 
 void PingScheduler::update_ping_statistics()
 {
-    ptime now = microsec_clock::universal_time(); // TODO
-    cerr << "- Time elapsed since last ping = "
-         << (now - TimeSentLastPing).total_seconds() << "s" << endl; // TODO
-    TimeSentLastPing = microsec_clock::universal_time(); // TODO
+    ptime now = microsec_clock::universal_time();
+    cerr << "- Time elapsed since last ping: "
+         << (now - TimeSentLastPing).total_seconds() << "s" << endl; // TODO reformat this message
+
+    TimeSentLastPing = microsec_clock::universal_time();
 }
 
 void PingScheduler::schedule_next_ping()
 {
-    uint interval = DestinationHost.get_interval(); // TODO configurable:
-    Timer.expires_from_now( seconds( interval ) );
-    Timer.async_wait( boost::bind( &PingScheduler::handle_next_ping, this ) );
+    Timer.expires_from_now( seconds( PingIntervalInSec ) );
+    Timer.async_wait( bind( &PingScheduler::handle_next_ping, this ) );
 }
 
 void PingScheduler::handle_next_ping()
index d81c04d..736e2c6 100644 (file)
@@ -5,7 +5,7 @@
 #include <boost/shared_ptr.hpp>
 #include <string>
 
-#include "host.h"
+#include "dnsresolver.h"
 
 //-----------------------------------------------------------------------------
 // PingScheduler
@@ -16,7 +16,8 @@ class PingScheduler
 public:
     PingScheduler(
             boost::asio::io_service &io_service,
-            const Host &host
+            const std::string &ping_address,
+            const uint ping_interval_in_sec
     );
     virtual ~PingScheduler();
 
@@ -32,7 +33,8 @@ private:
     boost::asio::io_service &IoService;
     boost::asio::deadline_timer Timer;
     boost::posix_time::ptime TimeSentLastPing;
-    Host DestinationHost;
+    DnsResolver IpList;
+    const uint PingIntervalInSec;
 
 };