Expire old entries in LastUpdates of each service.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Wed, 6 Oct 2010 14:22:49 +0000 (16:22 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Wed, 6 Oct 2010 14:23:22 +0000 (16:23 +0200)
src/service.cpp
src/service.hpp

index 0a6a29e..0294615 100644 (file)
@@ -235,7 +235,7 @@ void Service::update(const string& ip, const time_t current_time)
         if ( perform_update(ip) == 0 )
         {
             // if update was successful, we need to set the Lastupdated and ActualIP base member.
-            LastUpdates.push_front(current_time);
+            set_last_update(current_time);
             ActualIP = ip;
             Log->print_update_service_successful(get_service_name());
         }
@@ -249,6 +249,43 @@ void Service::update(const string& ip, const time_t current_time)
 
 
 /**
+* Sets the given time into the LastUpdates member and deletes expired entries.
+* @param _timeout Value to set into LastUpdates.
+*/
+void Service::set_last_update(const time_t current_time)
+{
+    // Insert value into the list.
+    LastUpdates.push_front(current_time);
+
+    // Check for expired entries:
+
+    // MaxUpdatesWithinInterval given in service config, then use this to check for expired entries.
+    if ( MaxUpdatesWithinInterval > 0 )
+    {
+        // Delete the oldest entry if there are more than MaxUpdatesWithinInterval+1 entries in the list.
+        if (LastUpdates.size() > (MaxUpdatesWithinInterval+1))
+            LastUpdates.pop_back();
+        return;
+    }
+    // UpdateInterval given in service config, then use this to check for expired entries.
+    else if ( UpdateInterval > 0 )
+    {
+        // Delete the oldest entry if it's older than current_time - UpdateInterval(minutes) + 1.
+        if ( (current_time - ((time_t)UpdateInterval*60) + 1) > LastUpdates.back() )
+            LastUpdates.pop_back();
+        return;
+    }
+    // Neither MaxUpdatesWithinInterval nor UpdateInterval are given, so keep fix number of 10 entries.
+    else
+    {
+        if ( LastUpdates.size() > 10 )
+            LastUpdates.pop_back();
+        return;
+    }
+}
+
+
+/**
  * Setter for member Timeout.
  * @param _timeout Value to set Timeout to.
  */
index dcec4b9..3b460e2 100644 (file)
@@ -70,6 +70,8 @@ public:
 
     bool update_allowed(const time_t current_time);
 
+    void set_last_update(const time_t current_time);
+
     int get_update_interval()const;
     void set_update_interval(const int _update_interval);