2 * @brief The abstract service class. This class represents all services.
6 * @copyright Intra2net AG
10 #include "service.hpp"
11 #include <boost/foreach.hpp>
15 /// Number of update errors until a service will be blocked
16 const int MaxErrorCount = 3;
17 /// Number of seconds a service will be blocked if MaxErrorCount is reached
18 const int ErrorBlockServiceSeconds = 15 * 60;
24 : Login("NOT SERIALIZED")
25 , Password("NOT SERIALIZED")
28 , MaxUpdatesWithinInterval(3)
29 , MaxEqualUpdatesInSuccession(2)
32 , ErrorServiceBlockedUntil(0)
39 * Default Destructor needed for deserialization.
47 * Setter for member Protocol.
48 * @param _protocol Value to set Protocol to.
50 void Service::set_protocol(const string& _protocol)
57 * Getter for memeber Protocol.
58 * @return Value of member Protocol.
60 string Service::get_protocol() const
67 * Setter for member Hostname.
68 * @param _hostname Value to set Hostname to.
70 void Service::set_hostname(const string& _hostname)
77 * Getter for member Hostname.
78 * @return Value of member Hostname.
80 string Service::get_hostname() const
87 * Setter for member Login.
88 * @param _login Value to set Login to.
90 void Service::set_login(const string& _login)
97 * Getter for member Login.
98 * @return Value of member Login.
100 string Service::get_login() const
107 * Setter for member Password.
108 * @param _password Value to set Password to.
110 void Service::set_password(const string& _password)
112 Password = _password;
117 * Getter for member Password.
118 * @return Value of member Password.
120 string Service::get_password() const
127 void Service::set_logger(const Logger::Ptr& _log)
134 * Getter for member Log.
135 * @return Shared pointer to Logger object.
137 Logger::Ptr Service::get_logger() const
144 * Setter for member LastUpdates.
145 * @param _last_updates Value to set LastUpdates to.
147 void Service::set_last_updates(std::map<time_t,std::string> _last_updates)
149 std::map<time_t,std::string> temp = _last_updates;
150 LastUpdates.swap(temp);
155 * Getter for member LastUpdates.
156 * @return Value of member LastUpdates.
158 const std::map<time_t,std::string> Service::get_last_updates() const
165 * Setter for member ActualIP.
166 * @param _actual_ip Value to set ActualIP to.
168 void Service::set_actual_ip(const std::string& _actual_ip)
170 ActualIP = _actual_ip;
175 * Getter for member ActualIP.
176 * @return Value of member ActualIP.
178 std::string Service::get_actual_ip() const
185 * Overloading of comparison operator.
186 * @param other Reference to other Service object.
187 * @return True if they equal, false if not.
189 bool Service::operator== (const Service& other) const
191 if ( ( this->Protocol == other.Protocol ) && ( this->Hostname == other.Hostname ) )
198 * Overloading of disparate operator.
199 * @param other Reference to other Service object.
200 * @return True if they differ, false if they are equal.
202 bool Service::operator!= (const Service& other) const
204 return !(*this == other);
209 * Checks if update will exceed max update interval.
210 * @param current_time Current time.
211 * @param changed_to_online True if we just changed to online, false if we were already online
212 * @return True if update is allowed, false if update would exceed max update interval.
214 bool Service::update_allowed(const time_t current_time, bool changed_to_online)
217 for ( std::map<time_t,std::string>::reverse_iterator r_iter = LastUpdates.rbegin(); (r_iter != LastUpdates.rend()) && ( i < MaxUpdatesWithinInterval ); r_iter++)
219 if ( (i == (MaxUpdatesWithinInterval-1)) && ( (r_iter->first + ((time_t)UpdateInterval*60)) >= current_time ) )
221 Log->print_update_not_allowed(changed_to_online,current_time,r_iter->first,MaxUpdatesWithinInterval,get_service_name());
231 * Service update method, common to each service.
232 * @param ip The new ip to set for the hostname.
233 * @param current_time Current time
234 * @param changed_to_online True if we just changed to online, false if we were already online
236 void Service::update(const string& ip, const time_t current_time, bool changed_to_online)
238 const std::string service_name = get_service_name();
240 // Check if service is blocked for a short period of time (because of update errors)
241 if (ErrorServiceBlockedUntil && current_time < ErrorServiceBlockedUntil)
243 Log->print_update_service_is_blocked(service_name, ErrorServiceBlockedUntil - current_time);
247 // test if update is permitted by UpdateInterval Status
248 if ( update_allowed(current_time, changed_to_online) )
250 Log->print_update_service(service_name);
252 if ( perform_update(ip) == 0 )
254 // if update was successful, we need to set the Lastupdated and ActualIP base member.
255 set_last_update(current_time,ip);
257 Log->print_update_service_successful(service_name);
260 ErrorServiceBlockedUntil = 0;
264 // problem while trying to update service
265 Log->print_update_service_failure(service_name);
268 if (ErrorCount >= MaxErrorCount)
270 Log->print_block_service(service_name, ErrorBlockServiceSeconds);
272 ErrorServiceBlockedUntil = time(NULL) + ErrorBlockServiceSeconds;
281 * Sets the given time into the LastUpdates member and deletes expired entries.
282 * @param _timeout Value to set into LastUpdates.
284 void Service::set_last_update(const time_t current_time, const string& ip)
286 // Insert value into the list.
287 LastUpdates.insert(make_pair(current_time,ip));
289 // Get the maximum of MaxUpdatesWithinInterval and MaxEqualUpdatesInSuccession
290 int maximum = max(MaxUpdatesWithinInterval,MaxEqualUpdatesInSuccession);
292 // Check for expired entries:
294 // MaxUpdatesWithinInterval given in service config, then use this to check for expired entries.
297 // Delete the oldest entry if there are more than max(MaxUpdatesWithinInterval,MaxEqualUpdatesInSuccession)+1 entries in the list.
298 if ( LastUpdates.size() > (size_t)(maximum+1) )
299 LastUpdates.erase(LastUpdates.begin());
302 // UpdateInterval given in service config, then use this to check for expired entries.
303 else if ( UpdateInterval > 0 )
305 // Delete the oldest entry if it's older than current_time - UpdateInterval(minutes) + 1.
306 if ( (current_time - ((time_t)UpdateInterval*60) + 1) > LastUpdates.begin()->first )
307 LastUpdates.erase(LastUpdates.begin());
310 // Neither MaxUpdatesWithinInterval nor UpdateInterval are given, so keep fix number of 10 entries.
313 if ( LastUpdates.size() > 10 )
314 LastUpdates.erase(LastUpdates.begin());
321 * Getter the last updated time.
322 * @return Value of the last update as time_t.
324 time_t Service::get_last_update_time( )
326 time_t last_update = 0;
327 if ( !LastUpdates.empty() )
329 std::map<time_t,std::string>::reverse_iterator r_iter = LastUpdates.rbegin();
330 if ( r_iter != LastUpdates.rend() )
331 last_update = r_iter->first;
338 * Setter for member Timeout.
339 * @param _timeout Value to set Timeout to.
341 void Service::set_update_interval(const int _update_interval)
343 UpdateInterval = _update_interval;
348 * Getter for member Timeout.
349 * @return Value of Timeout.
351 int Service::get_update_interval() const
353 return UpdateInterval;
358 * Setter for member Max_updates_per_timeout.
359 * @param _max_updates_per_timeout Value to set Max_updates_per_timeout to.
361 void Service::set_max_updates_within_interval(const int _max_updates_within_interval)
363 MaxUpdatesWithinInterval = _max_updates_within_interval;
368 * Getter for member Max_updates_per_timeout.
369 * @return Value of Max_updates_per_timeout.
371 int Service::get_max_updates_within_interval() const
373 return MaxUpdatesWithinInterval;
378 * Setter for member MaxEqualUpdatesInSuccession.
379 * @param _max_equal_updates_in_succession Value to set MaxEqualUpdatesInSuccession to.
381 void Service::set_max_equal_updates_in_succession(const int _max_equal_updates_in_succession)
383 MaxEqualUpdatesInSuccession = _max_equal_updates_in_succession;
388 * Getter for member MaxEqualUpdatesInSuccession.
389 * @return Value of MaxEqualUpdatesInSuccession.
391 int Service::get_max_equal_updates_in_succession() const
393 return MaxEqualUpdatesInSuccession;
398 * Get a unique service identify string
399 * @return A unique service identify string
401 string Service::get_service_name() const
405 service_name.append(Protocol);
406 service_name.append(" ");
407 service_name.append(Hostname);
414 * Get member DNSCacheTTL
415 * @return DNSCacheTTL
417 int Service::get_dns_cache_ttl() const
424 * Set member DNSCacheTTL
425 * @param _dns_cache_ttl DNSCacheTTL
427 void Service::set_dns_cache_ttl(const int _dns_cache_ttl)
429 DNSCacheTTL = _dns_cache_ttl;