The DNS address resolution is synchronized with the protocol change.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Mon, 5 Mar 2012 11:18:46 +0000 (08:18 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Mon, 5 Mar 2012 11:22:14 +0000 (08:22 -0300)
- In this way, when the new protocol requires a different IP version, the DNS
resolver is called again to get the according Address Resource Record.
- Still needs some intelligence.

src/host/pingrotate.cpp
src/host/pingrotate.h

index 0b0ce8b..4d11fde 100644 (file)
 
 #include "host/pingrotate.h"
 
-#include <algorithm>
-
 #include <boost/assert.hpp>
 #include <boost/bind.hpp>
 
+#include "dns/dnsresolverfactory.h"
 #include "host/pingerfactory.h"
 
 using namespace std;
@@ -58,8 +57,10 @@ PingRotate::PingRotate(
 ) :
     IoService( io_serv ),
     NetworkInterfaceName( network_interface ),
-    IpList( destination_address, nameserver ),
+    IpList(),
+    DestinationAddess( destination_address ),
     DestinationPort( destination_port ),
+    Nameserver( nameserver ),
     ProtocolList( protocol_list ),
     Ping(),
     PingDoneCallback()
@@ -69,6 +70,8 @@ PingRotate::PingRotate(
     BOOST_ASSERT( 0 < destination_port );
     BOOST_ASSERT( !nameserver.empty() );
     BOOST_ASSERT( 0 < protocol_list.size() );
+
+    update_ping_protocol();
 }
 
 /**
@@ -91,10 +94,10 @@ void PingRotate::ping( function<void(bool)> ping_done_callback )
 
     set_ping_done_callback( ping_done_callback );
 
-    string destination_ip = IpList.get_next_ip();
-
     update_ping_protocol();
 
+    string destination_ip = IpList->get_next_ip();
+
     Ping->ping(
             destination_ip,
             DestinationPort,
@@ -104,17 +107,17 @@ void PingRotate::ping( function<void(bool)> ping_done_callback )
 
 bool PingRotate::resolve_ping_address()
 {
-    return IpList.resolve();
+    return IpList->resolve();
 }
 
 int PingRotate::get_resolved_ip_count() const
 {
-    return IpList.get_resolved_ip_count();
+    return IpList->get_resolved_ip_count();
 }
 
 bool PingRotate::expired_resolved_ip() const
 {
-    return IpList.expired_resolved_ip();
+    return IpList->expired_resolved_ip();
 }
 
 void PingRotate::set_ping_done_callback( function<void(bool)> ping_done_callback )
@@ -141,13 +144,27 @@ void PingRotate::get_next_ping_protocol()
     {
         PingProtocol ping_protocol = ProtocolList.front();
 
-        rotate( ProtocolList.begin(), ++ProtocolList.begin(), ProtocolList.end() );
+        ProtocolList.pop_front();
 
         Ping = PingerFactory::createPinger( ping_protocol, IoService, NetworkInterfaceName );
+
+        update_dns_resolver( ping_protocol );
     }
 }
 
 bool PingRotate::can_change_ping_protocol() const
 {
+    // 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;
 }
+
+void PingRotate::update_dns_resolver( PingProtocol current_protocol )
+{
+    IpList = DnsResolverFactory::createResolver( DestinationAddess, Nameserver, current_protocol );
+
+    // when the protocol change, there is a chance that the IP version required by the new
+    // protocol differ from the previous one, thus it is required to resolve the address again.
+    // FIXME add some intelligence here to decide if the new protocol requires to update the DNS
+    resolve_ping_address();
+}
index 90df802..ab49f59 100644 (file)
@@ -73,6 +73,8 @@ private:
     void get_next_ping_protocol();
     bool can_change_ping_protocol() const;
 
+    void update_dns_resolver( PingProtocol current_protocol );
+
     //
     // Attributes
     //
@@ -82,9 +84,13 @@ private:
     /// The network interface name
     std::string NetworkInterfaceName;
     /// The list of IPs which are aliases to the host DNS
-    DnsResolver IpList;
+    DnsResolverItem IpList;
+    /// The address to ping
+    std::string DestinationAddess;
     /// The port to ping at destination host (same port to all protocols in the list)
     uint16_t DestinationPort;
+    /// Server to resolve the addresses
+    std::string Nameserver;
     /// The list of protocols to ping
     PingProtocolList ProtocolList;
     /// Internal boost pinger object