The BoostPinger now pings N times, instead of infinite pinging
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Mon, 28 Feb 2011 08:31:09 +0000 (09:31 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Mon, 28 Feb 2011 08:31:09 +0000 (09:31 +0100)
- moved the io_service calls to inside the BoostPinger
- splited the BoostPinger::start_send(), the code responsible to send is inside BoostPinger::send()

1  2 
src/main.cpp
src/ping/boostpinger.cpp
src/ping/boostpinger.h

diff --cc 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 )
          {
@@@ -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;
      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.
              && 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();
@@@ -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;