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 BS |
11 | #include <boost/foreach.hpp> |
12 | #include <boost/serialization/list.hpp> | |
2bc1878a | 13 | |
85a0abf9 BS |
14 | /** |
15 | * Default Constructor | |
16 | */ | |
4545a371 | 17 | Service::Service() |
27baf279 BS |
18 | : Protocol("") |
19 | , Hostname("") | |
20 | , Login("NOT SERIALIZED") | |
21 | , Password("NOT SERIALIZED") | |
025abebb | 22 | , ActualIP("0.0.0.0") |
3c0cd271 BS |
23 | , UpdateInterval(0) |
24 | , MaxUpdatesWithinInterval(0) | |
2e956a36 | 25 | , Log(new Logger()) |
4545a371 BS |
26 | { |
27 | } | |
28 | ||
2bc1878a | 29 | |
85a0abf9 | 30 | /** |
27baf279 | 31 | * Default Destructor needed for deserialization. |
85a0abf9 | 32 | */ |
4545a371 BS |
33 | Service::~Service() |
34 | { | |
35 | } | |
36 | ||
37 | ||
2bc1878a BS |
38 | /** |
39 | * Although this is an abstract class, we need the serialize function that we can serialize derived classes through a Service *. | |
40 | * @param ar Archive. | |
41 | * @param version Version. | |
42 | */ | |
43 | template<class Archive> | |
44 | void Service::serialize(Archive & ar, const unsigned int version) | |
45 | { | |
3a89ac31 BS |
46 | // protocol and hostname are the unique identifier for each service. |
47 | ar & Protocol; | |
48 | ar & Hostname; | |
27baf279 | 49 | // lastupdated and actual_ip must also be serialized, cause these are essential infos of each service. |
3c0cd271 | 50 | ar & LastUpdates; |
025abebb | 51 | ar & ActualIP; |
3c0cd271 BS |
52 | ar & UpdateInterval; |
53 | ar & MaxUpdatesWithinInterval; | |
3a89ac31 BS |
54 | } |
55 | ||
56 | ||
57 | /** | |
58 | * Setter for member Protocol. | |
59 | * @param _protocol Value to set Protocol to. | |
60 | */ | |
61 | void Service::set_protocol(const string& _protocol) | |
62 | { | |
63 | Protocol = _protocol; | |
64 | } | |
65 | ||
66 | ||
67 | /** | |
68 | * Getter for memeber Protocol. | |
69 | * @return Value of member Protocol. | |
70 | */ | |
b38684ce | 71 | string Service::get_protocol() const |
3a89ac31 BS |
72 | { |
73 | return Protocol; | |
74 | } | |
75 | ||
76 | ||
77 | /** | |
78 | * Setter for member Hostname. | |
79 | * @param _hostname Value to set Hostname to. | |
80 | */ | |
81 | void Service::set_hostname(const string& _hostname) | |
82 | { | |
83 | Hostname = _hostname; | |
84 | } | |
85 | ||
86 | ||
87 | /** | |
88 | * Getter for member Hostname. | |
89 | * @return Value of member Hostname. | |
90 | */ | |
b38684ce | 91 | string Service::get_hostname() const |
3a89ac31 BS |
92 | { |
93 | return Hostname; | |
94 | } | |
95 | ||
96 | ||
97 | /** | |
98 | * Setter for member Login. | |
99 | * @param _login Value to set Login to. | |
100 | */ | |
101 | void Service::set_login(const string& _login) | |
102 | { | |
103 | Login = _login; | |
104 | } | |
105 | ||
106 | ||
107 | /** | |
108 | * Getter for member Login. | |
109 | * @return Value of member Login. | |
110 | */ | |
b38684ce | 111 | string Service::get_login() const |
3a89ac31 BS |
112 | { |
113 | return Login; | |
114 | } | |
115 | ||
116 | ||
117 | /** | |
118 | * Setter for member Password. | |
119 | * @param _password Value to set Password to. | |
120 | */ | |
121 | void Service::set_password(const string& _password) | |
122 | { | |
123 | Password = _password; | |
124 | } | |
125 | ||
126 | ||
127 | /** | |
128 | * Getter for member Password. | |
129 | * @return Value of member Password. | |
130 | */ | |
b38684ce | 131 | string Service::get_password() const |
3a89ac31 BS |
132 | { |
133 | return Password; | |
134 | } | |
135 | ||
136 | ||
137 | /** | |
138 | * Setter for member Log. | |
139 | * @param _log Shared pointer to Logger object. | |
140 | */ | |
88a594e8 | 141 | void Service::set_logger(const Logger::Ptr& _log) |
3a89ac31 BS |
142 | { |
143 | Log = _log; | |
144 | } | |
145 | ||
146 | ||
147 | /** | |
148 | * Getter for member Log. | |
149 | * @return Shared pointer to Logger object. | |
150 | */ | |
b38684ce | 151 | Logger::Ptr Service::get_logger() const |
3a89ac31 BS |
152 | { |
153 | return Log; | |
2bc1878a BS |
154 | } |
155 | ||
156 | ||
3c0cd271 | 157 | void Service::set_last_updates(std::list<int>* _last_updates) |
2bc1878a | 158 | { |
3c0cd271 BS |
159 | LastUpdates.clear(); |
160 | BOOST_FOREACH( int update_time, *_last_updates ) | |
161 | { | |
162 | LastUpdates.push_back(update_time); | |
163 | } | |
2bc1878a BS |
164 | } |
165 | ||
166 | ||
167 | /** | |
168 | * Getter for member Lastupdated. | |
169 | * @return Value of member Lastupdated. | |
170 | */ | |
3c0cd271 | 171 | list<int>* Service::get_last_updates() |
2bc1878a | 172 | { |
3c0cd271 | 173 | return &LastUpdates; |
2bc1878a BS |
174 | } |
175 | ||
176 | ||
177 | /** | |
025abebb BS |
178 | * Setter for member ActualIP. |
179 | * @param _actual_ip Value to set ActualIP to. | |
27baf279 BS |
180 | */ |
181 | void Service::set_actual_ip(const std::string& _actual_ip) | |
182 | { | |
025abebb | 183 | ActualIP = _actual_ip; |
27baf279 BS |
184 | } |
185 | ||
186 | ||
187 | /** | |
025abebb BS |
188 | * Getter for member ActualIP. |
189 | * @return Value of member ActualIP. | |
27baf279 | 190 | */ |
b38684ce | 191 | std::string Service::get_actual_ip() const |
27baf279 | 192 | { |
025abebb | 193 | return ActualIP; |
27baf279 BS |
194 | } |
195 | ||
196 | ||
197 | ||
198 | /** | |
3a89ac31 BS |
199 | * Overloading of comparison operator. |
200 | * @param other Reference to other Service object. | |
201 | * @return True if they equal, false if not. | |
2bc1878a | 202 | */ |
3a89ac31 | 203 | bool Service::operator== (const Service& other) const |
2bc1878a | 204 | { |
3a89ac31 BS |
205 | if ( ( this->Protocol == other.Protocol ) && ( this->Hostname == other.Hostname ) ) |
206 | return true; | |
207 | return false; | |
2bc1878a BS |
208 | } |
209 | ||
210 | ||
211 | /** | |
3a89ac31 BS |
212 | * Overloading of disparate operator. |
213 | * @param other Reference to other Service object. | |
214 | * @return True if they differ, false if they are equal. | |
2bc1878a | 215 | */ |
3a89ac31 | 216 | bool Service::operator!= (const Service& other) const |
2bc1878a | 217 | { |
3a89ac31 | 218 | return !(*this == other); |
2bc1878a | 219 | } |
3c0cd271 BS |
220 | |
221 | ||
222 | /** | |
223 | * Checks if update will exceed max update interval. | |
224 | * @param current_time Current time. | |
225 | * @return True if update is allowed, false if update would exceed max update interval. | |
226 | */ | |
227 | bool Service::update_allowed(const int current_time) | |
228 | { | |
229 | list<int>::iterator iter; | |
230 | int i=0; | |
231 | ||
232 | for (iter = LastUpdates.begin(); (iter != LastUpdates.end()) && ( i < MaxUpdatesWithinInterval ); iter++) | |
233 | { | |
234 | if ( (i == (MaxUpdatesWithinInterval-1)) && ( (*iter + (UpdateInterval*60)) >= current_time ) ) | |
235 | { | |
236 | Log->print_update_not_allowed(current_time,*iter,MaxUpdatesWithinInterval,"ODS"); | |
237 | return false; | |
238 | } | |
239 | i++; | |
240 | } | |
241 | return true; | |
242 | } | |
243 | ||
244 | ||
245 | /** | |
246 | * Setter for member Timeout. | |
247 | * @param _timeout Value to set Timeout to. | |
248 | */ | |
249 | void Service::set_update_interval(const int _update_interval) | |
250 | { | |
251 | UpdateInterval = _update_interval; | |
252 | } | |
253 | ||
254 | ||
255 | /** | |
256 | * Getter for member Timeout. | |
257 | * @return Value of Timeout. | |
258 | */ | |
259 | int Service::get_update_interval() const | |
260 | { | |
261 | return UpdateInterval; | |
262 | } | |
263 | ||
264 | ||
265 | /** | |
266 | * Setter for member Max_updates_per_timeout. | |
267 | * @param _max_updates_per_timeout Value to set Max_updates_per_timeout to. | |
268 | */ | |
269 | void Service::set_max_updates_within_interval(const int _max_updates_within_interval) | |
270 | { | |
271 | MaxUpdatesWithinInterval = _max_updates_within_interval; | |
272 | } | |
273 | ||
274 | ||
275 | /** | |
276 | * Getter for member Max_updates_per_timeout. | |
277 | * @return Value of Max_updates_per_timeout. | |
278 | */ | |
279 | int Service::get_max_updates_within_interval() const | |
280 | { | |
281 | return MaxUpdatesWithinInterval; | |
282 | } |