Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/notifications/NotificationManager.cc @ 7307

Last change on this file since 7307 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • Property svn:eol-style set to native
File size: 10.9 KB
RevLine 
[2280]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
[2911]29/**
[3196]30    @file
[2911]31    @brief Implementation of the NotificationManager class.
32*/
33
[2280]34#include "NotificationManager.h"
35
[2911]36#include <set>
37
[7284]38#include "util/ScopedSingletonManager.h"
[3196]39#include "core/CoreIncludes.h"
[2910]40#include "Notification.h"
[5619]41#include "interfaces/NotificationListener.h"
[2280]42
[2435]43namespace orxonox
44{
[2280]45
[6417]46    const std::string NotificationManager::ALL("all");
47    const std::string NotificationManager::NONE("none");
[2911]48
[5929]49    ManageScopedSingleton(NotificationManager, ScopeID::Root, false);
[2911]50
51    /**
52    @brief
53        Constructor. Registers the Object.
54    */
55    NotificationManager::NotificationManager()
[2280]56    {
[2911]57        RegisterRootObject(NotificationManager);
58
59        this->highestIndex_ = 0;
[2280]60    }
61
[2911]62    /**
63    @brief
64        Destructor.
65    */
[2280]66    NotificationManager::~NotificationManager()
67    {
[7163]68
[2280]69    }
[2501]70
[2911]71    /**
72    @brief
[5619]73        Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationListeners associated with its sender.
[2911]74    @param notification
75        The Notification to be registered.
76    @return
77        Returns true if successful.
78    */
79    bool NotificationManager::registerNotification(Notification* notification)
80    {
[5619]81
[2911]82        if(notification == NULL) //!< A NULL-Notification cannot be registered.
83            return false;
[5619]84
[5748]85        std::time_t time = std::time(0); //TODO: Doesn't this expire? //!< Get current time.
[5619]86
[2911]87        this->allNotificationsList_.insert(std::pair<std::time_t,Notification*>(time,notification));
[5619]88
[2911]89        if(notification->getSender() == NONE) //!< If the sender has no specific name, then the Notification is only added to the list of all Notifications.
90            return true;
[5619]91
[2911]92        bool all = false;
[5619]93        if(notification->getSender() == ALL) //!< If all are the sender, then the Notifications is added to every NotificationListener.
[2911]94            all = true;
[5619]95
96        //!< Insert the notification in all listeners that have its sender as target.
97        for(std::map<NotificationListener*,int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) //!< Iterate through all listeners.
[2435]98        {
[2911]99            std::set<std::string> set = it->first->getTargetsSet();
[5748]100            if(all || set.find(notification->getSender()) != set.end() || set.find(ALL) != set.end()) //TODO: Make sure this works.
[2280]101            {
[5619]102                this->notificationLists_[it->second]->insert(std::pair<std::time_t,Notification*>(time,notification)); //!< Insert the Notification in the Notifications list of the current NotificationListener.
103                it->first->update(notification, time); //!< Update the listener.
[7163]104                std::map<Notification*, unsigned int>::iterator counterIt = this->listenerCounter_.find(notification);
105                if(counterIt == this->listenerCounter_.end())
106                    this->listenerCounter_[notification] = 1;
107                else
108                    this->listenerCounter_[notification] = counterIt->second + 1;
[2435]109            }
110        }
[5619]111
[7163]112        COUT(4) << "Notification registered with the NotificationManager." << std::endl;
[5619]113
[2280]114        return true;
115    }
[5619]116
[2911]117    /**
118    @brief
[7163]119        Unregisters a Notification within the NotificationManager.
120    @param notification
121        A pointer to the Notification to be unregistered.
122    @param listener
123        A pointer to the NotificationListener the Notification is unregistered for.
124    */
125    void NotificationManager::unregisterNotification(Notification* notification, NotificationListener* listener)
126    {
127        assert(notification);
128        assert(listener);
129
130        // If the Notification was removed from the list of Notifications of the input NotificationListener, the counter for the Notification of the number of NotificationListeners it is present in is decremented.
131        if(this->removeNotification(notification, *(this->notificationLists_.find(this->listenerList_.find(listener)->second)->second)))
132            this->listenerCounter_[notification] = this->listenerCounter_[notification] - 1;
133
134        // If the Notification is no longer present in any of the NotificationListeners it can be removed from the map of all Notifications and be destroyed.
135        if(this->listenerCounter_[notification] == (unsigned int) 0)
136        {
137            this->removeNotification(notification, this->allNotificationsList_);
138            this->listenerCounter_.erase(notification);
139            notification->destroy();
140        }
141
142        COUT(4) << "Notification unregistered with the NotificationManager." << std::endl;
143    }
144
145    /**
146    @brief
147        Helper method that removes an input notification form an input map.
148    @param notification
149        A pointer to the notification to be removed.
150    @param map
151        The map the notification should be removed from.
152    @return
153        Returns true if successful.
154    */
155    bool NotificationManager::removeNotification(Notification* notification, std::multimap<std::time_t, Notification*>& map)
156    {
157        // Iterates through all items in the map until the Notification is found.
158        //TODO: Do more efficiently?
159        for(std::multimap<std::time_t, Notification*>::iterator it = map.begin(); it != map.end(); it++)
160        {
161            if(it->second == notification)
162            {
163                map.erase(it);
164                return true;
165            }
166        }
167        return false;
168    }
169
170    /**
171    @brief
[5619]172        Registers a NotificationListener within the NotificationManager.
173    @param listener
174        The NotificationListener to be registered.
[2911]175    @return
176        Returns true if successful.
177    */
[5619]178    bool NotificationManager::registerListener(NotificationListener* listener)
[2280]179    {
[2911]180        this->highestIndex_ += 1;
181        int index = this->highestIndex_;
[5619]182
183        this->listenerList_[listener] = index; //!< Add the NotificationListener to the list of listeners.
184
[5760]185        std::set<std::string> set = listener->getTargetsSet(); //TODO: Does this work?
[5619]186
187        //! If all senders are the target of the listener, then the list of notification for that specific listener is te same as the list of all Notifications.
[2911]188        if(set.find(ALL) != set.end())
[2284]189        {
[2911]190            this->notificationLists_[index] = &this->allNotificationsList_;
[7163]191            COUT(4) << "NotificationListener registered with the NotificationManager." << std::endl;
[2911]192            return true;
[2284]193        }
[5619]194
[2911]195        this->notificationLists_[index] = new std::multimap<std::time_t,Notification*>;
196        std::multimap<std::time_t,Notification*> map = *this->notificationLists_[index];
[5619]197
198        //! Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationListener.
[2911]199        for(std::multimap<std::time_t,Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
200        {
201            if(set.find(it->second->getSender()) != set.end()) //!< Checks whether the overlay has the sender of the current notification as target.
202            {
[7163]203                map.insert(std::pair<std::time_t, Notification*>(it->first, it->second));
204                std::map<Notification*, unsigned int>::iterator counterIt = this->listenerCounter_.find(it->second);
205                if(counterIt == this->listenerCounter_.end())
206                    this->listenerCounter_[it->second] = 1;
207                else
208                    this->listenerCounter_[it->second] = counterIt->second + 1;
[2911]209            }
210        }
[2501]211
[5619]212        listener->update(); //!< Update the listener.
213
[7163]214        COUT(4) << "NotificationListener registered with the NotificationManager." << std::endl;
[5619]215
[2911]216        return true;
[2280]217    }
[5619]218
[2911]219    /**
220    @brief
[7163]221        Unregisters a NotificationListener withing the NotificationManager.
222    */
223    void NotificationManager::unregisterListener(NotificationListener* listener)
224    {
225        assert(listener);
226
227        int identifier = this->listenerList_.find(listener)->second;
228        std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(identifier)->second;
229
230        // Make sure all Notifications are removed.
231        std::multimap<std::time_t, Notification*>::iterator it = map->begin();
232        while(it != map->end())
233        {
234            this->unregisterNotification(it->second, listener);
235            it = map->begin();
236        }
237
238        this->listenerList_.erase(listener);
239        this->notificationLists_.erase(identifier);
240
241        // If the map is not the map of all notifications, delete it.
242        if(map != &this->allNotificationsList_)
243            delete map;
244
245        COUT(4) << "NotificationListener unregistered with the NotificationManager." << std::endl;
246    }
247
248    /**
249    @brief
[5619]250        Fetches the Notifications for a specific NotificationListener in a specified timeframe.
251    @param listener
252        The NotificationListener the Notifications are fetched for.
[2911]253    @param map
254        A multimap, in which the notifications are stored.
255    @param timeFrameStart
256        The start time of the timeframe.
257    @param timeFrameEnd
258        The end time of the timeframe.
259    @return
260        Returns true if successful.
261    */
[5619]262    bool NotificationManager::getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
[2346]263    {
[5619]264        if(listener == NULL || map == NULL)
[2911]265            return false;
[2501]266
[5619]267        std::multimap<std::time_t,Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; //!< The Notifications for the input NotificationListener.
268
[2911]269        if(notifications == NULL) //!< Returns NULL, if there are no Notifications.
270            return true;
[5619]271
[2911]272        std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest;
273        itLowest = notifications->lower_bound(timeFrameStart);
274        itHighest = notifications->upper_bound(timeFrameStart);
[5619]275
[2911]276        for(it = itLowest; it != itHighest; it++) //!< Iterate through the Notifications from the start of the time Frame to the end of it.
[2349]277        {
[2911]278            map->insert(std::pair<std::time_t,Notification*>(it->first,it->second)); //!< Add the found Notifications to the map.
[2349]279        }
[5619]280
[2911]281        return true;
[2346]282    }
[2280]283
284}
Note: See TracBrowser for help on using the repository browser.