made PingRotate actually rotate through protocols, and not just stick with last protocol
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 25 Mar 2015 13:09:24 +0000 (14:09 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 25 Mar 2015 13:09:24 +0000 (14:09 +0100)
src/host/pingrotate.cpp
src/host/pingrotate.h

index 20dd11e..64b0587 100644 (file)
@@ -21,6 +21,7 @@
 #include "host/pingrotate.h"
 
 #include <boost/bind.hpp>
+#include <boost/foreach.hpp>
 
 #include "boost_assert_handler.h"
 #include "dns/dnsresolverfactory.h"
@@ -64,7 +65,7 @@ PingRotate::PingRotate(
     Nameserver( nameserver ),
     ResolvedIpTtlThreshold( resolved_ip_ttl_threshold ),
     PingReplyTimeout( ping_reply_timeout ),
-    ProtocolList( protocol_list ),
+    ProtocolRotate( protocol_list.size() ),
     Ping(),
     PingDoneCallback()
 {
@@ -74,6 +75,10 @@ PingRotate::PingRotate(
     BOOST_ASSERT( !nameserver.empty() );
     BOOST_ASSERT( 0 < protocol_list.size() );
 
+    // fill circular buffer with protocols
+    BOOST_FOREACH( const PingProtocol &prot, protocol_list )
+        ProtocolRotate.push_back(prot);
+
     init_ping_protocol();
 }
 
@@ -153,16 +158,13 @@ void PingRotate::update_ping_protocol()
 
 void PingRotate::get_next_ping_protocol()
 {
-    if ( 1 <= ProtocolList.size() )
-    {
-        PingProtocol ping_protocol = ProtocolList.front();
+    PingProtocol ping_protocol = ProtocolRotate.front();
+    ProtocolRotate.pop_front();
+    ProtocolRotate.push_back(ping_protocol);
 
-        ProtocolList.pop_front();
+    Ping = PingerFactory::createPinger( ping_protocol, IoService, NetworkInterfaceName, PingReplyTimeout );
 
-        Ping = PingerFactory::createPinger( ping_protocol, IoService, NetworkInterfaceName, PingReplyTimeout );
-
-        update_dns_resolver( ping_protocol );
-    }
+    update_dns_resolver( ping_protocol );
 }
 
 bool PingRotate::can_change_ping_protocol() const
index e6e5367..f755f94 100644 (file)
@@ -28,6 +28,7 @@
 #include <boost/asio.hpp>
 #include <boost/function.hpp>
 #include <boost/shared_ptr.hpp>
+#include <boost/circular_buffer.hpp>
 
 #include "dns/dnsresolver.h"
 #include "host/pinger.h"
@@ -37,6 +38,8 @@
 // PingRotate
 //-----------------------------------------------------------------------------
 
+typedef boost::circular_buffer<PingProtocol> CircularProtocolList;
+
 /**
  * @brief This class is a wrapper to the Pingers, and serves to alternate
  * between protocols.
@@ -101,7 +104,7 @@ private:
     /// timeout for ping reply
     const int PingReplyTimeout;
     /// The list of protocols to ping
-    PingProtocolList ProtocolList;
+    CircularProtocolList ProtocolRotate;
     /// Internal boost pinger object
     PingerItem Ping;
     /// The callback function