Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 2, 2011, 10:20:45 AM (14 years ago)
Author:
dafrick
Message:

Changes in notifications module structure. Notifications can now be sent through the NotificationListener, meaning, they can be sent from anywhere within orxonox (and the modules of course).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/tutoriallevel2/src/modules/notifications/NotificationManager.cc

    r8371 r8374  
    3636#include "core/command/ConsoleCommand.h"
    3737#include "core/CoreIncludes.h"
     38#include "core/GameMode.h"
    3839#include "core/GUIManager.h"
    3940#include "core/LuaState.h"
    40 #include "network/Host.h"
    41 #include "network/NetworkFunction.h"
    4241#include "util/Convert.h"
    4342#include "util/ScopedSingletonManager.h"
     
    5352{
    5453
    55     const std::string NotificationManager::ALL("all");
    56     const std::string NotificationManager::NONE("none");
    57 
    5854    // Register tolua_open function when loading the library.
    5955    DeclareToluaInterface(Notifications);
     
    6460    SetConsoleCommand("enterEditMode", &NotificationManager::enterEditMode);
    6561
    66     registerStaticNetworkFunction(NotificationManager::sendNotification);
    67 
    6862    /**
    6963    @brief
     
    7367    {
    7468        RegisterRootObject(NotificationManager);
    75 
    76         this->highestIndex_ = 0;
    7769
    7870        ModifyConsoleCommand("enterEditMode").setObject(this);
     
    113105        this->queues_.clear();
    114106    }
    115 
    116     /**
    117     @brief
    118         Sends a Notification with the specified message to the specified client from the specified sender.
    119     @param message
    120         The message that should be sent.
    121     @param clientId
    122         The id of the client the notification should be sent to.
    123     @param sender
    124         The sender that sent the notification.
    125     @param isLocal
    126         If this is set to true (false is default), then the Notification is sent to the client where this function is executed, meaning the Notification is sent locally.
    127     */
    128     /*static*/ void NotificationManager::sendNotification(const std::string& message, unsigned int clientId, const std::string& sender, bool isLocal)
    129     {
    130         // If we're in standalone mode or we're already no the right client we create and send the Notification.
    131         if(GameMode::isStandalone() || isLocal || Host::getPlayerID() == clientId)
    132         {
    133             Notification* notification = new Notification(message, sender);
    134             if(NotificationManager::getInstance().registerNotification(notification))
    135                 COUT(3) << "Notification \"" << notification->getMessage() << "\" sent." << std::endl;
    136         }
    137         // If we're on the server (and the server is not the intended recipient of the Notification) we send it over the network.
    138         else if(GameMode::isServer())
    139         {
    140             callStaticNetworkFunction(NotificationManager::sendNotification, clientId, message, clientId, sender);
    141         }
    142     }
    143 
    144     /**
    145     @brief
    146         Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationListeners associated with its sender.
     107   
     108    bool NotificationManager::registerNotification(const std::string& message, const std::string& sender)
     109    {
     110        Notification* notification = new Notification(message, sender);
     111        return this->registerNotification(notification);
     112    }
     113
     114    /**
     115    @brief
     116        Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationQueues associated with its sender.
    147117    @param notification
    148118        The Notification to be registered.
     
    159129        this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification));
    160130
    161         if(notification->getSender() == NotificationManager::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications.
     131        if(notification->getSender() == NotificationListener::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications.
    162132            return true;
    163133
    164134        bool all = false;
    165         if(notification->getSender() == NotificationManager::ALL) // If all are the sender, then the Notifications is added to every NotificationListener.
     135        if(notification->getSender() == NotificationListener::ALL) // If all are the sender, then the Notifications is added to every NotificationQueue.
    166136            all = true;
    167137
    168         // Insert the Notification in all NotificationListeners that have its sender as target.
    169         for(std::map<NotificationListener*, unsigned int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all NotificationListeners.
    170         {
    171             const std::set<std::string>& set = it->first->getTargetsSet();
    172             bool bAll = set.find(NotificationManager::ALL) != set.end();
    173             // If either the Notification has as sender 'all', the NotificationListener displays all Notifications or the NotificationListener has the sender of the Notification as target.
     138        // Insert the Notification in all NotificationQueues that have its sender as target.
     139        for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); it++) // Iterate through all NotificationQueues.
     140        {
     141            const std::set<std::string>& set = it->second->getTargetsSet();
     142            bool bAll = set.find(NotificationListener::ALL) != set.end();
     143            // If either the Notification has as sender 'all', the NotificationQueue displays all Notifications or the NotificationQueue has the sender of the Notification as target.
    174144            if(all || bAll || set.find(notification->getSender()) != set.end())
    175145            {
    176146                if(!bAll)
    177                     this->notificationLists_[it->second]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationListener.
    178                 it->first->update(notification, time); // Update the NotificationListener.
     147                    this->notificationLists_[it->second->getName()]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationQueue.
     148                it->second->update(notification, time); // Update the NotificationQueue.
    179149            }
    180150        }
     
    187157    /**
    188158    @brief
    189         Unregisters a Notification within the NotificationManager for a given NotificationListener.
     159        Unregisters a Notification within the NotificationManager for a given NotificationQueue.
    190160    @param notification
    191161        A pointer to the Notification to be unregistered.
    192     @param listener
    193         A pointer to the NotificationListener the Notification is unregistered for.
    194     */
    195     void NotificationManager::unregisterNotification(Notification* notification, NotificationListener* listener)
     162    @param queue
     163        A pointer to the NotificationQueue the Notification is unregistered for.
     164    */
     165    void NotificationManager::unregisterNotification(Notification* notification, NotificationQueue* queue)
    196166    {
    197167        assert(notification);
    198         assert(listener);
    199 
    200         // Remove the Notification from the list of Notifications of the input NotificationListener.
    201         this->removeNotification(notification, *(this->notificationLists_.find(this->listenerList_.find(listener)->second)->second));
    202 
    203         COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from listener (&" << listener << ")" << std::endl;
     168        assert(queue);
     169
     170        // Remove the Notification from the list of Notifications of the input NotificationQueue.
     171        this->removeNotification(notification, *(this->notificationLists_.find(queue->getName())->second));
     172
     173        COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from NotificationQueue " << queue->getName() << "." << std::endl;
    204174    }
    205175
     
    231201    /**
    232202    @brief
    233         Registers a NotificationListener within the NotificationManager.
    234     @param listener
    235         The NotificationListener to be registered.
    236     @return
    237         Returns true if successful.  Fales if the NotificationListener is already registered.
    238     */
    239     bool NotificationManager::registerListener(NotificationListener* listener)
    240     {
    241         assert(listener);
    242 
    243         // If the NotificationListener is already registered.
    244         if(this->listenerList_.find(listener) != this->listenerList_.end())
    245             return false;
    246 
    247         this->highestIndex_ += 1;
    248         unsigned int index = this->highestIndex_; // An identifier that identifies each registered NotificationListener uniquely.
    249 
    250         this->listenerList_[listener] = index; // Add the NotificationListener to the list of NotificationListeners.
    251 
    252         const std::set<std::string>& set = listener->getTargetsSet();
    253 
    254         // If all senders are the target of the NotificationListener, then the list of Notifications for that specific NotificationListener is the same as the list of all Notifications.
    255         bool bAll = set.find(NotificationManager::ALL) != set.end();
    256         std::multimap<std::time_t, Notification*>* map = NULL;
    257         if(bAll)
    258             this->notificationLists_[index] = &this->allNotificationsList_;
    259         // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationListeners.
    260         else
    261         {
    262             this->notificationLists_[index] = new std::multimap<std::time_t, Notification*>;
    263             map = this->notificationLists_[index];
    264         }
    265 
    266         // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationListener.
    267         for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
    268         {
    269             if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target.
    270                 map->insert(std::pair<std::time_t, Notification*>(it->first, it->second));
    271         }
    272 
    273         listener->update(); // Update the listener.
    274 
    275         COUT(4) << "NotificationListener registered with the NotificationManager." << std::endl;
    276 
    277         return true;
    278     }
    279 
    280     /**
    281     @brief
    282         Unregisters a NotificationListener within the NotificationManager.
    283     @param listener
    284         The NotificationListener to be unregistered.
    285     */
    286     void NotificationManager::unregisterListener(NotificationListener* listener)
    287     {
    288         assert(listener);
    289 
    290         unsigned int identifier = this->listenerList_.find(listener)->second;
    291         std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(identifier)->second;
    292 
    293         // If the map is not the map of all Notifications, make sure all Notifications are unregistered.
    294         std::multimap<std::time_t, Notification*>::iterator it = map->begin();
    295         if(map != &this->allNotificationsList_)
    296         {
    297             while(it != map->end())
    298             {
    299                 this->unregisterNotification(it->second, listener);
    300                 it = map->begin();
    301             }
    302             delete map;
    303         }
    304 
    305         COUT(4) << "NotificationListener '" << identifier << "' unregistered with the NotificationManager." << std::endl;
    306 
    307         // Remove the NotificationListener from the list of NotificationListeners.
    308         this->listenerList_.erase(listener);
    309         // Remove the Notifications list that was associated with the input NotificationListener.
    310         this->notificationLists_.erase(identifier);
    311     }
    312 
    313     /**
    314     @brief
    315         Fetches the Notifications for a specific NotificationListener in a specified timeframe and stores them in the input map.
    316     @param listener
    317         The NotificationListener the Notifications are fetched for.
     203        Fetches the Notifications for a specific NotificationQueue in a specified timeframe and stores them in the input map.
     204    @param queue
     205        The NotificationQueue the Notifications are fetched for.
    318206    @param map
    319207        A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated.
     
    325213        Returns true if successful.
    326214    */
    327     void NotificationManager::getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
    328     {
    329         assert(listener);
     215    void NotificationManager::getNotifications(NotificationQueue* queue, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
     216    {
     217        assert(queue);
    330218        assert(map);
    331219
    332         std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // All the Notifications for the input NotificationListener.
     220        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[queue->getName()]; // All the Notifications for the input NotificationQueue.
    333221
    334222        std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest;
     
    343231    /**
    344232    @brief
    345         Fetches the newest Notifications for a specific NotificationListener and stores them in the input map.
    346     @param listener
    347         The NotificationListener the Notifications are fetched for.
     233        Fetches the newest Notifications for a specific NotificationQueue and stores them in the input map.
     234    @param queue
     235        The NotificationQueue the Notifications are fetched for.
    348236    @param map
    349237        A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated.
     
    353241        Returns true if successful.
    354242    */
    355     void NotificationManager::getNewestNotifications(NotificationListener* listener, std::multimap<std::time_t, Notification*>* map, int numberOfNotifications)
    356     {
    357         assert(listener);
     243    void NotificationManager::getNewestNotifications(NotificationQueue* queue, std::multimap<std::time_t, Notification*>* map, int numberOfNotifications)
     244    {
     245        assert(queue);
    358246        assert(map);
    359247
    360         std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // All the Notifications for the input NotificationListener.
     248        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[queue->getName()]; // All the Notifications for the input NotificationQueue.
    361249
    362250        if(!notifications->empty()) // If the list of Notifications is not empty.
     
    390278    @brief
    391279        Registers a NotificationQueue.
    392         This makes sure that the NotificationQueue can be attained through lua by name. It also makes sure that the NotificationQueue is destroyed upon destruction of the NotificationManager.
     280        This makes sure that the NotificationQueue can be accessed through lua by name. It also makes sure that the NotificationQueue is destroyed upon destruction of the NotificationManager.
    393281    @param queue
    394282        A pointer to the NotificationQueue to be registered.
     
    398286    bool NotificationManager::registerQueue(NotificationQueue* queue)
    399287    {
     288        assert(queue);
     289
     290        // If the NotificationQueue is already registered.
     291        if(this->queues_.find(queue->getName()) != this->queues_.end())
     292            return false;
     293
     294        this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)); // Add the NotificationQueue to the list of NotificationQueues.
     295
     296        const std::set<std::string>& set = queue->getTargetsSet();
     297
     298        // If all senders are the target of the NotificationQueue, then the list of Notifications for that specific NotificationQueue is the same as the list of all Notifications.
     299        bool bAll = set.find(NotificationListener::ALL) != set.end();
     300        std::multimap<std::time_t, Notification*>* map = NULL;
     301        if(bAll)
     302            this->notificationLists_[queue->getName()] = &this->allNotificationsList_;
     303        // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationQueues.
     304        else
     305        {
     306            this->notificationLists_[queue->getName()] = new std::multimap<std::time_t, Notification*>;
     307            map = this->notificationLists_[queue->getName()];
     308        }
     309
     310        // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationQueue.
     311        for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
     312        {
     313            if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target.
     314                map->insert(std::pair<std::time_t, Notification*>(it->first, it->second));
     315        }
     316
     317        queue->update(); // Update the queue.
     318       
    400319        COUT(4) << "NotificationQueue '" << queue->getName() << "' registered with the NotificationManager." << std::endl;
    401         return this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second;
     320        return true;
    402321    }
    403322
     
    410329    void NotificationManager::unregisterQueue(NotificationQueue* queue)
    411330    {
     331        assert(queue);
     332
     333        std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(queue->getName())->second;
     334
     335        // If the map is not the map of all Notifications, make sure all Notifications are unregistered.
     336        std::multimap<std::time_t, Notification*>::iterator it = map->begin();
     337        if(map != &this->allNotificationsList_)
     338        {
     339            while(it != map->end())
     340            {
     341                this->unregisterNotification(it->second, queue);
     342                it = map->begin();
     343            }
     344            delete map;
     345        }
     346
     347        // Remove the NotificationQueue from the list of NotificationQueues.
     348        this->queues_.erase(queue->getName());
     349        // Remove the Notifications list that was associated with the input NotificationQueue.
     350        this->notificationLists_.erase(queue->getName());
     351       
    412352        COUT(4) << "NotificationQueue '" << queue->getName() << "' unregistered with the NotificationManager." << std::endl;
    413         this->queues_.erase(queue->getName());
    414353    }
    415354
     
    424363        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.moveQueue(\"all\", 0, 10, 0.3, 0)");
    425364
    426         NotificationQueue* infoQueue = new NotificationQueue("info", NotificationManager::ALL, 1, -1);
     365        NotificationQueue* infoQueue = new NotificationQueue("info", NotificationListener::ALL, 1, -1);
    427366        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.changeQueueFont(\"info\", 24, \"FFFFFF00\")");
    428367        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.resizeQueue(\"info\", 0.6, 0, " + multi_cast<std::string>(infoQueue->getMaxSize()) + ")");
Note: See TracChangeset for help on using the changeset viewer.