2 * @brief The abstract service class. This class represents all services.
6 * @copyright Intra2net AG
10 #include "service.hpp"
11 #include <boost/foreach.hpp>
20 : Login("NOT SERIALIZED")
21 , Password("NOT SERIALIZED")
24 , MaxUpdatesWithinInterval(0)
32 * Default Destructor needed for deserialization.
40 * Setter for member Protocol.
41 * @param _protocol Value to set Protocol to.
43 void Service::set_protocol(const string& _protocol)
50 * Getter for memeber Protocol.
51 * @return Value of member Protocol.
53 string Service::get_protocol() const
60 * Setter for member Hostname.
61 * @param _hostname Value to set Hostname to.
63 void Service::set_hostname(const string& _hostname)
70 * Getter for member Hostname.
71 * @return Value of member Hostname.
73 string Service::get_hostname() const
80 * Setter for member Login.
81 * @param _login Value to set Login to.
83 void Service::set_login(const string& _login)
90 * Getter for member Login.
91 * @return Value of member Login.
93 string Service::get_login() const
100 * Setter for member Password.
101 * @param _password Value to set Password to.
103 void Service::set_password(const string& _password)
105 Password = _password;
110 * Getter for member Password.
111 * @return Value of member Password.
113 string Service::get_password() const
120 void Service::set_logger(const Logger::Ptr& _log)
127 * Getter for member Log.
128 * @return Shared pointer to Logger object.
130 Logger::Ptr Service::get_logger() const
136 void Service::set_last_updates(std::list<time_t> _last_updates)
139 BOOST_FOREACH( time_t update_time, _last_updates )
141 LastUpdates.push_back(update_time);
147 * Getter for member Lastupdated.
148 * @return Value of member Lastupdated.
150 const list<time_t> Service::get_last_updates() const
157 * Setter for member ActualIP.
158 * @param _actual_ip Value to set ActualIP to.
160 void Service::set_actual_ip(const std::string& _actual_ip)
162 ActualIP = _actual_ip;
167 * Getter for member ActualIP.
168 * @return Value of member ActualIP.
170 std::string Service::get_actual_ip() const
178 * Overloading of comparison operator.
179 * @param other Reference to other Service object.
180 * @return True if they equal, false if not.
182 bool Service::operator== (const Service& other) const
184 if ( ( this->Protocol == other.Protocol ) && ( this->Hostname == other.Hostname ) )
191 * Overloading of disparate operator.
192 * @param other Reference to other Service object.
193 * @return True if they differ, false if they are equal.
195 bool Service::operator!= (const Service& other) const
197 return !(*this == other);
202 * Checks if update will exceed max update interval.
203 * @param current_time Current time.
204 * @return True if update is allowed, false if update would exceed max update interval.
206 bool Service::update_allowed(const time_t current_time)
208 list<time_t>::iterator iter;
211 for (iter = LastUpdates.begin(); (iter != LastUpdates.end()) && ( i < MaxUpdatesWithinInterval ); iter++)
213 if ( (i == (MaxUpdatesWithinInterval-1)) && ( (*iter + ((time_t)UpdateInterval*60)) >= current_time ) )
215 Log->print_update_not_allowed(current_time,*iter,MaxUpdatesWithinInterval,get_service_name());
225 * Service update method, common to each service.
226 * @param ip The new ip to set for the hostname.
228 void Service::update(const string& ip, const time_t current_time)
230 Log->print_update_service(get_service_name());
232 // test if update is permitted by UpdateInterval Status
233 if ( update_allowed(current_time) )
235 if ( perform_update(ip) == 0 )
237 // if update was successful, we need to set the Lastupdated and ActualIP base member.
238 set_last_update(current_time);
240 Log->print_update_service_successful(get_service_name());
244 // problem while trying to update service
245 Log->print_update_service_failure(get_service_name());
252 * Sets the given time into the LastUpdates member and deletes expired entries.
253 * @param _timeout Value to set into LastUpdates.
255 void Service::set_last_update(const time_t current_time)
257 // Insert value into the list.
258 LastUpdates.push_front(current_time);
260 // Check for expired entries:
262 // MaxUpdatesWithinInterval given in service config, then use this to check for expired entries.
263 if ( MaxUpdatesWithinInterval > 0 )
265 // Delete the oldest entry if there are more than MaxUpdatesWithinInterval+1 entries in the list.
266 if (LastUpdates.size() > (MaxUpdatesWithinInterval+1))
267 LastUpdates.pop_back();
270 // UpdateInterval given in service config, then use this to check for expired entries.
271 else if ( UpdateInterval > 0 )
273 // Delete the oldest entry if it's older than current_time - UpdateInterval(minutes) + 1.
274 if ( (current_time - ((time_t)UpdateInterval*60) + 1) > LastUpdates.back() )
275 LastUpdates.pop_back();
278 // Neither MaxUpdatesWithinInterval nor UpdateInterval are given, so keep fix number of 10 entries.
281 if ( LastUpdates.size() > 10 )
282 LastUpdates.pop_back();
289 * Setter for member Timeout.
290 * @param _timeout Value to set Timeout to.
292 void Service::set_update_interval(const int _update_interval)
294 UpdateInterval = _update_interval;
299 * Getter for member Timeout.
300 * @return Value of Timeout.
302 int Service::get_update_interval() const
304 return UpdateInterval;
309 * Setter for member Max_updates_per_timeout.
310 * @param _max_updates_per_timeout Value to set Max_updates_per_timeout to.
312 void Service::set_max_updates_within_interval(const int _max_updates_within_interval)
314 MaxUpdatesWithinInterval = _max_updates_within_interval;
319 * Getter for member Max_updates_per_timeout.
320 * @return Value of Max_updates_per_timeout.
322 int Service::get_max_updates_within_interval() const
324 return MaxUpdatesWithinInterval;
329 * Get a unique service identify string
330 * @return A unique service identify string
332 string Service::get_service_name() const
336 service_name.append(Protocol);
337 service_name.append(" ");
338 service_name.append(Hostname);
345 * Get member DNSCacheTTL
346 * @return DNSCacheTTL
348 int Service::get_dns_cache_ttl() const
355 * Set member DNSCacheTTL
356 * @param _dns_cache_ttl DNSCacheTTL
358 void Service::set_dns_cache_ttl(const int _dns_cache_ttl)
360 DNSCacheTTL = _dns_cache_ttl;