From: Guilherme Maciel Ferreira Date: Mon, 28 Feb 2011 08:31:09 +0000 (+0100) Subject: The BoostPinger now pings N times, instead of infinite pinging X-Git-Tag: v1.0~176 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=a8d411d6af268bec6d47b95519ea3de6172c85dd;p=pingcheck The BoostPinger now pings N times, instead of infinite pinging - moved the io_service calls to inside the BoostPinger - splited the BoostPinger::start_send(), the code responsible to send is inside BoostPinger::send() --- a8d411d6af268bec6d47b95519ea3de6172c85dd diff --cc src/main.cpp index 5ce79f6,5ce79f6..b2e005b --- a/src/main.cpp +++ b/src/main.cpp @@@ -24,7 -24,7 +24,6 @@@ int main( int argc, char* argv[] Configuration config = config_reader.get_configuration(); Host host = config.get_host(); p.ping( host ); -- io_service.run(); } catch ( std::exception& e ) { diff --cc src/ping/boostpinger.cpp index 4968f99,4968f99..044b151 --- a/src/ping/boostpinger.cpp +++ b/src/ping/boostpinger.cpp @@@ -22,6 -22,6 +22,7 @@@ namespace posix_time = boost::posix_tim BoostPinger::BoostPinger( boost::asio::io_service& io_service ) : ++ io_service( io_service ), Resolver( io_service ), DestinationEndpoint(), Socket( io_service, icmp::v4() ), @@@ -39,12 -39,12 +40,17 @@@ BoostPinger::~BoostPinger( void BoostPinger::ping( const Host &host ) { ++ BOOST_ASSERT( !host.get_address().empty() ); ++ BOOST_ASSERT( ( 0 < host.get_interval() ) && ( host.get_interval() < UINT_MAX ) ); ++ std::string destination = host.get_address(); icmp::resolver::query query( icmp::v4(), destination, "" ); DestinationEndpoint = *Resolver.resolve( query ); start_send(); start_receive(); ++ ++ io_service.run(); } void BoostPinger::start_send() @@@ -52,7 -52,7 +58,6 @@@ IcmpBody body( "ping message" ); // Create an ICMP header for an echo request. -- SequenceNumber++; IcmpHeader::IcmpType type = IcmpHeader::EchoRequest; uint8_t code = 0; @@@ -66,6 -66,6 +71,19 @@@ std::ostream os( &request_buffer ); os << echo_request << body; ++ int ping_times = SequenceNumber; ++ if ( ping_times < 4 ) ++ { ++ send( request_buffer ); ++ } ++ else ++ { ++ io_service.stop(); ++ } ++} ++ ++void BoostPinger::send( const boost::asio::streambuf& request_buffer ) ++{ // Send the request. TimeSent = posix_time::microsec_clock::universal_time(); Socket.send_to( request_buffer.data(), DestinationEndpoint ); @@@ -98,7 -98,7 +116,7 @@@ void BoostPinger::start_receive( ); } --void BoostPinger::handle_receive( std::size_t length ) ++void BoostPinger::handle_receive( const std::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. @@@ -117,7 -117,7 +135,6 @@@ && icmp_hdr.get_identifier() == get_identifier() && icmp_hdr.get_sequence_number() == SequenceNumber ) { -- // If this is the first reply, interrupt the five second timeout. if ( NumReplies == 0 ) Timer.cancel(); diff --cc src/ping/boostpinger.h index dcfbf7d,dcfbf7d..af2653c --- a/src/ping/boostpinger.h +++ b/src/ping/boostpinger.h @@@ -23,14 -23,14 +23,16 @@@ public private: void start_send(); -- ++ void send( const boost::asio::streambuf& request_buffer ); void handle_timeout(); ++ void start_receive(); -- void handle_receive( std::size_t length ); ++ void handle_receive( const std::size_t length ); static uint16_t get_identifier(); private: ++ boost::asio::io_service& io_service; icmp::resolver Resolver; icmp::endpoint DestinationEndpoint; icmp::socket Socket;