void BoostPinger::start_send()
{
- IcmpPacket icmp_echo_request_packet = create_echo_request_packet();
+ IcmpPacket icmp_echo_request_packet = create_echo_request();
uint times_already_pinged = SequenceNumber;
if ( times_already_pinged <= TimesToPingTotal )
}
}
-IcmpPacket BoostPinger::create_echo_request_packet()
+IcmpPacket BoostPinger::create_echo_request()
{
IcmpData icmp_data( "ping-message" );
TimeSent = microsec_clock::universal_time();
Socket.send_to( request_buffer.data(), DestinationEndpoint );
- // Wait up to five seconds for a reply.
+ schedule_timeout_echo_reply();
+}
+
+void BoostPinger::schedule_timeout_echo_reply()
+{
+ // Wait up to N seconds for a reply.
RepliesCount = 0;
- Timer.expires_at( TimeSent + seconds( 5 ) ); // TODO configurable:
+ const uint echo_reply_timeout_in_sec = 5; // TODO configurable: this is the timeout to WAIT FOR the ping before considering a timeout
+ Timer.expires_at( TimeSent + seconds( echo_reply_timeout_in_sec ) );
Timer.async_wait( boost::bind( &BoostPinger::handle_timeout, this ) );
}
if ( RepliesCount == 0 )
cout << "Request timed out" << endl;
+ schedule_next_echo_request();
+}
+
+void BoostPinger::schedule_next_echo_request()
+{
// Requests must be sent no less than one second apart.
- Timer.expires_at( TimeSent + seconds( 1 ) );
+ const uint echo_request_interval_in_sec = 1;
+ Timer.expires_at( TimeSent + seconds( echo_request_interval_in_sec ) );
Timer.async_wait( boost::bind( &BoostPinger::start_send, this ) );
}
++RepliesCount;
- // Print out some information about the reply packet.
print_echo_reply( icmp_packet, bytes_transferred );
}
void BoostPinger::print_echo_reply(
const IcmpPacket &icmp_packet,
const size_t &bytes_transferred
-)
+) const
{
Ipv4Header ipv4_hdr = icmp_packet.get_ip_header();
IcmpHeader icmp_hdr = icmp_packet.get_icmp_header();
<< " time=" << time << " ms" << endl;
}
-uint16_t BoostPinger::get_identifier()
+uint16_t BoostPinger::get_identifier() const
{
return static_cast<uint16_t> ( ::getpid() );
}
void stop_pinger();
void start_send();
- IcmpPacket create_echo_request_packet();
+ IcmpPacket create_echo_request();
void send_echo_request( const IcmpPacket &icmp_packet );
+ void schedule_timeout_echo_reply();
void handle_timeout();
+ void schedule_next_echo_request();
void start_receive();
void handle_receive( const std::size_t &bytes_transferred );
void print_echo_reply(
const IcmpPacket &icmp_packet,
const std::size_t &bytes_transferred
- );
+ ) const;
- uint16_t get_identifier();
+ uint16_t get_identifier() const;
private:
boost::asio::io_service &io_service;