From 475ad07c90527a889a72d0f4bf1aa16dd6063e9e Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Mon, 18 Jul 2011 22:26:01 -0300 Subject: [PATCH] Fully functional pinger factory --- src/CMakeLists.txt | 1 + src/host/pingerfactory.cpp | 43 +++++++++++++++++++++++++++++++++++++++++-- src/host/pingerfactory.h | 12 +++++++++++- src/host/pingscheduler.cpp | 16 +++++----------- src/host/pingscheduler.h | 2 +- 5 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index eaa04f8..d601e4b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,6 +36,7 @@ set(SOURCES dns/timetolive.cpp host/hoststatusanalyzer.cpp host/pinger.cpp + host/pingerfactory.cpp host/pinginterval.cpp host/pingprotocol.cpp host/pingscheduler.cpp diff --git a/src/host/pingerfactory.cpp b/src/host/pingerfactory.cpp index 5b38f14..767d58a 100644 --- a/src/host/pingerfactory.cpp +++ b/src/host/pingerfactory.cpp @@ -20,6 +20,13 @@ #include "host/pingerfactory.h" +#include "icmp/icmppinger.h" +#include "tcp/tcppinger.h" + +using namespace std; +using boost::shared_ptr; +using boost::asio::io_service; + //----------------------------------------------------------------------------- // PingerFactory //----------------------------------------------------------------------------- @@ -32,7 +39,39 @@ PingerFactory::~PingerFactory() { } -void PingerFactory::createPinger( PingProtocol type ) +/** + * @brief Create a Pinger object suitable to the given protocol. + * + * @param protocol One of the available ping protocols. + * @param io_serv The io_service object. + * @param network_interface The network interface name from where the ping + * packet will be sent. + * @return a Pinger object to able to ping using the given protocol. + */ +shared_ptr PingerFactory::createPinger( + const PingProtocol protocol, + io_service &io_serv, + const string &network_interface +) { - // TODO + BOOST_ASSERT( ( PingProtocol_First <= protocol ) && ( protocol <= PingProtocol_Last ) ); + BOOST_ASSERT( !network_interface.empty() ); + + // Ping reply timeout. Could be made a configuration variable + const int ping_reply_timeout_in_sec = 30; // TODO + + switch ( protocol ) + { + case PingProtocol_ICMP: + return shared_ptr( + new IcmpPinger( io_serv, network_interface, ping_reply_timeout_in_sec ) + ); + case PingProtocol_TCP: + return shared_ptr( + new TcpPinger( io_serv, network_interface, ping_reply_timeout_in_sec ) + ); + default: + BOOST_ASSERT( false ); + return shared_ptr(); + } } diff --git a/src/host/pingerfactory.h b/src/host/pingerfactory.h index c708ab3..d369865 100644 --- a/src/host/pingerfactory.h +++ b/src/host/pingerfactory.h @@ -21,6 +21,12 @@ #ifndef PINGERFACTORY_H #define PINGERFACTORY_H +#include + +#include +#include + +#include "host/pinger.h" #include "host/pingprotocol.h" //----------------------------------------------------------------------------- @@ -33,7 +39,11 @@ public: PingerFactory(); virtual ~PingerFactory(); - void createPinger( PingProtocol type ); + static boost::shared_ptr createPinger( + const PingProtocol protocol, + boost::asio::io_service &io_serv, + const std::string &network_interface + ); }; #endif /* PINGERFACTORY_H */ diff --git a/src/host/pingscheduler.cpp b/src/host/pingscheduler.cpp index c5250b9..baaf831 100644 --- a/src/host/pingscheduler.cpp +++ b/src/host/pingscheduler.cpp @@ -44,13 +44,10 @@ using I2n::Logger::GlobalLogger; // PingScheduler //----------------------------------------------------------------------------- -/// Ping reply timeout. Could be made a configuration variable -const int PingReplyTimeout = 30; - PingScheduler::PingScheduler( const string &network_interface, const string &destination_address, - const PingProtocol /*ping_protocol*/, + const PingProtocol protocol, const long ping_interval_in_sec, const int ping_fail_percentage_limit, const string &nameserver, @@ -67,13 +64,10 @@ PingScheduler::PingScheduler( Ping(), Thread() { -#if 0 - // TODO read the Factory Design Pattern from Head First - Ping = PingerFactory::create( ping_protocol ); -#endif - Ping = shared_ptr( - new IcmpPinger( IoService, LocalNetworkInterfaceName, PingReplyTimeout ) - ); + BOOST_ASSERT( !network_interface.empty() ); + BOOST_ASSERT( !destination_address.empty() ); + + Ping = PingerFactory::createPinger( protocol, IoService, network_interface ); } PingScheduler::~PingScheduler() diff --git a/src/host/pingscheduler.h b/src/host/pingscheduler.h index 84384d6..1bd3556 100644 --- a/src/host/pingscheduler.h +++ b/src/host/pingscheduler.h @@ -51,7 +51,7 @@ public: PingScheduler( const std::string &network_interface, const std::string &destination_address, - const PingProtocol ping_protocol, + const PingProtocol protocol, const long ping_interval_in_sec, const int ping_fail_percentage_limit, const std::string &nameserver, -- 1.7.1