Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/modules/notifications/NotificationDispatcher.cc @ 10904

Last change on this file since 10904 was 10478, checked in by landauf, 10 years ago

callStaticNetworkFunction() and callMemberNetworkFunction() are now template functions instead of macros.
simplified function call interface: always pass five MultiType-references. Check if MultiType.null() evaluates to true to see if the argument was defined.
moved references to NetworkFunctionManager to NetworkFunctionIncludes.cc.
this also fixed a linker error in MSVC by including NetworkFunctionIncludes.h in a build-unit inside the network library.

  • Property svn:eol-style set to native
File size: 6.6 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 thes
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 NotificationDispatcher.cc
31    @brief Implementation of the NotificationDispatcher class.
32*/
33
34#include "NotificationDispatcher.h"
35
36#include "core/CoreIncludes.h"
37#include "core/EventIncludes.h"
38#include "core/XMLPort.h"
39#include "network/NetworkFunctionIncludes.h"
40#include "network/Host.h"
41
42#include "infos/PlayerInfo.h"
43#include "interfaces/NotificationListener.h"
44#include "interfaces/PlayerTrigger.h"
45#include "worldentities/pawns/Pawn.h"
46
47namespace orxonox
48{
49
50    RegisterUnloadableClass(NotificationDispatcher);
51
52    registerMemberNetworkFunction(NotificationDispatcher, broadcastHelper);
53    registerMemberNetworkFunction(NotificationDispatcher, dispatch);
54
55    /**
56    @brief
57        Default constructor. Initializes the object.
58    */
59    NotificationDispatcher::NotificationDispatcher(Context* context) : BaseObject(context), Synchronisable(context)
60    {
61        RegisterObject(NotificationDispatcher);
62
63        this->sender_ = NotificationListener::NONE;
64        this->bBroadcast_ = false;
65        this->registerVariables();
66    }
67
68    /**
69    @brief
70        Destructor.
71    */
72    NotificationDispatcher::~NotificationDispatcher()
73    {
74
75    }
76
77    /**
78    @brief
79        Method for creating a NotificationDispatcher object through XML.
80    */
81    void NotificationDispatcher::XMLPort(Element& xmlelement, XMLPort::Mode mode)
82    {
83        SUPER(NotificationDispatcher, XMLPort, xmlelement, mode);
84
85        XMLPortParam(NotificationDispatcher, "sender", setSender, getSender, xmlelement, mode);
86        XMLPortParam(NotificationDispatcher, "broadcast", setBroadcasting, isBroadcasting, xmlelement, mode);
87
88        XMLPortEventSink(NotificationDispatcher, BaseObject, "trigger", trigger, xmlelement, mode);
89    }
90
91    void NotificationDispatcher::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
92    {
93        SUPER(NotificationDispatcher, XMLEventPort, xmlelement, mode);
94
95        XMLPortEventState(NotificationDispatcher, BaseObject, "trigger", trigger, xmlelement, mode);
96    }
97
98    /**
99    @brief
100        Registers variables for synchronisation.
101    */
102    void NotificationDispatcher::registerVariables(void)
103    {
104        registerVariable(this->sender_, VariableDirection::ToClient);
105    }
106
107    /**
108    @brief
109        Broadcasts a specific Notification.
110    */
111    void NotificationDispatcher::broadcast(void)
112    {
113        // TODO: Needed?
114        const std::string message = this->createNotificationMessage();
115        NotificationListener::sendNotification(message, this->getSender(), notificationMessageType::info, notificationSendMode::local);
116
117        // Broadcast
118        if(!GameMode::isStandalone())
119        {
120            callMemberNetworkFunction(&NotificationDispatcher::broadcastHelper, this->getObjectID(), NETWORK_PEER_ID_BROADCAST);
121        }
122    }
123
124    /**
125    @brief
126        Helper function for broadcast.
127    */
128    void NotificationDispatcher::broadcastHelper(void)
129    {
130        this->dispatch(Host::getPlayerID());
131    }
132
133    /**
134    @brief
135        Dispatches a Notification with a message supplied by the createNotificationMessage() method, which can be overloaded.
136    @param clientId
137        The id of the client the notification should be dispatched to.
138    */
139    void NotificationDispatcher::dispatch(unsigned int clientId)
140    {
141        // We don't call sendNotification() directly on the server, because if might be necessary that createNotificationMessage() is executed on the client as the message may be client-specific.
142        if(GameMode::isStandalone() || Host::getPlayerID() == clientId || this->getSyncMode() == ObjectDirection::None)
143        {
144            const std::string message = this->createNotificationMessage();
145            // TODO: Make the type configurable.
146            NotificationListener::sendNotification(message, this->getSender(), notificationMessageType::info, notificationSendMode::network, clientId);
147        }
148        else if(GameMode::isServer())
149        {
150            callMemberNetworkFunction(&NotificationDispatcher::dispatch, this->getObjectID(), clientId, clientId);
151        }
152    }
153
154    /**
155    @brief
156        Is called when the NotificationDispatcher is triggered.
157    @param triggered
158        Whether it has been triggered or untriggered. The NotificationDispatcher only reacts to the first kind of events.
159    @param trigger
160        The object that caused the event to be fired.
161    @return
162        Returns true if the NotificationDispatcher was successfully triggered.
163    */
164    bool NotificationDispatcher::trigger(bool triggered, BaseObject* trigger)
165    {
166        if(!triggered || !this->isActive()) // If the NotificationDispatcher is inactive it cannot be executed.
167            return false;
168
169        orxout(verbose, context::notifications) << "NotificationDispatcher (&" << this << ") triggered." << endl;
170
171        // If the NotificationDispatcher is set to broadcast.
172        if(this->isBroadcasting())
173        {
174            this->broadcast();
175            return true;
176        }
177
178        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
179        PlayerInfo* player = NULL;
180
181        // If the trigger is a PlayerTrigger.
182        if(pTrigger != NULL)
183        {
184            if(!pTrigger->isForPlayer())  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
185                return false;
186            else
187                player = pTrigger->getTriggeringPlayer();
188        }
189        else
190            return false;
191
192        if(player == NULL)
193        {
194            orxout(verbose, context::notifications) << "The NotificationDispatcher was triggered by an entity other than a Pawn. (" << trigger->getIdentifier()->getName() << ")" << endl;
195            return false;
196        }
197
198        this->dispatch(player->getClientID());
199
200        return true;
201    }
202
203}
Note: See TracBrowser for help on using the repository browser.