1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Damian 'Mozork' Frick |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file NotificationQueue.cc |
---|
31 | @brief Implementation of the NotificationQueue class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "NotificationQueue.h" |
---|
35 | |
---|
36 | #include <map> |
---|
37 | #include <sstream> |
---|
38 | |
---|
39 | #include "core/CoreIncludes.h" |
---|
40 | #include "core/XMLPort.h" |
---|
41 | #include "util/SubString.h" |
---|
42 | |
---|
43 | namespace orxonox |
---|
44 | { |
---|
45 | |
---|
46 | CreateFactory(NotificationQueue); |
---|
47 | |
---|
48 | /** |
---|
49 | @brief |
---|
50 | Default constructor. Registers and initializes the object. |
---|
51 | @param creator |
---|
52 | The creator of the NotificationQueue. |
---|
53 | */ |
---|
54 | NotificationQueue::NotificationQueue(BaseObject* creator) : BaseObject(creator), Synchronisable(creator), registered_(false) |
---|
55 | { |
---|
56 | RegisterObject(NotificationQueue); |
---|
57 | |
---|
58 | this->initialize(); |
---|
59 | this->registerVariables(); |
---|
60 | } |
---|
61 | |
---|
62 | // TODO move to docu. |
---|
63 | /** |
---|
64 | @brief |
---|
65 | Constructor. Registers and initializes the object. |
---|
66 | @param creator |
---|
67 | The creator of the NotificationQueue |
---|
68 | @param name |
---|
69 | The name of the new NotificationQueue. It needs to be unique |
---|
70 | @param senders |
---|
71 | The senders that are targets of this NotificationQueue, i.e. the names of senders whose Notifications this NotificationQueue displays. |
---|
72 | The senders need to be seperated by commas. |
---|
73 | @param size |
---|
74 | The size (the maximum number of displayed Notifications) of this NotificationQueue. |
---|
75 | @param displayTime |
---|
76 | The time during which a Notification is (at most) displayed. |
---|
77 | */ |
---|
78 | |
---|
79 | /** |
---|
80 | @brief |
---|
81 | Destructor. |
---|
82 | */ |
---|
83 | NotificationQueue::~NotificationQueue() |
---|
84 | { |
---|
85 | this->targets_.clear(); |
---|
86 | |
---|
87 | if(this->isRegistered()) // If the NotificationQueue is registered. |
---|
88 | { |
---|
89 | this->clear(true); |
---|
90 | |
---|
91 | // Unregister with the NotificationManager. |
---|
92 | NotificationManager::getInstance().unregisterQueue(this); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | @brief |
---|
98 | Initializes the NotificationQueue. |
---|
99 | */ |
---|
100 | void NotificationQueue::initialize(void) |
---|
101 | { |
---|
102 | this->size_ = 0; |
---|
103 | this->tickTime_ = 0.0f; |
---|
104 | this->maxSize_ = NotificationQueue::DEFAULT_SIZE; |
---|
105 | this->displayTime_ = NotificationQueue::DEFAULT_DISPLAY_TIME; |
---|
106 | |
---|
107 | this->creationTime_ = std::time(0); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | @brief |
---|
112 | Creates the NotificationQueue. |
---|
113 | */ |
---|
114 | void NotificationQueue::create(void) |
---|
115 | { |
---|
116 | // Register the NotificationQueue with the NotificationManager. |
---|
117 | bool queueRegistered = NotificationManager::getInstance().registerQueue(this); |
---|
118 | this->registered_ = true; |
---|
119 | if(!queueRegistered) // If the registration has failed. |
---|
120 | { |
---|
121 | this->registered_ = false; |
---|
122 | COUT(1) << "Error: NotificationQueue '" << this->getName() << "' could not be registered." << std::endl; |
---|
123 | return; |
---|
124 | } |
---|
125 | |
---|
126 | COUT(3) << "NotificationQueue '" << this->getName() << "' created." << std::endl; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | @brief |
---|
131 | Updates the queue from time to time. |
---|
132 | @param dt |
---|
133 | The time interval that has passed since the last tick. |
---|
134 | */ |
---|
135 | void NotificationQueue::tick(float dt) |
---|
136 | { |
---|
137 | this->tickTime_ += dt; // Add the time interval that has passed to the time counter. |
---|
138 | if(this->displayTime_ != INF && this->tickTime_ >= 1.0) // If the time counter is greater than 1 s all Notifications that have expired are removed, if it is smaller we wait to the next tick. |
---|
139 | { |
---|
140 | this->timeLimit_.time = std::time(0)-this->displayTime_; // Container containing the current time. |
---|
141 | |
---|
142 | std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator it = this->ordering_.begin(); |
---|
143 | // Iterate through all elements whose creation time is smaller than the current time minus the display time. |
---|
144 | while(it != this->ordering_.upper_bound(&this->timeLimit_)) |
---|
145 | { |
---|
146 | this->remove(it); // Remove the Notifications that have expired. |
---|
147 | it = this->ordering_.begin(); |
---|
148 | } |
---|
149 | |
---|
150 | this->tickTime_ = this->tickTime_ - (int)this->tickTime_; // Reset time counter. |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | void NotificationQueue::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
155 | { |
---|
156 | SUPER(NotificationQueue, XMLPort, xmlelement, mode); |
---|
157 | |
---|
158 | XMLPortParam(NotificationQueue, "targets", setTargets, getTargets, xmlelement, mode).defaultValues(NotificationListener::ALL); |
---|
159 | XMLPortParam(NotificationQueue, "size", setMaxSize, getMaxSize, xmlelement, mode); |
---|
160 | XMLPortParam(NotificationQueue, "displayTime", setDisplayTime, getDisplayTime, xmlelement, mode); |
---|
161 | |
---|
162 | this->create(); |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | /** |
---|
167 | @brief |
---|
168 | Registers Variables to be Synchronised. |
---|
169 | Registers Variables which have to be synchronised to the network system. |
---|
170 | */ |
---|
171 | void NotificationQueue::registerVariables() |
---|
172 | { |
---|
173 | registerVariable( this->maxSize_, VariableDirection::ToClient, new NetworkCallback<NotificationQueue>(this, &NotificationQueue::maxSizeChanged)); |
---|
174 | registerVariable( this->targets_, VariableDirection::ToClient, new NetworkCallback<NotificationQueue>(this, &NotificationQueue::targetsChanged)); |
---|
175 | registerVariable( this->displayTime_, VariableDirection::ToClient, new NetworkCallback<NotificationQueue>(this, &NotificationQueue::displayTimeChanged)); |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | /** |
---|
180 | @brief |
---|
181 | Updates the NotificationQueue. |
---|
182 | Updates by clearing the queue and requesting all relevant Notifications from the NotificationManager and inserting them into the queue. |
---|
183 | This is called by the NotificationManager when the Notifications have changed so much, that the NotificationQueue may have to re-initialize his operations. |
---|
184 | */ |
---|
185 | void NotificationQueue::update(void) |
---|
186 | { |
---|
187 | this->clear(); |
---|
188 | |
---|
189 | std::multimap<std::time_t, Notification*>* notifications = new std::multimap<std::time_t, Notification*>; |
---|
190 | // Get the Notifications sent in the interval from now to now minus the display time. |
---|
191 | if(this->displayTime_ == INF) |
---|
192 | NotificationManager::getInstance().getNewestNotifications(this, notifications, this->getMaxSize()); |
---|
193 | else |
---|
194 | NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_); |
---|
195 | |
---|
196 | if(!notifications->empty()) |
---|
197 | { |
---|
198 | // Add all Notifications that have been created after this NotificationQueue was created. |
---|
199 | for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) |
---|
200 | { |
---|
201 | if(it->first >= this->creationTime_) |
---|
202 | this->push(it->second, it->first); |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | delete notifications; |
---|
207 | |
---|
208 | COUT(4) << "NotificationQueue '" << this->getName() << "' updated." << std::endl; |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | @brief |
---|
213 | Updates the NotificationQueue by adding an new Notification. |
---|
214 | @param notification |
---|
215 | Pointer to the Notification. |
---|
216 | @param time |
---|
217 | The time the Notification was sent. |
---|
218 | */ |
---|
219 | void NotificationQueue::update(Notification* notification, const std::time_t & time) |
---|
220 | { |
---|
221 | assert(notification); |
---|
222 | |
---|
223 | this->push(notification, time); |
---|
224 | |
---|
225 | COUT(4) << "NotificationQueue '" << this->getName() << "' updated. A new Notification has been added." << std::endl; |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | @brief |
---|
230 | Adds (pushes) a Notification to the NotificationQueue. |
---|
231 | It inserts it into the storage containers, creates a corresponding container and pushes the notification message to the GUI. |
---|
232 | @param notification |
---|
233 | The Notification to be pushed. |
---|
234 | @param time |
---|
235 | The time when the Notification has been sent. |
---|
236 | */ |
---|
237 | void NotificationQueue::push(Notification* notification, const std::time_t & time) |
---|
238 | { |
---|
239 | assert(notification); |
---|
240 | |
---|
241 | NotificationContainer* container = new NotificationContainer; |
---|
242 | container->notification = notification; |
---|
243 | container->time = time; |
---|
244 | |
---|
245 | // If the maximum size of the NotificationQueue has been reached the last (least recently added) Notification is removed. |
---|
246 | if(this->getSize() >= this->getMaxSize()) |
---|
247 | this->pop(); |
---|
248 | |
---|
249 | this->size_++; |
---|
250 | |
---|
251 | this->ordering_.insert(container); |
---|
252 | // Insert the Notification at the begin of the list (vector, actually). |
---|
253 | this->notifications_.insert(this->notifications_.begin(), container); |
---|
254 | |
---|
255 | // Inform that a Notification was pushed. |
---|
256 | this->notificationPushed(notification); |
---|
257 | |
---|
258 | COUT(5) << "Notification \"" << notification->getMessage() << "\" pushed to NotificationQueue '" << this->getName() << "'" << endl; |
---|
259 | COUT(3) << "NotificationQueue \"" << this->getName() << "\": " << notification->getMessage() << endl; |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | @brief |
---|
264 | Removes (pops) the least recently added Notification form the NotificationQueue. |
---|
265 | */ |
---|
266 | void NotificationQueue::pop(void) |
---|
267 | { |
---|
268 | NotificationContainer* container = this->notifications_.back(); |
---|
269 | // Get all the NotificationContainers that were sent the same time the NotificationContainer we want to pop was sent. |
---|
270 | std::pair<std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator, std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator> iterators = this->ordering_.equal_range(container); |
---|
271 | |
---|
272 | // Iterate through all suspects and remove the container as soon as we find it. |
---|
273 | for(std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator it = iterators.first; it != iterators.second; it++) |
---|
274 | { |
---|
275 | if(container == *it) |
---|
276 | { |
---|
277 | COUT(5) << "Notification \"" << (*it)->notification->getMessage() << "\" popped from NotificationQueue '" << this->getName() << "'" << endl; |
---|
278 | this->ordering_.erase(it); |
---|
279 | break; |
---|
280 | } |
---|
281 | } |
---|
282 | this->notifications_.pop_back(); |
---|
283 | |
---|
284 | this->size_--; |
---|
285 | |
---|
286 | delete container; |
---|
287 | |
---|
288 | // Inform that a Notification was popped. |
---|
289 | this->notificationPopped(); |
---|
290 | } |
---|
291 | |
---|
292 | /** |
---|
293 | @brief |
---|
294 | Removes the Notification that is stored in the input NotificationContainer. |
---|
295 | @param containerIterator |
---|
296 | An iterator to the NotificationContainer to be removed. |
---|
297 | */ |
---|
298 | void NotificationQueue::remove(const std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator& containerIterator) |
---|
299 | { |
---|
300 | std::vector<NotificationContainer*>::iterator it = std::find(this->notifications_.begin(), this->notifications_.end(), *containerIterator); |
---|
301 | // Get the index at which the Notification is. |
---|
302 | std::vector<NotificationContainer*>::difference_type index = it - this->notifications_.begin (); |
---|
303 | |
---|
304 | COUT(5) << "Notification \"" << (*it)->notification->getMessage() << "\" removed from NotificationQueue '" << this->getName() << "'" << endl; |
---|
305 | |
---|
306 | this->ordering_.erase(containerIterator); |
---|
307 | this->notifications_.erase(it); |
---|
308 | |
---|
309 | this->size_--; |
---|
310 | |
---|
311 | delete *containerIterator; |
---|
312 | |
---|
313 | // TODO: index automatically cast? |
---|
314 | // Inform that a Notification was removed. |
---|
315 | this->notificationRemoved(index); |
---|
316 | } |
---|
317 | |
---|
318 | /** |
---|
319 | @brief |
---|
320 | Clears the NotificationQueue by removing all NotificationContainers. |
---|
321 | @param noGraphics |
---|
322 | If this is set to true the GUI is not informed of the clearing of the NotificationQueue. This is needed only internally. |
---|
323 | */ |
---|
324 | void NotificationQueue::clear(bool noGraphics) |
---|
325 | { |
---|
326 | COUT(4) << "Clearing NotificationQueue " << this->getName() << "." << endl; |
---|
327 | this->ordering_.clear(); |
---|
328 | // Delete all NotificationContainers in the list. |
---|
329 | for(std::vector<NotificationContainer*>::iterator it = this->notifications_.begin(); it != this->notifications_.end(); it++) |
---|
330 | delete *it; |
---|
331 | |
---|
332 | this->notifications_.clear(); |
---|
333 | this->size_ = 0; |
---|
334 | } |
---|
335 | |
---|
336 | /** |
---|
337 | @brief |
---|
338 | Sets the name of the NotificationQueue. |
---|
339 | @param name |
---|
340 | The name to be set. |
---|
341 | */ |
---|
342 | void NotificationQueue::setName(const std::string& name) |
---|
343 | { |
---|
344 | this->name_ = name; |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | @brief |
---|
349 | Sets the maximum number of displayed Notifications. |
---|
350 | @param size |
---|
351 | The size to be set. |
---|
352 | */ |
---|
353 | void NotificationQueue::setMaxSize(unsigned int size) |
---|
354 | { |
---|
355 | if(this->maxSize_ == size) |
---|
356 | return; |
---|
357 | |
---|
358 | if(size == 0) |
---|
359 | { |
---|
360 | COUT(2) << "Trying to set maximal size of NotificationQueue '" << this->getName() << "' to 0. Ignoring..." << endl; |
---|
361 | return; |
---|
362 | } |
---|
363 | |
---|
364 | this->maxSize_ = size; |
---|
365 | this->maxSizeChanged(); |
---|
366 | } |
---|
367 | |
---|
368 | /** |
---|
369 | @brief |
---|
370 | Is called when the maximum number of displayed Notifications has changed. |
---|
371 | */ |
---|
372 | void NotificationQueue::maxSizeChanged(void) |
---|
373 | { |
---|
374 | if(this->isRegistered()) |
---|
375 | this->update(); |
---|
376 | } |
---|
377 | |
---|
378 | /** |
---|
379 | @brief |
---|
380 | Sets the maximum number of seconds a Notification is displayed. |
---|
381 | @param time |
---|
382 | The number of seconds a Notification is displayed. |
---|
383 | */ |
---|
384 | void NotificationQueue::setDisplayTime(int time) |
---|
385 | { |
---|
386 | if(this->displayTime_ == time) |
---|
387 | return; |
---|
388 | |
---|
389 | if(time != NotificationQueue::INF && time <= 0) |
---|
390 | { |
---|
391 | COUT(2) << "Trying to set display time of NotificationQueue '" << this->getName() << "' to non-positive value. Ignoring..." << endl; |
---|
392 | } |
---|
393 | |
---|
394 | this->displayTime_ = time; |
---|
395 | this->displayTimeChanged(); |
---|
396 | } |
---|
397 | |
---|
398 | /** |
---|
399 | @brief |
---|
400 | Is called when the maximum number of seconds a Notification is displayed has changed. |
---|
401 | */ |
---|
402 | void NotificationQueue::displayTimeChanged(void) |
---|
403 | { |
---|
404 | if(this->isRegistered()) |
---|
405 | this->update(); |
---|
406 | } |
---|
407 | |
---|
408 | /** |
---|
409 | @brief |
---|
410 | Produces all targets of the NotificationQueue concatenated as string, with commas (',') as separators. |
---|
411 | @return |
---|
412 | Returns the targets as a string. |
---|
413 | */ |
---|
414 | const std::string& NotificationQueue::getTargets(void) const |
---|
415 | { |
---|
416 | std::stringstream stream; |
---|
417 | bool first = true; |
---|
418 | // Iterate through the set of targets. |
---|
419 | for(std::set<std::string>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++) |
---|
420 | { |
---|
421 | if(!first) |
---|
422 | stream << ", "; |
---|
423 | else |
---|
424 | first = false; |
---|
425 | stream << *it; |
---|
426 | } |
---|
427 | |
---|
428 | return *(new std::string(stream.str())); |
---|
429 | } |
---|
430 | |
---|
431 | /** |
---|
432 | @brief |
---|
433 | Sets the targets of the NotificationQueue. |
---|
434 | The targets are the senders whose Notifications are displayed in this queue. |
---|
435 | @param targets |
---|
436 | Accepts a string of targets, each separated by commas (','), spaces are ignored. |
---|
437 | */ |
---|
438 | void NotificationQueue::setTargets(const std::string & targets) |
---|
439 | { |
---|
440 | this->targets_.clear(); |
---|
441 | |
---|
442 | SubString string = SubString(targets, ",", " ", false); |
---|
443 | for(unsigned int i = 0; i < string.size(); i++) |
---|
444 | this->targets_.insert(string[i]); |
---|
445 | |
---|
446 | this->targetsChanged(); |
---|
447 | } |
---|
448 | |
---|
449 | /** |
---|
450 | @brief |
---|
451 | Is called when the NotificationQueue's targets have changed. |
---|
452 | */ |
---|
453 | void NotificationQueue::targetsChanged(void) |
---|
454 | { |
---|
455 | // TODO: Why? |
---|
456 | if(this->isRegistered()) |
---|
457 | { |
---|
458 | NotificationManager::getInstance().unregisterQueue(this); |
---|
459 | NotificationManager::getInstance().registerQueue(this); |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | /** |
---|
464 | @brief |
---|
465 | Pops all Notifications from the NotificationQueue. |
---|
466 | @return |
---|
467 | Returns true if successful, false if not. |
---|
468 | */ |
---|
469 | bool NotificationQueue::tidy(void) |
---|
470 | { |
---|
471 | while(this->size_ > 0) |
---|
472 | this->pop(); |
---|
473 | return true; |
---|
474 | } |
---|
475 | |
---|
476 | } |
---|
477 | |
---|