/**
- * 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()
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);
int Config::deserialize_services()
{
// TODO: error handling
-
+ // TODO: redefine algorithm, to avoid tripple FOREACHes ;-)
ifstream ifs(OBJECT_FILE);
if ( ifs.is_open() )
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
{
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
DHS::DHS()
: Timeout(0)
, Max_updates_per_timeout(0)
+ , Updates_within_timeout(0)
{
}
* @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;
ar & boost::serialization::base_object<Service>(*this);
ar & Timeout;
ar & Max_updates_per_timeout;
+ ar & Updates_within_timeout;
}
ODS::ODS()
: Timeout(0)
, Max_updates_per_timeout(0)
+ , Updates_within_timeout(0)
{
}
* @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;
ar & boost::serialization::base_object<Service>(*this);
ar & Timeout;
ar & Max_updates_per_timeout;
+ ar & Updates_within_timeout;
}