created and passed first unit tests for DNS; finished recovery from PingScheduler...
[pingcheck] / src / host / pingscheduler.cpp
index 694f6a7..e2267fd 100644 (file)
@@ -89,7 +89,7 @@ PingScheduler::PingScheduler(
     Ping(),
     WantToPing( false ),
     LogPrefix(),
-    ContinueOnOutdatedIps( false );
+    ContinueOnOutdatedIps( false )
 {
     BOOST_ASSERT( !network_interface.empty() );
     BOOST_ASSERT( !destination_address.empty() );
@@ -161,11 +161,11 @@ void PingScheduler::ping(const boost::system::error_code &error)
 
     // ping as soon as dns is ready
     WantToPing = true;
-    try_to_ping();
+    ping_when_ready();
 }
 
 
-void PingScheduler::try_to_ping()
+void PingScheduler::ping_when_ready()
 {
     if ( !WantToPing )
     {
@@ -184,9 +184,11 @@ void PingScheduler::try_to_ping()
     GlobalLogger.info() << "PingScheduler: start ping";
     WantToPing = false;
 
+    // try to get an up-to-date IP
     HostAddress ip = Resolver->get_next_ip();
+
     if ( !ip.is_valid() && ContinueOnOutdatedIps)
-    {
+    {   // we failed to resolve --> try to use outdated IP
         GlobalLogger.info() << LogPrefix << "Checking for outdated IPs";
         bool check_up_to_date = false;
         ip = Resolver->get_next_ip(check_up_to_date);
@@ -202,9 +204,12 @@ void PingScheduler::try_to_ping()
         GlobalLogger.error() << LogPrefix << "No IP to ping "
                              << "-- this should not have happened!!";
         WantToPing = true;
-        if ( !Resolver.is_resolving() )
+        if ( !Resolver->is_resolving() )
             start_resolving_ping_address();
     }
+
+    // next time try with up-to-date IP
+    ContinueOnOutdatedIps = false;
 }
 
 
@@ -315,7 +320,7 @@ bool PingScheduler::can_change_ping_protocol() const
 //------------------------------------------------------------------------------
 
 // show "!" after host name if running on outdated IPs
-void update_log_prefix()
+void PingScheduler::update_log_prefix()
 {
     std::stringstream temp;
     temp << "PS(" << DestinationAddress;
@@ -346,7 +351,7 @@ void PingScheduler::update_dns_resolver( PingProtocol current_protocol )
             GlobalLogger.warning() << "PingScheduler: have up to date IPs but "
                 << "resolver seems to be resolving all the same... "
                 << "Start pinging anyway!";
-        try_to_ping();
+        ping_when_ready();
     }
     else
         start_resolving_ping_address();
@@ -367,36 +372,38 @@ void PingScheduler::dns_resolve_callback(const bool was_success,
 
     // TODO this is too simple, but need to think more about how to update here!
     // (may have to switch back some time to resolver for original host or so
-    ContinueOnOutdatedIps = true;
+    ContinueOnOutdatedIps = !was_success;
     update_log_prefix();
 
     if ( was_success )
     {
         HostAnalyzer.set_resolved_ip_count( Resolver->get_resolved_ip_count());
-        try_to_ping();
+        ping_when_ready();
     }
     else
     {   // host name resolution failed; try again bypassing first outdated CNAME
+        // or using cached IP
 
         std::string skip_host = Resolver->get_skip_cname();
 
         if (skip_host.empty())
-        {
+        {   // continue with IP
             GlobalLogger.notice() << LogPrefix << "DNS failed, "
                 << "try anyway with cached data";
             HostAnalyzer.set_resolved_ip_count(0);
-            try_to_ping();
+            ping_when_ready();
         }
         else
-        {
+        {   // have CNAME to continue
             GlobalLogger.notice() << LogPrefix << "DNS failed, "
                 << "try again skipping a CNAME and resolving "
                 << skip_host << " directly";
             Resolver = DnsMaster::get_instance()
                                    ->get_resolver_for(skip_host, *ProtocolIter);
-            // the original resolver is still alive and cached by DnsMaster
-            //   and counting down the time to re-try on its own
             start_resolving_ping_address();
+
+            // (the original resolver is still alive and cached by DnsMaster and
+            //  counting down time to re-try on its own until cancel_resolve)
         }
     }
 }