Bring aboard the Ping factory
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Thu, 23 Jun 2011 18:32:22 +0000 (15:32 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Thu, 23 Jun 2011 18:43:14 +0000 (15:43 -0300)
src/host/pingerfactory.cpp [new file with mode: 0644]
src/host/pingerfactory.h [new file with mode: 0644]
src/host/pingscheduler.cpp
src/host/pingscheduler.h
src/main.cpp

diff --git a/src/host/pingerfactory.cpp b/src/host/pingerfactory.cpp
new file mode 100644 (file)
index 0000000..5b38f14
--- /dev/null
@@ -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 (file)
index 0000000..fc93cf0
--- /dev/null
@@ -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 */
index f3c793d..814c781 100644 (file)
@@ -26,6 +26,7 @@ on this file might be covered by the GNU General Public License.
 #include <logfunc.hpp>
 
 #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<Pinger>(
+            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()
index 9a7afe7..52f036f 100644 (file)
@@ -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<Pinger> Ping;
     /// thread object
     boost::thread Thread;
 
index 152e659..0f0dc55 100644 (file)
@@ -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,