Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merging presentation branch back into trunk.
There are many new features and also a lot of other changes and bugfixes, if you want to know, digg through the svn log.
Not everything is yet working as it should, but it should be fairly stable. If you habe any bug reports, just send me an email.

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