Fully functional pinger factory
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 19 Jul 2011 01:26:01 +0000 (22:26 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 19 Jul 2011 01:26:01 +0000 (22:26 -0300)
src/CMakeLists.txt
src/host/pingerfactory.cpp
src/host/pingerfactory.h
src/host/pingscheduler.cpp
src/host/pingscheduler.h

index eaa04f8..d601e4b 100644 (file)
@@ -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
index 5b38f14..767d58a 100644 (file)
 
 #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<Pinger> 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<Pinger>(
+                new IcmpPinger( io_serv, network_interface, ping_reply_timeout_in_sec )
+        );
+    case PingProtocol_TCP:
+        return shared_ptr<Pinger>(
+                new TcpPinger( io_serv, network_interface, ping_reply_timeout_in_sec )
+        );
+    default:
+        BOOST_ASSERT( false );
+        return shared_ptr<Pinger>();
+    }
 }
index c708ab3..d369865 100644 (file)
 #ifndef PINGERFACTORY_H
 #define PINGERFACTORY_H
 
+#include <string>
+
+#include <boost/asio.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include "host/pinger.h"
 #include "host/pingprotocol.h"
 
 //-----------------------------------------------------------------------------
@@ -33,7 +39,11 @@ public:
     PingerFactory();
     virtual ~PingerFactory();
 
-    void createPinger( PingProtocol type );
+    static boost::shared_ptr<Pinger> createPinger(
+            const PingProtocol protocol,
+            boost::asio::io_service &io_serv,
+            const std::string &network_interface
+    );
 };
 
 #endif /* PINGERFACTORY_H */
index c5250b9..baaf831 100644 (file)
@@ -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<Pinger>(
-            new IcmpPinger( IoService, LocalNetworkInterfaceName, PingReplyTimeout )
-    );
+    BOOST_ASSERT( !network_interface.empty() );
+    BOOST_ASSERT( !destination_address.empty() );
+
+    Ping = PingerFactory::createPinger( protocol, IoService, network_interface );
 }
 
 PingScheduler::~PingScheduler()
index 84384d6..1bd3556 100644 (file)
@@ -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,