#include "ipv4header.h"
#include "boostpinger.h"
-namespace posix_time = boost::posix_time;
+using namespace std;
+using namespace boost::asio;
+using namespace boost::asio::ip;
+using namespace boost::posix_time;
//-----------------------------------------------------------------------------
// BoostPinger
Socket( io_service, icmp::v4() ),
Timer( io_service ),
SequenceNumber( 0 ),
- TimeSent( posix_time::microsec_clock::universal_time() ),
+ TimeSent( microsec_clock::universal_time() ),
ReplyBuffer(),
RepliesCount( 0 ),
TimesToPingTotal( 0 ),
MinTimesToPing( 0 ),
- MaxTimesToPing( std::numeric_limits<std::size_t>::max() )
+ MaxTimesToPing( numeric_limits<size_t>::max() )
{
}
}
void BoostPinger::ping(
- const std::string &destination,
- const std::size_t times_to_ping
+ const string &destination,
+ const size_t times_to_ping
)
{
BOOST_ASSERT( !destination.empty() );
{
IcmpPacket icmp_echo_request_packet = create_echo_request_packet();
- std::size_t times_already_pinged = SequenceNumber;
+ size_t times_already_pinged = SequenceNumber;
if ( times_already_pinged <= TimesToPingTotal )
{
send_echo_request( icmp_echo_request_packet );
void BoostPinger::send_echo_request( const IcmpPacket &icmp_packet )
{
boost::asio::streambuf request_buffer;
- std::ostream os( &request_buffer );
+ ostream os( &request_buffer );
os << icmp_packet;
// Send the request.
- TimeSent = posix_time::microsec_clock::universal_time();
+ TimeSent = microsec_clock::universal_time();
Socket.send_to( request_buffer.data(), DestinationEndpoint );
// Wait up to five seconds for a reply.
RepliesCount = 0;
- Timer.expires_at( TimeSent + posix_time::seconds( 5 ) );
+ Timer.expires_at( TimeSent + seconds( 5 ) );
Timer.async_wait( boost::bind( &BoostPinger::handle_timeout, this ) );
}
void BoostPinger::handle_timeout()
{
if ( RepliesCount == 0 )
- std::cout << "Request timed out" << std::endl;
+ cout << "Request timed out" << endl;
// Requests must be sent no less than one second apart.
- Timer.expires_at( TimeSent + posix_time::seconds( 1 ) );
+ Timer.expires_at( TimeSent + seconds( 1 ) );
Timer.async_wait( boost::bind( &BoostPinger::start_send, this ) );
}
);
}
-void BoostPinger::handle_receive( const std::size_t &length )
+void BoostPinger::handle_receive( const size_t &length )
{
// The actual number of bytes received is committed to the buffer so that we
// can extract it using a std::istream object.
ReplyBuffer.commit( length );
// Decode the reply packet.
- std::istream is( &ReplyBuffer );
+ istream is( &ReplyBuffer );
IcmpPacket icmp_packet;
is >> icmp_packet;
void BoostPinger::print_echo_reply(
const IcmpPacket &icmp_packet,
- const std::size_t &length
+ const size_t &length
)
{
Ipv4Header ipv4_hdr = icmp_packet.get_ip_header();
IcmpHeader icmp_hdr = icmp_packet.get_icmp_header();
- std::size_t bytes_received = length - ipv4_hdr.get_header_length();
- std::string source_address = ipv4_hdr.get_source_address().to_string();
- std::size_t sequence_number = icmp_hdr.get_sequence_number();
- std::size_t ttl = ipv4_hdr.get_time_to_live();
- posix_time::ptime now = posix_time::microsec_clock::universal_time();
- std::size_t time = (now - TimeSent).total_milliseconds();
+ size_t bytes_received = length - ipv4_hdr.get_header_length();
+ string source_address = ipv4_hdr.get_source_address().to_string();
+ size_t sequence_number = icmp_hdr.get_sequence_number();
+ size_t ttl = ipv4_hdr.get_time_to_live();
+ ptime now = microsec_clock::universal_time();
+ size_t time = (now - TimeSent).total_milliseconds();
- std::cout << bytes_received << " bytes "
+ cout << bytes_received << " bytes "
<< "from " << source_address
<< ": icmp_seq=" << sequence_number
<< " ttl=" << ttl
- << " time=" << time << " ms" << std::endl;
+ << " time=" << time << " ms" << endl;
}
uint16_t BoostPinger::get_identifier()
#include "pingcheck.h"
+using namespace std;
+using namespace boost::asio;
+
//-----------------------------------------------------------------------------
// PingCheck
//-----------------------------------------------------------------------------
void PingCheck::start_pinging()
{
- std::cout << "[start_pinging]" << std::endl;
-
- std::string destination = Host_.get_address();
- std::size_t ping_set_total = 3; // TODO configurable:
- std::size_t ping_set_count = 0;
+ string destination = Host_.get_address();
+ size_t ping_set_total = 3; // TODO configurable:
+ size_t ping_set_count = 0;
while ( ping_set_count < ping_set_total )
{
ping_and_wait( destination );
}
}
-void PingCheck::ping_and_wait( const std::string &destination )
+void PingCheck::ping_and_wait( const string &destination )
{
- std::cout << "[ping_and_wait]" << std::endl;
-
try
{
ping( destination );
wait();
}
- catch ( std::exception& e )
+ catch ( exception& e )
{
- std::cerr << "Exception: " << e.what() << std::endl;
+ cerr << "Exception: " << e.what() << endl;
return;
}
}
-void PingCheck::ping( const std::string &destination ) throw ( std::exception& )
+void PingCheck::ping( const string &destination ) throw ( exception& )
{
- std::cout << "[ping]" << std::endl;
-
- std::size_t times_to_ping = 3; // TODO configurable: this must be automatically selected
+ size_t times_to_ping = 3; // TODO configurable: this must be automatically selected
boost::asio::io_service io_service;
BoostPinger pinger( io_service );
pinger.ping( destination, times_to_ping );
void PingCheck::wait()
{
- std::cout << "[wait]" << std::endl;
-
boost::asio::io_service io_service;
deadline_timer timer( io_service );
- std::size_t interval = Host_.get_interval(); // TODO configurable:
+ size_t interval = Host_.get_interval(); // TODO configurable:
timer.expires_from_now( boost::posix_time::seconds( interval ) );
timer.wait();
}