Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/modules/questsystem/notifications/NotificationManager.cc @ 6944

Last change on this file since 6944 was 6944, checked in by dafrick, 14 years ago

No more seg-faults when restarting a game, quests still not working properly the second thime, though.
Also replaced QuestListener mode 'receive' with 'start' in quest_test, which appears to be the correct term.

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