renamed IcmpPinger::handle_ping_done to handle_timeout; handle error_codes from timer...
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 12 Jan 2015 15:54:44 +0000 (16:54 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 12 Jan 2015 15:54:44 +0000 (16:54 +0100)
src/host/pingscheduler.cpp
src/host/pingscheduler.h
src/icmp/icmppinger.cpp
src/icmp/icmppinger.h

index 65b5a3e..08f8c3d 100644 (file)
@@ -130,10 +130,11 @@ void PingScheduler::start_pinging()
     {
         GlobalLogger.info() << "Delaying first ping by " << FirstDelay << "s";
         (void) NextPingTimer.expires_from_now( seconds( FirstDelay ) );
-        NextPingTimer.async_wait( bind( &PingScheduler::resolve_and_ping, this ) );
+        NextPingTimer.async_wait( bind( &PingScheduler::resolve_and_ping, this,
+                                              boost::asio::placeholders::error ) );
     }
     else
-        resolve_and_ping();
+        resolve_and_ping(boost::system::error_code());
 }
 
 
@@ -142,8 +143,18 @@ void PingScheduler::start_pinging()
  *   call ping;
  *   If name resolution fails too often, reports this to HostAnalyzer
  */
-void PingScheduler::resolve_and_ping()
+void PingScheduler::resolve_and_ping(const boost::system::error_code &error)
 {
+    if ( error )
+    {
+        if ( error ==  boost::asio::error::operation_aborted )
+            GlobalLogger.error() << "Timer for resolve_and_ping was cancelled! Stopping" << endl;
+        else
+            GlobalLogger.error() << "Received error " << error
+                                 << " waiting for resolve_and_ping! Stopping" << endl;
+        return;
+    }
+
     bool ips_up_to_date = EverHadAnyIP && !Ping->expired_resolved_ip();
 
     // determine if address resolution is required
@@ -175,7 +186,8 @@ void PingScheduler::resolve_and_ping()
             HostAnalyzer.report_dns_resolution_failure();
         }
         (void) NextAddressTimer.expires_from_now( seconds( AddressResolveIntervalInSec ) );
-        NextAddressTimer.async_wait( bind( &PingScheduler::resolve_and_ping, this ) );
+        NextAddressTimer.async_wait( bind( &PingScheduler::resolve_and_ping, this,
+                                                 boost::asio::placeholders::error ) );
     }
 }
 
@@ -206,7 +218,8 @@ void PingScheduler::ping_done_handler( const bool ping_success )
 
     // schedule next ping
     (void) NextPingTimer.expires_from_now( seconds( PingIntervalInSec ) );
-    NextPingTimer.async_wait( bind( &PingScheduler::resolve_and_ping, this ) );
+    NextPingTimer.async_wait( bind( &PingScheduler::resolve_and_ping, this,
+                                         boost::asio::placeholders::error ) );
 }
 
 void PingScheduler::update_ping_interval()
index 1cdeaec..8cd0b59 100644 (file)
@@ -72,7 +72,7 @@ private:
     // Methods
     //
 
-    void resolve_and_ping();
+    void resolve_and_ping(const boost::system::error_code &error);
     bool resolve_address();
     void force_address_resolution();
 
index 7e98d03..f0079e0 100644 (file)
@@ -200,7 +200,7 @@ void IcmpPinger::schedule_timeout_echo_reply()
             TimeSent + seconds( EchoReplyTimeoutInSec )
     );
     IcmpPacketReceiveTimer.async_wait(
-            boost::bind( &IcmpPinger::handle_ping_done, this )
+            boost::bind( &IcmpPinger::handle_timeout, this, boost::asio::placeholders::error )
     );
 }
 
@@ -209,8 +209,23 @@ void IcmpPinger::schedule_timeout_echo_reply()
  *
  * @return void
  **/
-void IcmpPinger::handle_ping_done()
+void IcmpPinger::handle_timeout(const boost::system::error_code& error)
 {
+    if (error)
+    {
+        if ( error ==  boost::asio::error::operation_aborted )
+        {
+            if (! ReplyReceived)
+                GlobalLogger.notice() << "Timer waiting for ICMP echo reply was cancelled!" << endl;
+            // otherwise probably called by IcmpPacketReceiveTimer.cancel in handle_receive_icmp_packet!
+        }
+        else
+            GlobalLogger.notice() << "Error " << error << " waiting for ICMP echo reply!" << endl;
+
+        // Still continue with rest of function, so PingStatus is updated and Callback executed
+        //   when timer was cancelled
+    }
+
     // Check ReplyReceived as the timer handler
     // is also called by Timer.cancel();
     if ( !ReplyReceived )
@@ -240,7 +255,8 @@ void IcmpPinger::start_receive()
     // Waiting for a reply, We prepare the buffer to receive up to SOCKET_BUFFER_SIZE bytes
     Socket.async_receive(
             ReplyBuffer.prepare( SOCKET_BUFFER_SIZE ),
-            boost::bind( &IcmpPinger::handle_receive_icmp_packet, this, _1, _2 )
+            boost::bind( &IcmpPinger::handle_receive_icmp_packet, this, boost::asio::placeholders::error,
+                                                                        boost::asio::placeholders::bytes_transferred )
     );
     ReceiveHandlerInPlace = true;
 }
index 715ab62..b9fb752 100644 (file)
@@ -51,7 +51,7 @@ private:
     bool start_send();
     bool send_echo_request( const IcmpPacketItem icmp_packet );
     void schedule_timeout_echo_reply();
-    void handle_ping_done();
+    void handle_timeout(const boost::system::error_code &error);
 
     void start_receive();
     void handle_receive_icmp_packet( const boost::system::error_code &error,