Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutoriallevel2/src/orxonox/interfaces/NotificationListener.cc @ 8445

Last change on this file since 8445 was 8445, checked in by dafrick, 13 years ago

NotificationListener is new an entity which is informed of new notifications. The NotificationManager is, so far, the only NotificationListener. This means that Notifications can now be sent from within orxonox (though not libraries or external).
Also introduced notification commands to affect the NotificationQueues in more ways than just have them display messages (e.g. clearing them).
Added a message type which allows to send Notifications of different importance, allowing the NotificationQueus to display them differently.

File size: 5.8 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 NotificationListener.cc
31    @brief Implementation of the NotificationListener class.
32*/
33
34#include "core/CoreIncludes.h"
35#include "network/Host.h"
36#include "network/NetworkFunction.h"
37#include "util/SubString.h"
38
39#include "interfaces/NotificationListener.h"
40
41namespace orxonox
42{
43   
44    /*static*/ const std::string NotificationListener::ALL("all");
45    /*static*/ const std::string NotificationListener::NONE("none");
46   
47    // Commands
48    /*static*/ const std::string NotificationListener::COMMAND_CLEAR("clear");
49   
50    registerStaticNetworkFunction(NotificationListener::sendHelper);
51   
52    NotificationListener::NotificationListener()
53    {
54        RegisterRootObject(NotificationListener);
55    }
56
57    /**
58    @brief
59        Helper method to send both notifications and commands over the network.
60    @param message
61        The message/command that should be sent.
62    @param sender
63        The sender that sent the notification/command.
64    @param sendMode
65        The mode in which the notification/command is sent, can be 'local' to send the notification to the client where this function is executed, 'network' if the notification is to be sent to the client with the specified clientID, or 'broadcast' if the notification should be sent to all hosts.
66    @param clientId
67        The id of the client the notification/command should be sent to.
68    @param isCommand
69        Whether the message is a notification or a command.
70    @param messageType
71        The type of the notification, can be either 'info' or 'important'.
72    */
73    /*static*/ void NotificationListener::sendNetworkHelper(const std::string& message, const std::string& sender, notificationSendMode::Value sendMode, unsigned int clientId, bool isCommand, notificationMessageType::Value messageType)
74    {
75        // If we're in standalone mode or we're already no the right client we create and send the notification/command.
76        if(GameMode::isStandalone() || sendMode == notificationSendMode::local || (sendMode ==  notificationSendMode::network && Host::getPlayerID() == clientId))
77        {
78            sendHelper(message, sender, isCommand, messageType);
79        }
80        // If we're on the server (and the server is not the intended recipient of the notification/command) we send it over the network.
81        else if(GameMode::isServer() && sendMode == notificationSendMode::network && Host::getPlayerID() != clientId)
82        {
83            callStaticNetworkFunction(NotificationListener::sendHelper, clientId, message, sender, (unsigned int)messageType);
84        }
85        else if(GameMode::isServer() && sendMode == notificationSendMode::broadcast)
86        {
87            // TODO: Works as intended?
88            callStaticNetworkFunction(NotificationListener::sendHelper, NETWORK_PEER_ID_BROADCAST, message, sender, (unsigned int)messageType);
89        }
90    }
91
92    /**
93    @brief
94        Helper method to register a notification/execute a command with all NotificationListeners after it has been sent over the network.
95    @param message
96        The notification/command to be sent/executed.
97    @param sender
98        The sender that sent the notification/command.
99    @param isCommand
100        Whether the message is a command or a notification.
101    @param messageType
102        The type of the notification.
103    */
104    /*static*/ void NotificationListener::sendHelper(const std::string& message, const std::string& sender, bool isCommand, unsigned int messageType)
105    {
106        // Iterate through all NotificationListeners and notify them by calling the method they overloaded.
107        for(ObjectList<NotificationListener>::iterator it = ObjectList<NotificationListener>::begin(); it != ObjectList<NotificationListener>::end(); ++it)
108        {
109            // If the notification is a message.
110            if(!isCommand && it->registerNotification(message, sender, notificationMessageType::Value(messageType)))
111                COUT(3) << "Notification \"" << message << "\" sent." << std::endl;
112
113            // If the notification is a command.
114            if(isCommand)
115            {
116                notificationCommand::Value command = str2Command(message);
117                if(command != notificationCommand::none && it->executeCommand(command, sender))
118                    COUT(3) << "Command \"" << message << "\" executed." << std::endl;
119            }
120        }
121    }
122
123    /**
124    @brief
125        Helper method. Converts a string into the enum for a command.
126    @param string
127        The string to be converted.
128    @return
129        Returns the corresponding enum, notificationCommand::none if the command doesn't exist.
130    */
131    /*static*/ notificationCommand::Value NotificationListener::str2Command(const std::string& string)
132    {
133        notificationCommand::Value command = notificationCommand::none;
134
135        if(string == NotificationListener::COMMAND_CLEAR)
136            command = notificationCommand::clear;
137
138        return command;
139    }
140   
141}
Note: See TracBrowser for help on using the repository browser.