From 51cbc7903a6f44fd7831059f41e9117e5d258b45 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Thu, 23 Jun 2011 15:32:22 -0300 Subject: [PATCH] Bring aboard the Ping factory --- src/host/pingerfactory.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/host/pingerfactory.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/host/pingscheduler.cpp | 13 +++++++++++-- src/host/pingscheduler.h | 5 ++++- src/main.cpp | 3 +++ 5 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 src/host/pingerfactory.cpp create mode 100644 src/host/pingerfactory.h diff --git a/src/host/pingerfactory.cpp b/src/host/pingerfactory.cpp new file mode 100644 index 0000000..5b38f14 --- /dev/null +++ b/src/host/pingerfactory.cpp @@ -0,0 +1,38 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#include "host/pingerfactory.h" + +//----------------------------------------------------------------------------- +// PingerFactory +//----------------------------------------------------------------------------- + +PingerFactory::PingerFactory() +{ +} + +PingerFactory::~PingerFactory() +{ +} + +void PingerFactory::createPinger( PingProtocol type ) +{ + // TODO +} diff --git a/src/host/pingerfactory.h b/src/host/pingerfactory.h new file mode 100644 index 0000000..fc93cf0 --- /dev/null +++ b/src/host/pingerfactory.h @@ -0,0 +1,44 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#ifndef PINGERFACTORY_H +#define PINGERFACTORY_H + +//----------------------------------------------------------------------------- +// PingerFactory +//----------------------------------------------------------------------------- + +class PingerFactory +{ +public: + enum PingProtocol + { + PingProtocol_ICMP, + PingProtocol_TCP + }; + +public: + PingerFactory(); + virtual ~PingerFactory(); + + void createPinger( PingProtocol type ); +}; + +#endif /* PINGERFACTORY_H */ diff --git a/src/host/pingscheduler.cpp b/src/host/pingscheduler.cpp index f3c793d..814c781 100644 --- a/src/host/pingscheduler.cpp +++ b/src/host/pingscheduler.cpp @@ -26,6 +26,7 @@ on this file might be covered by the GNU General Public License. #include #include "dns/dnsresolver.h" +#include "icmp/icmppinger.h" #include "link/linkstatusanalyzer.h" using namespace std; @@ -49,6 +50,7 @@ const int PingReplyTimeout = 30; PingScheduler::PingScheduler( const string &network_interface, const string &destination_address, + const PingerFactory::PingProtocol ping_protocol, const long ping_interval_in_sec, const int ping_fail_percentage_limit, const string &nameserver, @@ -62,9 +64,16 @@ PingScheduler::PingScheduler( PingIntervalInSec( ping_interval_in_sec ), IpList( destination_address, nameserver ), HostAnalyzer( destination_address, ping_fail_percentage_limit, link_analyzer ), - Pinger(IoService, LocalNetworkInterfaceName, PingReplyTimeout), + 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 ) + ); } PingScheduler::~PingScheduler() @@ -140,7 +149,7 @@ void PingScheduler::ping( const string &destination_ip ) { BOOST_ASSERT( !destination_ip.empty() ); - Pinger.ping( destination_ip, boost::bind(&PingScheduler::ping_done_handler, this, _1) ); + Ping->ping( destination_ip, boost::bind(&PingScheduler::ping_done_handler, this, _1) ); } void PingScheduler::setup_next_ping() diff --git a/src/host/pingscheduler.h b/src/host/pingscheduler.h index 9a7afe7..52f036f 100644 --- a/src/host/pingscheduler.h +++ b/src/host/pingscheduler.h @@ -30,6 +30,8 @@ on this file might be covered by the GNU General Public License. #include "dns/dnsresolver.h" #include "link/linkstatusanalyzer.h" #include "host/hoststatusanalyzer.h" +#include "host/pinger.h" +#include "host/pingerfactory.h" #include "host/pinginterval.h" #include "icmp/icmppinger.h" @@ -48,6 +50,7 @@ public: PingScheduler( const std::string &network_interface, const std::string &destination_address, + const PingerFactory::PingProtocol ping_protocol, const long ping_interval_in_sec, const int ping_fail_percentage_limit, const std::string &nameserver, @@ -90,7 +93,7 @@ private: /// object responsible to evaluate the status of the host HostStatusAnalyzer HostAnalyzer; /// Internal boost pinger object - IcmpPinger Pinger; + boost::shared_ptr Ping; /// thread object boost::thread Thread; diff --git a/src/main.cpp b/src/main.cpp index 152e659..0f0dc55 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,6 +32,7 @@ on this file might be covered by the GNU General Public License. #include "config/configurationreader.h" #include "config/host.h" #include "link/linkstatusanalyzer.h" +#include "host/pingerfactory.h" #include "host/pingscheduler.h" using namespace std; @@ -101,10 +102,12 @@ void init_pingers( { string destination_address = host->get_address(); int ping_interval_in_sec = host->get_interval_in_sec(); + PingerFactory::PingProtocol protocol = PingerFactory::PingProtocol_ICMP; PingSchedulerItem scheduler( new PingScheduler( local_interface, destination_address, + protocol, ping_interval_in_sec, ping_fail_limit, nameserver, -- 1.7.1