Implemented logic to avoid recently redefined Services from expiring their update...
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Fri, 7 Aug 2009 15:53:23 +0000 (17:53 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Fri, 7 Aug 2009 15:53:23 +0000 (17:53 +0200)
This is done by holding (serialize/deserialize) old (not active) Services in a internal list,
until their update timeout is expired or the same Service is redefined (values copied from old service).

src/config.cpp
src/config.h
src/dhs.cpp
src/dhs.h
src/ods.cpp
src/ods.h
src/service.h

index cb6769f..9b6f61b 100644 (file)
@@ -87,7 +87,7 @@ Config::~Config()
 
 
 /**
- * This function serializes all Service objects in Services into a specified file.
+ * This function serializes all Service objects in Services and Old_services (where the update Timeout isn't expired) into a specified file.
  * @return 0 if all is fine, -1 otherwise.
  */
 int Config::serialize_services()
@@ -103,6 +103,14 @@ int Config::serialize_services()
         service_holder->add_service(service);
     }
 
+    int current_time = time(NULL);
+
+    BOOST_FOREACH(ServicePtr service, Old_services)
+    {
+        if ( ( service->get_lastupdated() + service->get_timeout() ) >= current_time )  // Update Timeout of service isn't expired.
+            service_holder->add_service(service);
+    }
+
     // Then we can serialize this separate Object into one file.
 
     ofstream ofs(OBJECT_FILE);
@@ -124,7 +132,7 @@ int Config::serialize_services()
 int Config::deserialize_services()
 {
     // TODO: error handling
-
+    // TODO: redefine algorithm, to avoid tripple FOREACHes ;-)
 
     ifstream ifs(OBJECT_FILE);
     if ( ifs.is_open() )
@@ -135,25 +143,31 @@ int Config::deserialize_services()
         ServiceholderPtr service_holder(_service_holder);
         ifs.close();
 
-        list<ServicePtr> old_services = service_holder->get_hold_services();
+        list<ServicePtr> _old_services = service_holder->get_hold_services();
 
-        BOOST_FOREACH(ServicePtr old_service, old_services)
+        BOOST_FOREACH(ServicePtr old_service, _old_services)
         {
+            // Put the deserialized service into the Old_service member list.
+            Old_services.push_back(old_service);
             Log->print_service_object("Deserialized following Service object:", old_service->get_protocol(), old_service->get_hostname(), old_service->get_login() ,old_service->get_password() ,old_service->get_actual_ip(), old_service->get_lastupdated());
         }
 
         BOOST_FOREACH(ServicePtr service, Services)
         {
-            BOOST_FOREACH(ServicePtr old_service, old_services)
+            BOOST_FOREACH(ServicePtr old_service, Old_services)
             {
                 if ( *service == *old_service )
                 {
                     service->set_lastupdated(old_service->get_lastupdated());
                     service->set_actual_ip(old_service->get_actual_ip());
                     Log->print_service_object("New Service object with adopted values:", service->get_protocol(), service->get_hostname(), service->get_login() ,service->get_password() ,service->get_actual_ip(), service->get_lastupdated());
+                    // We have adopted the values of the old_service. Just set lastupdated and timeout to 0, so this old_service wont be serialized.
+                    old_service->set_lastupdated(0);
+                    old_service->set_timeout(0);
                 }
             }
         }
+        //TODO: debug output of Old_services (is element really removed from list if values were adopted???)
         Log->print_deserialized_objects_success();
     }
     else
