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(0)
31 , ErrorServiceBlockedUntil(0)
38 * Default Destructor needed for deserialization.
46 * Setter for member Protocol.
47 * @param _protocol Value to set Protocol to.
49 void Service::set_protocol(const string& _protocol)
56 * Getter for memeber Protocol.
57 * @return Value of member Protocol.
59 string Service::get_protocol() const
66 * Setter for member Hostname.
67 * @param _hostname Value to set Hostname to.
69 void Service::set_hostname(const string& _hostname)
76 * Getter for member Hostname.
77 * @return Value of member Hostname.
79 string Service::get_hostname() const
86 * Setter for member Login.
87 * @param _login Value to set Login to.
89 void Service::set_login(const string& _login)
96 * Getter for member Login.
97 * @return Value of member Login.
99 string Service::get_login() const
106 * Setter for member Password.
107 * @param _password Value to set Password to.
109 void Service::set_password(const string& _password)
111 Password = _password;
116 * Getter for member Password.
117 * @return Value of member Password.
119 string Service::get_password() const
126 void Service::set_logger(const Logger::Ptr& _log)
133 * Getter for member Log.
134 * @return Shared pointer to Logger object.
136 Logger::Ptr Service::get_logger() const
142 void Service::set_last_updates(std::list<time_t> _last_updates)
145 BOOST_FOREACH( time_t update_time, _last_updates )
147 LastUpdates.push_back(update_time);
153 * Getter for member Lastupdated.
154 * @return Value of member Lastupdated.
156 const list<time_t> Service::get_last_updates() const
163 * Setter for member ActualIP.
164 * @param _actual_ip Value to set ActualIP to.
166 void Service::set_actual_ip(const std::string& _actual_ip)
168 ActualIP = _actual_ip;
173 * Getter for member ActualIP.
174 * @return Value of member ActualIP.
176 std::string Service::get_actual_ip() const
184 * Overloading of comparison operator.
185 * @param other Reference to other Service object.
186 * @return True if they equal, false if not.
188 bool Service::operator== (const Service& other) const
190 if ( ( this->Protocol == other.Protocol ) && ( this->Hostname == other.Hostname ) )
197 * Overloading of disparate operator.
198 * @param other Reference to other Service object.
199 * @return True if they differ, false if they are equal.
201 bool Service::operator!= (const Service& other) const
203 return !(*this == other);
208 * Checks if update will exceed max update interval.
209 * @param current_time Current time.
210 * @return True if update is allowed, false if update would exceed max update interval.
212 bool Service::update_allowed(const time_t current_time)
214 list<time_t>::iterator iter;
217 for (iter = LastUpdates.begin(); (iter != LastUpdates.end()) && ( i < MaxUpdatesWithinInterval ); iter++)
219 if ( (i == (MaxUpdatesWithinInterval-1)) && ( (*iter + ((time_t)UpdateInterval*60)) >= current_time ) )
221 Log->print_update_not_allowed(current_time,*iter,MaxUpdatesWithinInterval,get_service_name());
231 * Service update method, common to each service.
232 * @param ip The new ip to set for the hostname.
234 void Service::update(const string& ip, const time_t current_time)
236 const std::string service_name = get_service_name();
238 // Check if service is blocked for a short period of time (because of update errors)
239 if (ErrorServiceBlockedUntil && current_time < ErrorServiceBlockedUntil)
241 Log->print_update_service_is_blocked(service_name, ErrorServiceBlockedUntil - current_time);
245 Log->print_update_service(service_name);
247 // test if update is permitted by UpdateInterval Status
248 if ( update_allowed(current_time) )
250 if ( perform_update(ip) == 0 )
252 // if update was successful, we need to set the Lastupdated and ActualIP base member.
253 set_last_update(current_time);
255 Log->print_update_service_successful(service_name);
258 ErrorServiceBlockedUntil = 0;
262 // problem while trying to update service
263 Log->print_update_service_failure(service_name);
266 if (ErrorCount >= MaxErrorCount)
268 Log->print_block_service(service_name, ErrorBlockServiceSeconds);
270 ErrorServiceBlockedUntil = time(NULL) + ErrorBlockServiceSeconds;
279 * Sets the given time into the LastUpdates member and deletes expired entries.
280 * @param _timeout Value to set into LastUpdates.
282 void Service::set_last_update(const time_t current_time)
284 // Insert value into the list.
285 LastUpdates.push_front(current_time);
287 // Check for expired entries:
289 // MaxUpdatesWithinInterval given in service config, then use this to check for expired entries.
290 if ( MaxUpdatesWithinInterval > 0 )
292 // Delete the oldest entry if there are more than MaxUpdatesWithinInterval+1 entries in the list.
293 if (LastUpdates.size() > (MaxUpdatesWithinInterval+1))
294 LastUpdates.pop_back();
297 // UpdateInterval given in service config, then use this to check for expired entries.
298 else if ( UpdateInterval > 0 )
300 // Delete the oldest entry if it's older than current_time - UpdateInterval(minutes) + 1.
301 if ( (current_time - ((time_t)UpdateInterval*60) + 1) > LastUpdates.back() )
302 LastUpdates.pop_back();
305 // Neither MaxUpdatesWithinInterval nor UpdateInterval are given, so keep fix number of 10 entries.
308 if ( LastUpdates.size() > 10 )
309 LastUpdates.pop_back();
316 * Setter for member Timeout.
317 * @param _timeout Value to set Timeout to.
319 void Service::set_update_interval(const int _update_interval)
321 UpdateInterval = _update_interval;
326 * Getter for member Timeout.
327 * @return Value of Timeout.
329 int Service::get_update_interval() const
331 return UpdateInterval;
336 * Setter for member Max_updates_per_timeout.
337 * @param _max_updates_per_timeout Value to set Max_updates_per_timeout to.
339 void Service::set_max_updates_within_interval(const int _max_updates_within_interval)
341 MaxUpdatesWithinInterval = _max_updates_within_interval;
346 * Getter for member Max_updates_per_timeout.
347 * @return Value of Max_updates_per_timeout.
349 int Service::get_max_updates_within_interval() const
351 return MaxUpdatesWithinInterval;
356 * Get a unique service identify string
357 * @return A unique service identify string
359 string Service::get_service_name() const
363 service_name.append(Protocol);
364 service_name.append(" ");
365 service_name.append(Hostname);
372 * Get member DNSCacheTTL
373 * @return DNSCacheTTL
375 int Service::get_dns_cache_ttl() const
382 * Set member DNSCacheTTL
383 * @param _dns_cache_ttl DNSCacheTTL
385 void Service::set_dns_cache_ttl(const int _dns_cache_ttl)
387 DNSCacheTTL = _dns_cache_ttl;