Implement dialup mode
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 11 Oct 2010 10:38:06 +0000 (12:38 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 11 Oct 2010 10:38:06 +0000 (12:38 +0200)
config/bpdyndnsd.conf
src/logger.cpp
src/logger.hpp
src/main.cpp

index acd969a..87e39b5 100644 (file)
@@ -18,4 +18,4 @@ syslog=1
 # in which we act normally and then keep quiet.
 dialup_mode=0
 dialup_burst_period_seconds=120
-dialup_sleep_seconds=
+dialup_sleep_seconds=600
index 8f42a3e..4df181e 100644 (file)
@@ -736,6 +736,21 @@ void Logger::print_offline_mode() const
 
 
 /**
+ * Dialup mode: Print how long we plan to delay the next network traffic
+ * @param sleep_until Timestamp until we plan to keep quiet
+ */
+void Logger::print_sleep_dialup_mode(time_t sleep_until) const
+{
+    int level = 1;
+    if ( level <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Skipped update in dialup mode (" << sleep_until-time(NULL) << " more seconds)" << endl;
+        log_error(msg.str());
+    }
+}
+
+/**
  * Objects successfully serialized.
  */
 void Logger::print_serialized_objects_success() const
index c2f6609..8a27831 100644 (file)
@@ -136,6 +136,8 @@ public:
 
     void print_offline_mode() const;
 
+    void print_sleep_dialup_mode(time_t sleep_until) const;
+
     void print_serialized_objects_success() const;
 
     void print_deserialized_objects_success() const;
index a801d89..4fcf1ed 100644 (file)
@@ -293,6 +293,18 @@ int init_daemon_mode(bool daemon_mode)
 }
 
 /**
+ * Calculate how long the dialup mode burst period will last (dialup mode only)
+ * @return Timestamp until dialup period lasts or zero if dialup mode is disabled
+*/
+time_t calc_dialup_mode_burst_period()
+{
+    if (updater->get_config()->get_dialup_mode() )
+        return time(NULL) + updater->get_config()->get_dialup_burst_period_seconds();
+
+    return 0;
+}
+
+/**
  * @brief The main part.
  * @param argc Number of arguments
  * @param argv Command line arguments
@@ -334,6 +346,10 @@ int main(int argc, char *argv[])
 
     int original_log_level = updater->get_logger()->get_loglevel();
 
+    // Dialup mode: Sleep a specified number of seconds between network traffic
+    time_t dialup_mode_burst_mode_until = calc_dialup_mode_burst_period();
+    time_t dialup_mode_sleep_until = 0;
+
     // service processing starts here
     do
     {
@@ -353,6 +369,7 @@ int main(int argc, char *argv[])
             // Go online
             is_online = true;
             webcheck_enabled = false;
+            dialup_mode_burst_mode_until = calc_dialup_mode_burst_period();
         } else if (caught_sig_rtmin)
         {
             caught_sig_rtmin = false;
@@ -361,6 +378,7 @@ int main(int argc, char *argv[])
             // Go online - with webcheck
             is_online = true;
             webcheck_enabled = true;
+            dialup_mode_burst_mode_until = calc_dialup_mode_burst_period();
         } else if (caught_sig_rtmax)
         {
             caught_sig_rtmax = false;
@@ -388,6 +406,9 @@ int main(int argc, char *argv[])
 
             // Reset log level to startup log level in case it got elevated by SIGRTMAX
             updater->get_logger()->set_loglevel(original_log_level);
+
+            // Reset dialup mode burst period on config reload
+            dialup_mode_burst_mode_until = calc_dialup_mode_burst_period();
         }
 
         // State handling
@@ -397,8 +418,16 @@ int main(int argc, char *argv[])
             if ( updater->get_config()->get_webcheck_enabled() != webcheck_enabled )    /*lint !e731 */
                 updater->get_config()->set_webcheck_enabled(webcheck_enabled);
 
-            // update all configured services
-            updater->update_services(is_online != old_online_state);
+            if (updater->get_config()->get_dialup_mode() == false ||
+                (time(NULL) > dialup_mode_sleep_until || time(NULL) < dialup_mode_burst_mode_until) )
+            {
+                // update all configured services
+                updater->update_services(is_online != old_online_state);
+
+                // Refresh sleep state (dialup mode only)
+                dialup_mode_sleep_until = time(NULL) + updater->get_config()->get_dialup_sleep_seconds();
+            } else
+                updater->get_logger()->print_sleep_dialup_mode(dialup_mode_sleep_until);
         }
         else
         {