From 4bb97b452c941eee89680ad83b1656097a02083f Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Wed, 9 Mar 2011 09:47:23 +0100 Subject: [PATCH] Renamed PingCheck class to PingScheduler. - this name describes better the main purpose behind this class, which is to call the pinger in configured intervals --- src/CMakeLists.txt | 2 +- src/main.cpp | 17 ++++++--- src/ping/pingcheck.cpp | 76 -------------------------------------------- src/ping/pingcheck.h | 45 -------------------------- src/ping/pingscheduler.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++ src/ping/pingscheduler.h | 45 ++++++++++++++++++++++++++ 6 files changed, 133 insertions(+), 128 deletions(-) delete mode 100644 src/ping/pingcheck.cpp delete mode 100644 src/ping/pingcheck.h create mode 100644 src/ping/pingscheduler.cpp create mode 100644 src/ping/pingscheduler.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2c5bf24..74e9cc9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,8 +15,8 @@ set( SOURCES icmp/ipv4header.cpp ping/boostpinger.cpp ping/host.cpp - ping/pingcheck.cpp ping/pinger.cpp + ping/pingscheduler.cpp main.cpp ) diff --git a/src/main.cpp b/src/main.cpp index 43db4c7..1dfbb68 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,31 +4,36 @@ #include "configurationreader.h" #include "host.h" -#include "pingcheck.h" +#include "pingscheduler.h" using namespace std; using namespace boost::asio; int main( int argc, char* argv[] ) { + // TODO load_configuration() or read_configuration() ConfigurationReader config_reader; bool read_success = config_reader.parse( argc, argv ); if ( read_success ) { io_service io_service; + // TODO init_pingers() Configuration config = config_reader.get_configuration(); vector< HostItem > hosts = config.get_hosts(); - vector< PingCheckItem > check_list; + vector< PingSchedulerItem > scheduler_list; BOOST_FOREACH( HostItem host, hosts ) { - PingCheckItem check( new PingCheck( io_service, *host ) ); - check_list.push_back( check ); + PingSchedulerItem scheduler( + new PingScheduler( io_service, *host ) + ); + scheduler_list.push_back( scheduler ); } - BOOST_FOREACH( PingCheckItem check, check_list ) + // TODO ping_loop() + BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list ) { - check->start_pinging(); + scheduler->start_pinging(); } // Main loop to handle ping requests when they were scheduled diff --git a/src/ping/pingcheck.cpp b/src/ping/pingcheck.cpp deleted file mode 100644 index 9551d16..0000000 --- a/src/ping/pingcheck.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include - -#include "boostpinger.h" - -#include "pingcheck.h" - -using namespace std; -using namespace boost::asio; -using namespace boost::posix_time; - -//----------------------------------------------------------------------------- -// PingCheck -//----------------------------------------------------------------------------- - -PingCheck::PingCheck( - boost::asio::io_service &io_service, - const Host &host -) : - IoService( io_service ), - Timer( io_service ), - TimeSentLastPing( microsec_clock::universal_time() ), - DestinationHost( host ) -{ -} - -PingCheck::~PingCheck() -{ -} - -void PingCheck::start_pinging() -{ - string destination = DestinationHost.get_address(); - uint ping_set_total = 1; // TODO configurable: - uint ping_set_count = 0; - while ( ping_set_count < ping_set_total ) - { - ping( destination ); - - ping_set_count++; - } -} - -void PingCheck::ping( const string &destination ) -{ - BOOST_ASSERT( !destination.empty() ); - - uint times_to_ping = 1; // TODO configurable: this must be automatically selected - boost::asio::io_service io_service; - BoostPinger pinger( io_service ); - pinger.ping( destination, times_to_ping ); - - update_ping_statistics(); - - schedule_next_ping(); -} - -void PingCheck::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 -} - -void PingCheck::schedule_next_ping() -{ - uint interval = DestinationHost.get_interval(); // TODO configurable: - Timer.expires_from_now( seconds( interval ) ); - Timer.async_wait( boost::bind( &PingCheck::handle_next_ping, this ) ); -} - -void PingCheck::handle_next_ping() -{ - start_pinging(); -} diff --git a/src/ping/pingcheck.h b/src/ping/pingcheck.h deleted file mode 100644 index e733385..0000000 --- a/src/ping/pingcheck.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef PINGCHECK_H -#define PINGCHECK_H - -#include -#include -#include - -#include "host.h" - -//----------------------------------------------------------------------------- -// PingCheck -//----------------------------------------------------------------------------- - -class PingCheck -{ -public: - PingCheck( - boost::asio::io_service &io_service, - const Host &host - ); - virtual ~PingCheck(); - - void start_pinging(); - -private: - void ping( const std::string &destination ); - void update_ping_statistics(); - void schedule_next_ping(); - void handle_next_ping(); - -private: - boost::asio::io_service &IoService; - boost::asio::deadline_timer Timer; - boost::posix_time::ptime TimeSentLastPing; - Host DestinationHost; - -}; - -//----------------------------------------------------------------------------- -// PingCheckItem -//----------------------------------------------------------------------------- - -typedef boost::shared_ptr PingCheckItem; - -#endif /* PINGCHECK_H */ diff --git a/src/ping/pingscheduler.cpp b/src/ping/pingscheduler.cpp new file mode 100644 index 0000000..5a85fac --- /dev/null +++ b/src/ping/pingscheduler.cpp @@ -0,0 +1,76 @@ +#include +#include + +#include "boostpinger.h" + +#include "pingscheduler.h" + +using namespace std; +using namespace boost::asio; +using namespace boost::posix_time; + +//----------------------------------------------------------------------------- +// PingScheduler +//----------------------------------------------------------------------------- + +PingScheduler::PingScheduler( + boost::asio::io_service &io_service, + const Host &host +) : + IoService( io_service ), + Timer( io_service ), + TimeSentLastPing( microsec_clock::universal_time() ), + DestinationHost( host ) +{ +} + +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++; + } +} + +void PingScheduler::ping( const string &destination ) +{ + BOOST_ASSERT( !destination.empty() ); + + uint times_to_ping = 1; // TODO configurable: this must be automatically selected + boost::asio::io_service io_service; + BoostPinger pinger( io_service ); + pinger.ping( destination, times_to_ping ); + + update_ping_statistics(); + + schedule_next_ping(); +} + +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 +} + +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 ) ); +} + +void PingScheduler::handle_next_ping() +{ + start_pinging(); +} diff --git a/src/ping/pingscheduler.h b/src/ping/pingscheduler.h new file mode 100644 index 0000000..d81c04d --- /dev/null +++ b/src/ping/pingscheduler.h @@ -0,0 +1,45 @@ +#ifndef PINGSCHEDULER_H +#define PINGSCHEDULER_H + +#include +#include +#include + +#include "host.h" + +//----------------------------------------------------------------------------- +// PingScheduler +//----------------------------------------------------------------------------- + +class PingScheduler +{ +public: + PingScheduler( + boost::asio::io_service &io_service, + const Host &host + ); + virtual ~PingScheduler(); + + void start_pinging(); + +private: + void ping( const std::string &destination ); + void update_ping_statistics(); + void schedule_next_ping(); + void handle_next_ping(); + +private: + boost::asio::io_service &IoService; + boost::asio::deadline_timer Timer; + boost::posix_time::ptime TimeSentLastPing; + Host DestinationHost; + +}; + +//----------------------------------------------------------------------------- +// PingSchedulerItem +//----------------------------------------------------------------------------- + +typedef boost::shared_ptr PingSchedulerItem; + +#endif /* PINGSCHEDULER_H */ -- 1.7.1