From e57069688f1d0a84a5b4789452d3e99b853c8e6b Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Fri, 11 Mar 2011 11:02:15 +0100 Subject: [PATCH] PingScheduler now pings hosts IPs cyclically - 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 | 8 ++++++-- src/ping/pingscheduler.cpp | 42 +++++++++++++++++++++++------------------- src/ping/pingscheduler.h | 8 +++++--- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 1dfbb68..8175c8d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); } diff --git a/src/ping/pingscheduler.cpp b/src/ping/pingscheduler.cpp index 810eed6..13dc633 100644 --- a/src/ping/pingscheduler.cpp +++ b/src/ping/pingscheduler.cpp @@ -2,10 +2,12 @@ #include #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() diff --git a/src/ping/pingscheduler.h b/src/ping/pingscheduler.h index d81c04d..736e2c6 100644 --- a/src/ping/pingscheduler.h +++ b/src/ping/pingscheduler.h @@ -5,7 +5,7 @@ #include #include -#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; }; -- 1.7.1