@@ -254,12 +268,12 @@ ServicePtr Config::create_service(const string &protocol,const string &hostname,
 {
     if(protocol == "dhs")
     {
-        ServicePtr service_dhs(new DHS(protocol,hostname,login,password,Log,0,0,1));
+        ServicePtr service_dhs(new DHS(protocol,hostname,login,password,Log,0,60,1));
         return service_dhs;
     }
     else if(protocol == "ods")
     {
-        ServicePtr service_ods(new ODS(protocol,hostname,login,password,Log,0,0,1));
+        ServicePtr service_ods(new ODS(protocol,hostname,login,password,Log,0,60,1));
         return service_ods;
     }
     else
index 9bd6645..136dbea 100644 (file)
@@ -24,6 +24,8 @@
 #include <iostream>
 #include <fstream>
 
+#include <time.h>
+
 #include "service.h"
 #include "logger.h"
 #include "serviceholder.h"
@@ -46,7 +48,9 @@ private:
     Options_descriptionPtr Opt_desc_conf_service;
     boost::program_options::variables_map Variables_map;
 
-    std::list<ServicePtr> Services;
+    std::list<ServicePtr> Services;         // Represents all active Services.
+    std::list<ServicePtr> Old_services;     // Represents all old Services where the timeout isn't expired (in case the same Service is redefined).
+
     LoggerPtr Log;
 
     bool Daemon_mode;
index ac3d195..1b3e0b3 100644 (file)
@@ -18,6 +18,7 @@ using namespace std;
 DHS::DHS()
     : Timeout(0)
     , Max_updates_per_timeout(0)
+    , Updates_within_timeout(0)
 {
 }
 
@@ -29,6 +30,7 @@ DHS::DHS()
  * @param _password The corresponding password.
  */
 DHS::DHS(const string& _protocol, const string& _hostname, const string& _login, const string& _password, const LoggerPtr& _logger, const int _lastupdated, const int _timeout, const int _max_updates_per_timeout)
+    : Updates_within_timeout(1)
 {
     Timeout = _timeout;
     Max_updates_per_timeout = _max_updates_per_timeout;
@@ -75,6 +77,7 @@ void DHS::serialize(Archive & ar, const unsigned int version)
     ar & boost::serialization::base_object<Service>(*this);
     ar & Timeout;
     ar & Max_updates_per_timeout;
+    ar & Updates_within_timeout;
 }
 
 
index 911dc29..edda3f3 100644 (file)
--- a/src/dhs.h
+++ b/src/dhs.h
@@ -25,6 +25,7 @@ class DHS : public Service
 private:
     int Timeout;
     int Max_updates_per_timeout;
+    int Updates_within_timeout;
 
     friend class boost::serialization::access;
     template<class Archive>
index 70a6399..fc0b908 100644 (file)
@@ -18,6 +18,7 @@ using namespace std;
 ODS::ODS()
     : Timeout(0)
     , Max_updates_per_timeout(0)
+    , Updates_within_timeout(0)
 {
 }
 
@@ -29,6 +30,7 @@ ODS::ODS()
  * @param _password The corresponding password.
  */
 ODS::ODS(const string& _protocol, const string& _hostname, const string& _login, const string& _password, const LoggerPtr& _logger, const int _lastupdated, const int _timeout, const int _max_updates_per_timeout)
+    : Updates_within_timeout(1)
 {
     Timeout = _timeout;
     Max_updates_per_timeout = _max_updates_per_timeout;
@@ -75,6 +77,7 @@ void ODS::serialize(Archive & ar, const unsigned int version)
     ar & boost::serialization::base_object<Service>(*this);
     ar & Timeout;
     ar & Max_updates_per_timeout;
+    ar & Updates_within_timeout;
 }
 
 
index ed91c31..60272db 100644 (file)
--- a/src/ods.h
+++ b/src/ods.h
@@ -25,6 +25,7 @@ class ODS : public Service
 private:
     int Timeout;
     int Max_updates_per_timeout;
+    int Updates_within_timeout;
 
     friend class boost::serialization::access;
     template<class Archive>
index b020173..ebdd22f 100644 (file)
@@ -44,6 +44,9 @@ public:
 
     virtual void update(const std::string&)=0;
 
+    virtual int get_timeout()=0;
+    virtual void set_timeout(const int)=0;
+
     void set_protocol(const std::string&);
     std::string get_protocol();