| 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 NotificationManager.cc | 
|---|
| 31 |     @brief Implementation of the NotificationManager class. | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #include "NotificationManager.h" | 
|---|
| 35 |  | 
|---|
| 36 | #include "core/CoreIncludes.h" | 
|---|
| 37 |  | 
|---|
| 38 | #include <set> | 
|---|
| 39 |  | 
|---|
| 40 | #include "Notification.h" | 
|---|
| 41 | #include "NotificationQueue.h" | 
|---|
| 42 |  | 
|---|
| 43 | namespace orxonox | 
|---|
| 44 | { | 
|---|
| 45 |  | 
|---|
| 46 |     const std::string NotificationManager::ALL = "all"; | 
|---|
| 47 |     const std::string NotificationManager::NONE = "none"; | 
|---|
| 48 |  | 
|---|
| 49 |     NotificationManager* NotificationManager::singletonRef_s = NULL; | 
|---|
| 50 |  | 
|---|
| 51 |     /** | 
|---|
| 52 |     @brief | 
|---|
| 53 |         Constructor. Registers the Object. | 
|---|
| 54 |     */ | 
|---|
| 55 |     NotificationManager::NotificationManager() | 
|---|
| 56 |     { | 
|---|
| 57 |         RegisterRootObject(NotificationManager); | 
|---|
| 58 |  | 
|---|
| 59 |         assert(singletonRef_s == 0); | 
|---|
| 60 |         singletonRef_s = this; | 
|---|
| 61 |  | 
|---|
| 62 |         this->highestIndex_ = 0; | 
|---|
| 63 |     } | 
|---|
| 64 |  | 
|---|
| 65 |     /** | 
|---|
| 66 |     @brief | 
|---|
| 67 |         Destructor. | 
|---|
| 68 |     */ | 
|---|
| 69 |     NotificationManager::~NotificationManager() | 
|---|
| 70 |     { | 
|---|
| 71 |     } | 
|---|
| 72 |  | 
|---|
| 73 |     /** | 
|---|
| 74 |     @brief | 
|---|
| 75 |         Returns the current (and single) instance of the NotificationManager. Creates one, if there isn't one to begin with. | 
|---|
| 76 |     @return | 
|---|
| 77 |         Returns a reference to the single instance of the NotificationManager. | 
|---|
| 78 |     */ | 
|---|
| 79 |     /*static*/ NotificationManager & NotificationManager::getInstance() | 
|---|
| 80 |     { | 
|---|
| 81 |         assert(singletonRef_s); | 
|---|
| 82 |         return *singletonRef_s; | 
|---|
| 83 |     } | 
|---|
| 84 |      | 
|---|
| 85 |     /** | 
|---|
| 86 |     @brief | 
|---|
| 87 |         Registers a Notification within the NotificationManager and makes sure that the Notification is displayed in all the NotificationQueues associated with its sender. | 
|---|
| 88 |     @param notification | 
|---|
| 89 |         The Notification to be registered. | 
|---|
| 90 |     @return | 
|---|
| 91 |         Returns true if successful. | 
|---|
| 92 |     */ | 
|---|
| 93 |     bool NotificationManager::registerNotification(Notification* notification) | 
|---|
| 94 |     { | 
|---|
| 95 |      | 
|---|
| 96 |         if(notification == NULL) //!< A NULL-Notification cannot be registered. | 
|---|
| 97 |             return false; | 
|---|
| 98 |          | 
|---|
| 99 |         std::time_t time = std::time(0); //TDO: Doesn't this expire? //!< Get current time. | 
|---|
| 100 |          | 
|---|
| 101 |         this->allNotificationsList_.insert(std::pair<std::time_t,Notification*>(time,notification)); | 
|---|
| 102 |          | 
|---|
| 103 |         if(notification->getSender() == NONE) //!< If the sender has no specific name, then the Notification is only added to the list of all Notifications. | 
|---|
| 104 |             return true; | 
|---|
| 105 |          | 
|---|
| 106 |         bool all = false; | 
|---|
| 107 |         if(notification->getSender() == ALL) //!< If all are the sender, then the Notifications is added to every NotificationQueue. | 
|---|
| 108 |             all = true; | 
|---|
| 109 |          | 
|---|
| 110 |         //!< Insert the notification in all queues that have its sender as target. | 
|---|
| 111 |         for(std::map<NotificationQueue*,int>::iterator it = this->queueList_.begin(); it != this->queueList_.end(); it++) //!< Iterate through all queues. | 
|---|
| 112 |         { | 
|---|
| 113 |             std::set<std::string> set = it->first->getTargetsSet(); | 
|---|
| 114 |             if(all || set.find(notification->getSender()) != set.end() || set.find(ALL) != set.end()) //TDO: Make sure this works. | 
|---|
| 115 |             { | 
|---|
| 116 |                 this->notificationLists_[it->second]->insert(std::pair<std::time_t,Notification*>(time,notification)); //!< Insert the Notification in the Notifications list of the current NotificationQueue. | 
|---|
| 117 |                 it->first->update(notification, time); //!< Update the queue. | 
|---|
| 118 |             } | 
|---|
| 119 |         } | 
|---|
| 120 |          | 
|---|
| 121 |         COUT(3) << "Notification registered with the NotificationManager." << std::endl; | 
|---|
| 122 |          | 
|---|
| 123 |         return true; | 
|---|
| 124 |     } | 
|---|
| 125 |      | 
|---|
| 126 |     /** | 
|---|
| 127 |     @brief | 
|---|
| 128 |         Registers a NotificationQueue within the NotificationManager. | 
|---|
| 129 |     @param queue | 
|---|
| 130 |         The NotificationQueue to be registered. | 
|---|
| 131 |     @return | 
|---|
| 132 |         Returns true if successful. | 
|---|
| 133 |     */ | 
|---|
| 134 |     bool NotificationManager::registerQueue(NotificationQueue* queue) | 
|---|
| 135 |     { | 
|---|
| 136 |         this->highestIndex_ += 1; | 
|---|
| 137 |         int index = this->highestIndex_; | 
|---|
| 138 |          | 
|---|
| 139 |         this->queueList_[queue] = index; //!< Add the NotificationQueue to the list of queues. | 
|---|
| 140 |          | 
|---|
| 141 |         std::set<std::string> set = queue->getTargetsSet(); //TDO: Works this? | 
|---|
| 142 |          | 
|---|
| 143 |         //! If all senders are the target of the queue, then the list of notification for that specific queue is te same as the list of all Notifications. | 
|---|
| 144 |         if(set.find(ALL) != set.end()) | 
|---|
| 145 |         { | 
|---|
| 146 |             this->notificationLists_[index] = &this->allNotificationsList_; | 
|---|
| 147 |             COUT(3) << "NotificationQueue registered with the NotificationManager." << std::endl; | 
|---|
| 148 |             return true; | 
|---|
| 149 |         } | 
|---|
| 150 |          | 
|---|
| 151 |         this->notificationLists_[index] = new std::multimap<std::time_t,Notification*>; | 
|---|
| 152 |         std::multimap<std::time_t,Notification*> map = *this->notificationLists_[index]; | 
|---|
| 153 |          | 
|---|
| 154 |         //! Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationQueue. | 
|---|
| 155 |         for(std::multimap<std::time_t,Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++) | 
|---|
| 156 |         { | 
|---|
| 157 |             if(set.find(it->second->getSender()) != set.end()) //!< Checks whether the overlay has the sender of the current notification as target. | 
|---|
| 158 |             { | 
|---|
| 159 |                 map.insert(std::pair<std::time_t,Notification*>(it->first, it->second)); | 
|---|
| 160 |             } | 
|---|
| 161 |         } | 
|---|
| 162 |          | 
|---|
| 163 |         queue->update(); //!< Update the queue. | 
|---|
| 164 |  | 
|---|
| 165 |         COUT(3) << "NotificationQueue registered with the NotificationManager." << std::endl; | 
|---|
| 166 |          | 
|---|
| 167 |         return true; | 
|---|
| 168 |     } | 
|---|
| 169 |      | 
|---|
| 170 |     /** | 
|---|
| 171 |     @brief | 
|---|
| 172 |         Fetches the Notifications for a specific NotificationQueue in a specified timeframe. | 
|---|
| 173 |     @param queue | 
|---|
| 174 |         The NotificationQueue the Notifications are fetched for. | 
|---|
| 175 |     @param map | 
|---|
| 176 |         A multimap, in which the notifications are stored. | 
|---|
| 177 |     @param timeFrameStart | 
|---|
| 178 |         The start time of the timeframe. | 
|---|
| 179 |     @param timeFrameEnd | 
|---|
| 180 |         The end time of the timeframe. | 
|---|
| 181 |     @return | 
|---|
| 182 |         Returns true if successful. | 
|---|
| 183 |     */ | 
|---|
| 184 |     bool NotificationManager::getNotifications(NotificationQueue* queue, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd) | 
|---|
| 185 |     { | 
|---|
| 186 |         if(queue == NULL || map == NULL) | 
|---|
| 187 |             return false; | 
|---|
| 188 |  | 
|---|
| 189 |         std::multimap<std::time_t,Notification*>* notifications = this->notificationLists_[this->queueList_[queue]]; //!< The Notifications for the input NotificationQueue. | 
|---|
| 190 |          | 
|---|
| 191 |         if(notifications == NULL) //!< Returns NULL, if there are no Notifications. | 
|---|
| 192 |             return true; | 
|---|
| 193 |      | 
|---|
| 194 |         std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest; | 
|---|
| 195 |         itLowest = notifications->lower_bound(timeFrameStart); | 
|---|
| 196 |         itHighest = notifications->upper_bound(timeFrameStart); | 
|---|
| 197 |          | 
|---|
| 198 |         for(it = itLowest; it != itHighest; it++) //!< Iterate through the Notifications from the start of the time Frame to the end of it. | 
|---|
| 199 |         { | 
|---|
| 200 |             map->insert(std::pair<std::time_t,Notification*>(it->first,it->second)); //!< Add the found Notifications to the map. | 
|---|
| 201 |         } | 
|---|
| 202 |          | 
|---|
| 203 |         return true; | 
|---|
| 204 |     } | 
|---|
| 205 |  | 
|---|
| 206 | } | 
|---|