Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/modules/notifications/NotificationQueue.cc @ 8651

Last change on this file since 8651 was 8651, checked in by scheusso, 13 years ago

first attempt to make notifications synchronisable
not yet succeeded though (some callbacks missing)

  • Property svn:eol-style set to native
File size: 15.2 KB
Line 
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
43namespace 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 1s 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 );
174        registerVariable( this->targets_, VariableDirection::ToClient );
175        registerVariable( this->displayTime_, VariableDirection::ToClient );
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
366        if(this->isRegistered())
367            this->update();
368    }
369
370    /**
371    @brief
372        Sets the maximum number of seconds a Notification is displayed.
373    @param time
374        The number of seconds a Notification is displayed.
375    */
376    void NotificationQueue::setDisplayTime(int time)
377    {
378        if(this->displayTime_ == time)
379            return;
380
381        if(time != NotificationQueue::INF && time <= 0)
382        {
383            COUT(2) << "Trying to set display time of NotificationQueue '" << this->getName() << "' to non-positive value. Ignoring..." << endl;
384        }
385           
386        this->displayTime_ = time;
387
388        if(this->isRegistered())
389            this->update();
390    }
391
392    /**
393    @brief
394        Produces all targets of the NotificationQueue concatenated as string, with commas (',') as separators.
395    @return
396        Returns the targets as a string.
397    */
398    const std::string& NotificationQueue::getTargets(void) const
399    {
400        std::stringstream stream;
401        bool first = true;
402        // Iterate through the set of targets.
403        for(std::set<std::string>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++)
404        {
405            if(!first)
406                stream << ", ";
407            else
408                first = false;
409            stream << *it;
410        }
411
412        return *(new std::string(stream.str()));
413    }
414
415    /**
416    @brief
417        Sets the targets of the NotificationQueue.
418        The targets are the senders whose Notifications are displayed in this queue.
419    @param targets
420        Accepts a string of targets, each separated by commas (','), spaces are ignored.
421    */
422    void NotificationQueue::setTargets(const std::string & targets)
423    {
424        this->targets_.clear();
425
426        SubString string = SubString(targets, ",", " ", false);
427        for(unsigned int i = 0; i < string.size(); i++)
428            this->targets_.insert(string[i]);
429
430        // TODO: Why?
431        if(this->isRegistered())
432        {
433            NotificationManager::getInstance().unregisterQueue(this);
434            NotificationManager::getInstance().registerQueue(this);
435        }
436    }
437
438    /**
439    @brief
440        Pops all Notifications from the NotificationQueue.
441    @return
442        Returns true if successful, false if not.
443    */
444    bool NotificationQueue::tidy(void)
445    {
446        while(this->size_ > 0)
447            this->pop();
448        return true;
449    }
450
451}
452
Note: See TracBrowser for help on using the repository browser.