#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;
) :
IoService( io_serv ),
NetworkInterfaceName( network_interface ),
- IpList( destination_address, nameserver ),
+ IpList(),
+ DestinationAddess( destination_address ),
DestinationPort( destination_port ),
+ Nameserver( nameserver ),
ProtocolList( protocol_list ),
Ping(),
PingDoneCallback()
BOOST_ASSERT( 0 < destination_port );
BOOST_ASSERT( !nameserver.empty() );
BOOST_ASSERT( 0 < protocol_list.size() );
+
+ update_ping_protocol();
}
/**
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,
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 )
{
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();
+}
void get_next_ping_protocol();
bool can_change_ping_protocol() const;
+ void update_dns_resolver( PingProtocol current_protocol );
+
//
// Attributes
//
/// 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