Commit | Line | Data |
---|---|---|
b1be615b | 1 | /** @file |
2bc1878a | 2 | * @brief The abstract service class. This class represents all services. |
b1be615b BS |
3 | * |
4 | * | |
5 | * | |
6 | * @copyright Intra2net AG | |
7 | * @license GPLv2 | |
8 | */ | |
9 | ||
4545a371 | 10 | #include "service.h" |
3c0cd271 | 11 | #include <boost/foreach.hpp> |
ca5d6889 BS |
12 | |
13 | using namespace std; | |
14 | ||
2bc1878a | 15 | |
85a0abf9 BS |
16 | /** |
17 | * Default Constructor | |
18 | */ | |
4545a371 | 19 | Service::Service() |
9a0aff44 | 20 | : Login("NOT SERIALIZED") |
27baf279 | 21 | , Password("NOT SERIALIZED") |
025abebb | 22 | , ActualIP("0.0.0.0") |
3c0cd271 BS |
23 | , UpdateInterval(0) |
24 | , MaxUpdatesWithinInterval(0) | |
d5a516ba | 25 | , DNSCacheTTL(0) |
2e956a36 | 26 | , Log(new Logger()) |
4545a371 BS |
27 | { |
28 | } | |
29 | ||
2bc1878a | 30 | |
85a0abf9 | 31 | /** |
27baf279 | 32 | * Default Destructor needed for deserialization. |
85a0abf9 | 33 | */ |
4545a371 BS |
34 | Service::~Service() |
35 | { | |
36 | } | |
37 | ||
38 | ||
2bc1878a | 39 | /** |
3a89ac31 BS |
40 | * Setter for member Protocol. |
41 | * @param _protocol Value to set Protocol to. | |
42 | */ | |
43 | void Service::set_protocol(const string& _protocol) | |
44 | { | |
45 | Protocol = _protocol; | |
46 | } | |
47 | ||
48 | ||
49 | /** | |
50 | * Getter for memeber Protocol. | |
51 | * @return Value of member Protocol. | |
52 | */ | |
b38684ce | 53 | string Service::get_protocol() const |
3a89ac31 BS |
54 | { |
55 | return Protocol; | |
56 | } | |
57 | ||
58 | ||
59 | /** | |
60 | * Setter for member Hostname. | |
61 | * @param _hostname Value to set Hostname to. | |
62 | */ | |
63 | void Service::set_hostname(const string& _hostname) | |
64 | { | |
65 | Hostname = _hostname; | |
66 | } | |
67 | ||
68 | ||
69 | /** | |
70 | * Getter for member Hostname. | |
71 | * @return Value of member Hostname. | |
72 | */ | |
b38684ce | 73 | string Service::get_hostname() const |
3a89ac31 BS |
74 | { |
75 | return Hostname; | |
76 | } | |
77 | ||
78 | ||
79 | /** | |
80 | * Setter for member Login. | |
81 | * @param _login Value to set Login to. | |
82 | */ | |
83 | void Service::set_login(const string& _login) | |
84 | { | |
85 | Login = _login; | |
86 | } | |
87 | ||
88 | ||
89 | /** | |
90 | * Getter for member Login. | |
91 | * @return Value of member Login. | |
92 | */ | |
b38684ce | 93 | string Service::get_login() const |
3a89ac31 BS |
94 | { |
95 | return Login; | |
96 | } | |
97 | ||
98 | ||
99 | /** | |
100 | * Setter for member Password. | |
101 | * @param _password Value to set Password to. | |
102 | */ | |
103 | void Service::set_password(const string& _password) | |
104 | { | |
105 | Password = _password; | |
106 | } | |
107 | ||
108 | ||
109 | /** | |
110 | * Getter for member Password. | |
111 | * @return Value of member Password. | |
112 | */ | |
b38684ce | 113 | string Service::get_password() const |
3a89ac31 BS |
114 | { |
115 | return Password; | |
116 | } | |
117 | ||
118 | ||
119 | /** | |
120 | * Setter for member Log. | |
121 | * @param _log Shared pointer to Logger object. | |
122 | */ | |
88a594e8 | 123 | void Service::set_logger(const Logger::Ptr& _log) |
3a89ac31 BS |
124 | { |
125 | Log = _log; | |
126 | } | |
127 | ||
128 | ||
129 | /** | |
130 | * Getter for member Log. | |
131 | * @return Shared pointer to Logger object. | |
132 | */ | |
b38684ce | 133 | Logger::Ptr Service::get_logger() const |
3a89ac31 BS |
134 | { |
135 | return Log; | |
2bc1878a BS |
136 | } |
137 | ||
138 | ||
c1b8cb79 | 139 | void Service::set_last_updates(std::list<int> _last_updates) |
2bc1878a | 140 | { |
3c0cd271 | 141 | LastUpdates.clear(); |
c1b8cb79 | 142 | BOOST_FOREACH( int update_time, _last_updates ) |
3c0cd271 BS |
143 | { |
144 | LastUpdates.push_back(update_time); | |
145 | } | |
2bc1878a BS |
146 | } |
147 | ||
148 | ||
149 | /** | |
150 | * Getter for member Lastupdated. | |
151 | * @return Value of member Lastupdated. | |
152 | */ | |
c1b8cb79 | 153 | list<int> Service::get_last_updates() |
2bc1878a | 154 | { |
c1b8cb79 | 155 | return LastUpdates; |
2bc1878a BS |
156 | } |
157 | ||
158 | ||
159 | /** | |
025abebb BS |
160 | * Setter for member ActualIP. |
161 | * @param _actual_ip Value to set ActualIP to. | |
27baf279 BS |
162 | */ |
163 | void Service::set_actual_ip(const std::string& _actual_ip) | |
164 | { | |
025abebb | 165 | ActualIP = _actual_ip; |
27baf279 BS |
166 | } |
167 | ||
168 | ||
169 | /** | |
025abebb BS |
170 | * Getter for member ActualIP. |
171 | * @return Value of member ActualIP. | |
27baf279 | 172 | */ |
b38684ce | 173 | std::string Service::get_actual_ip() const |
27baf279 | 174 | { |
025abebb | 175 | return ActualIP; |
27baf279 BS |
176 | } |
177 | ||
178 | ||
179 | ||
180 | /** | |
3a89ac31 BS |
181 | * Overloading of comparison operator. |
182 | * @param other Reference to other Service object. | |
183 | * @return True if they equal, false if not. | |
2bc1878a | 184 | */ |
3a89ac31 | 185 | bool Service::operator== (const Service& other) const |
2bc1878a | 186 | { |
3a89ac31 BS |
187 | if ( ( this->Protocol == other.Protocol ) && ( this->Hostname == other.Hostname ) ) |
188 | return true; | |
189 | return false; | |
2bc1878a BS |
190 | } |
191 | ||
192 | ||
193 | /** | |
3a89ac31 BS |
194 | * Overloading of disparate operator. |
195 | * @param other Reference to other Service object. | |
196 | * @return True if they differ, false if they are equal. | |
2bc1878a | 197 | */ |
3a89ac31 | 198 | bool Service::operator!= (const Service& other) const |
2bc1878a | 199 | { |
3a89ac31 | 200 | return !(*this == other); |
2bc1878a | 201 | } |
3c0cd271 BS |
202 | |
203 | ||
204 | /** | |
205 | * Checks if update will exceed max update interval. | |
206 | * @param current_time Current time. | |
207 | * @return True if update is allowed, false if update would exceed max update interval. | |
208 | */ | |
209 | bool Service::update_allowed(const int current_time) | |
210 | { | |
211 | list<int>::iterator iter; | |
212 | int i=0; | |
213 | ||
214 | for (iter = LastUpdates.begin(); (iter != LastUpdates.end()) && ( i < MaxUpdatesWithinInterval ); iter++) | |
215 | { | |
216 | if ( (i == (MaxUpdatesWithinInterval-1)) && ( (*iter + (UpdateInterval*60)) >= current_time ) ) | |
217 | { | |
c3dea5dc | 218 | Log->print_update_not_allowed(current_time,*iter,MaxUpdatesWithinInterval,get_service_name()); |
3c0cd271 BS |
219 | return false; |
220 | } | |
221 | i++; | |
222 | } | |
223 | return true; | |
224 | } | |
225 | ||
226 | ||
227 | /** | |
c3dea5dc BS |
228 | * Service update method, common to each service. |
229 | * @param ip The new ip to set for the hostname. | |
230 | */ | |
231 | void Service::update(const string& ip, const int current_time) | |
232 | { | |
233 | Log->print_update_service(get_service_name()); | |
234 | ||
235 | // test if update is permitted by UpdateInterval Status | |
236 | if ( update_allowed(current_time) ) | |
237 | { | |
238 | if ( perform_update(ip) == 0 ) | |
239 | { | |
240 | // if update was successful, we need to set the Lastupdated and ActualIP base member. | |
241 | LastUpdates.push_front(current_time); | |
242 | ActualIP = ip; | |
243 | Log->print_update_service_successful(get_service_name()); | |
244 | } | |
245 | else | |
246 | { | |
247 | // problem while trying to update service | |
248 | Log->print_update_service_failure(get_service_name()); | |
249 | } | |
250 | } | |
251 | } | |
252 | ||
253 | ||
254 | /** | |
3c0cd271 BS |
255 | * Setter for member Timeout. |
256 | * @param _timeout Value to set Timeout to. | |
257 | */ | |
258 | void Service::set_update_interval(const int _update_interval) | |
259 | { | |
260 | UpdateInterval = _update_interval; | |
261 | } | |
262 | ||
263 | ||
264 | /** | |
265 | * Getter for member Timeout. | |
266 | * @return Value of Timeout. | |
267 | */ | |
268 | int Service::get_update_interval() const | |
269 | { | |
270 | return UpdateInterval; | |
271 | } | |
272 | ||
273 | ||
274 | /** | |
275 | * Setter for member Max_updates_per_timeout. | |
276 | * @param _max_updates_per_timeout Value to set Max_updates_per_timeout to. | |
277 | */ | |
278 | void Service::set_max_updates_within_interval(const int _max_updates_within_interval) | |
279 | { | |
280 | MaxUpdatesWithinInterval = _max_updates_within_interval; | |
281 | } | |
282 | ||
283 | ||
284 | /** | |
285 | * Getter for member Max_updates_per_timeout. | |
286 | * @return Value of Max_updates_per_timeout. | |
287 | */ | |
288 | int Service::get_max_updates_within_interval() const | |
289 | { | |
290 | return MaxUpdatesWithinInterval; | |
291 | } | |
c3dea5dc BS |
292 | |
293 | ||
294 | /** | |
295 | * Get a unique service identify string | |
296 | * @return A unique service identify string | |
297 | */ | |
298 | string Service::get_service_name() const | |
299 | { | |
300 | string service_name; | |
301 | ||
302 | service_name.append(Protocol); | |
303 | service_name.append(" "); | |
304 | service_name.append(Hostname); | |
305 | ||
306 | return service_name; | |
307 | } | |
308 | ||
309 | ||
310 | /** | |
311 | * Get member DNSCacheTTL | |
312 | * @return DNSCacheTTL | |
313 | */ | |
314 | int Service::get_dns_cache_ttl() const | |
315 | { | |
316 | return DNSCacheTTL; | |
317 | } | |
318 | ||
319 | ||
320 | /** | |
321 | * Set member DNSCacheTTL | |
322 | * @param _dns_cache_ttl DNSCacheTTL | |
323 | */ | |
324 | void Service::set_dns_cache_ttl(const int _dns_cache_ttl) | |
325 | { | |
326 | DNSCacheTTL = _dns_cache_ttl; | |
327 | } |