Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/interfaces/NotificationListener.cc @ 11099

Last change on this file since 11099 was 11099, checked in by muemart, 8 years ago

Fix loads of doxygen warnings and other documentation issues

  • Property svn:eol-style set to native
File size: 6.2 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/NetworkFunctionIncludes.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    /*static*/ const std::string NotificationListener::COMMAND_NONE("none");
50   
51    registerStaticNetworkFunction(NotificationListener::sendHelper);
52   
53    RegisterAbstractClass(NotificationListener).inheritsFrom<OrxonoxInterface>();
54
55    NotificationListener::NotificationListener()
56    {
57        RegisterObject(NotificationListener);
58    }
59
60    /**
61    @brief
62        Helper method to send both notifications and commands over the network.
63    @param message
64        The message/command that should be sent.
65    @param sender
66        The sender that sent the notification/command.
67    @param sendMode
68        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.
69    @param clientId
70        The id of the client the notification/command should be sent to.
71    @param isCommand
72        Whether the message is a notification or a command.
73    @param messageType
74        The type of the notification, can be either 'info' or 'important'.
75    */
76    /*static*/ void NotificationListener::sendNetworkHelper(const std::string& message, const std::string& sender, NotificationSendMode sendMode, unsigned int clientId, bool isCommand, NotificationMessageType messageType)
77    {
78        // If we're in standalone mode or we're already no the right client we create and send the notification/command.
79        if(GameMode::isStandalone() || sendMode == NotificationSendMode::local || (sendMode ==  NotificationSendMode::network && Host::getPlayerID() == clientId))
80        {
81            sendHelper(message, sender, isCommand, messageType);
82        }
83        // If we're on the server (and the server is not the intended recipient of the notification/command) we send it over the network.
84        else if(GameMode::isServer() && sendMode == NotificationSendMode::network && Host::getPlayerID() != clientId)
85        {
86            callStaticNetworkFunction(&NotificationListener::sendHelper, clientId, message, sender, isCommand, messageType);
87        }
88        else if(GameMode::isServer() && sendMode == NotificationSendMode::broadcast)
89        {
90            // TODO: Works as intended?
91            callStaticNetworkFunction(&NotificationListener::sendHelper, NETWORK_PEER_ID_BROADCAST, message, sender, isCommand, messageType);
92        }
93    }
94
95    /**
96    @brief
97        Helper method to register a notification/execute a command with all NotificationListeners after it has been sent over the network.
98    @param message
99        The notification/command to be sent/executed.
100    @param sender
101        The sender that sent the notification/command.
102    @param isCommand
103        Whether the message is a command or a notification.
104    @param type
105        The type of the notification.
106    */
107    /*static*/ void NotificationListener::sendHelper(const std::string& message, const std::string& sender, bool isCommand, NotificationMessageType type)
108    {
109        // Iterate through all NotificationListeners and notify them by calling the method they overloaded.
110        for(NotificationListener* listener : ObjectList<NotificationListener>())
111        {
112            // If the notification is a message.
113            if(!isCommand)
114                listener->registerNotification(message, sender, type);
115
116            // If the notification is a command.
117            if(isCommand)
118            {
119                NotificationCommand command = str2Command(message);
120                if(command != NotificationCommand::none)
121                    listener->executeCommand(command, sender);
122            }
123        }
124    }
125
126    /**
127    @brief
128        Helper method. Converts a string into the enum for a command.
129    @param string
130        The string to be converted.
131    @return
132        Returns the corresponding enum, NotificationCommand::none if the command doesn't exist.
133    */
134    /*static*/ NotificationCommand NotificationListener::str2Command(const std::string& string)
135    {
136        NotificationCommand command = NotificationCommand::none;
137
138        if(string == NotificationListener::COMMAND_CLEAR)
139            command = NotificationCommand::clear;
140
141        return command;
142    }
143
144    /**
145    @brief
146        Helper method. Converts a command enum into its corresponding string.
147    @param command
148        The command to be converted.
149    @return
150        Returns the corresponding string.
151    */
152    /*static*/ const std::string& NotificationListener::command2Str(NotificationCommand command)
153    {
154        switch(command)
155        {
156            case NotificationCommand::clear:
157                return NotificationListener::COMMAND_CLEAR;
158            default:
159                return NotificationListener::COMMAND_NONE;
160        }
161    }
162   
163}
Note: See TracBrowser for help on using the repository browser.