Perform N pings using one protocol, then after those N pings, switch to
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Wed, 7 Mar 2012 10:24:51 +0000 (07:24 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Wed, 7 Mar 2012 10:24:51 +0000 (07:24 -0300)
the second protocol, if it available.

src/host/pingerfactory.cpp
src/host/pingerfactory.h
src/host/pingrotate.cpp
src/host/pingrotate.h
src/host/pingscheduler.cpp
src/host/pingscheduler.h
src/main.cpp

index 561d170..cc50336 100644 (file)
@@ -116,6 +116,7 @@ PingerItem PingerFactory::createPinger(
  * @brief Create a Pinger object suitable to the given list of protocols.
  *
  * @param protocol_list A list of the available ping protocols.
+ * @param protocol_fallback_count The amount of pings to perform before change the protocol.
  * @param io_serv The @c io_service object.
  * @param network_interface The network interface name from where the ping
  * packet will be sent.
@@ -127,11 +128,13 @@ PingerItem PingerFactory::createPinger(
  */
 PingRotateItem PingerFactory::createPinger(
         const PingProtocolList &protocol_list,
+        const int protocol_fallback_count,
         io_service &io_serv,
         const string &network_interface,
         const string &destination_address,
         const uint16_t destination_port,
         const string &nameserver
+
 )
 {
     BOOST_ASSERT( 0 < protocol_list.size() );
@@ -146,6 +149,7 @@ PingRotateItem PingerFactory::createPinger(
             destination_address,
             destination_port,
             nameserver,
-            protocol_list
+            protocol_list,
+            protocol_fallback_count
     ) );
 }
index 05d3415..8f2b71d 100644 (file)
@@ -45,6 +45,7 @@ public:
 
     static PingRotateItem createPinger(
             const PingProtocolList &protocol_list,
+            const int protocol_fallback_count,
             boost::asio::io_service &io_serv,
             const std::string &network_interface,
             const std::string &destination_address,
index c8b3433..2c94783 100644 (file)
@@ -46,6 +46,7 @@ using boost::shared_ptr;
  * @param nameserver Server to resolve the addresses.
  * @param protocol_list A list of protocols to be used to ping the
  * host. The protocols will be used in the order they are in the list.
+ * @param protocol_fallback_count The amount of pings to perform before change the protocol.
  */
 PingRotate::PingRotate(
         io_service &io_serv,
@@ -53,7 +54,8 @@ PingRotate::PingRotate(
         const string &destination_address,
         const uint16_t destination_port,
         const string &nameserver,
-        const PingProtocolList protocol_list
+        const PingProtocolList protocol_list,
+        const int protocol_fallback_count
 ) :
     IoService( io_serv ),
     NetworkInterfaceName( network_interface ),
@@ -62,6 +64,8 @@ PingRotate::PingRotate(
     DestinationPort( destination_port ),
     Nameserver( nameserver ),
     ProtocolList( protocol_list ),
+    ProtocolFallbackCount( protocol_fallback_count ),
+    PingCount( 0 ),
     Ping(),
     PingDoneCallback()
 {
@@ -71,7 +75,7 @@ PingRotate::PingRotate(
     BOOST_ASSERT( !nameserver.empty() );
     BOOST_ASSERT( 0 < protocol_list.size() );
 
-    update_ping_protocol();
+    get_next_ping_protocol();
 }
 
 /**
@@ -91,6 +95,7 @@ PingRotate::~PingRotate()
 void PingRotate::ping( function<void(bool)> ping_done_callback )
 {
     BOOST_ASSERT( ( 0 < DestinationPort ) && ( DestinationPort < numeric_limits<uint16_t>::max() ) );
+    BOOST_ASSERT( ( 0 <= PingCount ) && ( PingCount < numeric_limits<int>::max() ) );
 
     set_ping_done_callback( ping_done_callback );
 
@@ -103,6 +108,8 @@ void PingRotate::ping( function<void(bool)> ping_done_callback )
             DestinationPort,
             boost::bind(&PingRotate::ping_done_handler, this, _1)
     );
+
+    PingCount++;
 }
 
 bool PingRotate::resolve_ping_address()
@@ -154,9 +161,13 @@ void PingRotate::get_next_ping_protocol()
 
 bool PingRotate::can_change_ping_protocol() const
 {
+    BOOST_ASSERT( ( 0 <= ProtocolFallbackCount ) && ( ProtocolFallbackCount < numeric_limits<int>::max() ) );
+    BOOST_ASSERT( ( 0 <= PingCount ) && ( PingCount < numeric_limits<int>::max() ) );
+
     // TODO can_change_ping_protocol() and get_next_ping_protocol() may be implemented in a Algorithm
     // class that can be exchanged in this class to provide an algorithm neutral class
-    return true;
+
+    return ( PingCount >= ProtocolFallbackCount );
 }
 
 void PingRotate::update_dns_resolver( PingProtocol current_protocol )
index f666d11..61b0f3c 100644 (file)
@@ -18,8 +18,8 @@
  on this file might be covered by the GNU General Public License.
  */
 
-#ifndef PIN_GROTATE_H
-#define PIN_GROTATE_H
+#ifndef PING_ROTATE_H
+#define PING_ROTATE_H
 
 #include <stdint.h>
 
@@ -51,7 +51,8 @@ public:
             const std::string &destination_address,
             const uint16_t destination_port,
             const std::string &nameserver,
-            const PingProtocolList protocol_list
+            const PingProtocolList protocol_list,
+            const int protocol_fallback_count
     );
     virtual ~PingRotate();
 
@@ -93,6 +94,10 @@ private:
     std::string Nameserver;
     /// The list of protocols to ping
     PingProtocolList ProtocolList;
+    /// The amount of pings to perform before change the protocol
+    const int ProtocolFallbackCount;
+    /// The amount of pings performed
+    int PingCount;
     /// Internal boost pinger object
     PingerItem Ping;
     /// The callback function
index 6138afa..ea0b128 100644 (file)
@@ -53,6 +53,7 @@ using I2n::Logger::GlobalLogger;
  * @param destination_address The remote address to ping.
  * @param destination_port The remote port to ping.
  * @param ping_protocol_list A list of protocols to use.
+ * @param ping_protocol_fallback_count The amount of pings to perform before change the protocol.
  * @param ping_interval_in_sec Amount of time between each ping.
  * @param ping_fail_percentage_limit Maximum amount of pings that can fail.
  * @param nameserver Server to resolve the addresses.
@@ -63,6 +64,7 @@ PingScheduler::PingScheduler(
         const string &destination_address,
         const uint16_t destination_port,
         const PingProtocolList ping_protocol_list,
+        const int protocol_fallback_count,
         const long ping_interval_in_sec,
         const int ping_fail_percentage_limit,
         const string &nameserver,
@@ -87,6 +89,7 @@ PingScheduler::PingScheduler(
 
     Ping = PingerFactory::createPinger(
             ping_protocol_list,
+            protocol_fallback_count,
             IoService,
             network_interface,
             destination_address,
index 583ff44..0029297 100644 (file)
@@ -53,6 +53,7 @@ public:
             const std::string &destination_address,
             const uint16_t destination_port,
             const PingProtocolList ping_protocol_list,
+            const int protocol_fallback_count,
             const long ping_interval_in_sec,
             const int ping_fail_percentage_limit,
             const std::string &nameserver,
index 17f685f..16fc853 100644 (file)
@@ -123,6 +123,7 @@ void init_pingers(
         string destination_address = host->get_address();
         uint16_t destination_port = host->get_port();
         PingProtocolList protocol_list = host->get_ping_protocol_list();
+        int protocol_fallback_count = host->get_ping_protocol_fallback_count();
         int ping_interval_in_sec = host->get_interval_in_sec();
         PingSchedulerItem scheduler(
                 new PingScheduler(
@@ -130,6 +131,7 @@ void init_pingers(
                         destination_address,
                         destination_port,
                         protocol_list,
+                        protocol_fallback_count,
                         ping_interval_in_sec,
                         ping_fail_limit,
                         nameserver,