- Timestamp:
- May 10, 2011, 11:37:22 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/tutoriallevel2/src/orxonox/interfaces/NotificationListener.cc
r8376 r8445 35 35 #include "network/Host.h" 36 36 #include "network/NetworkFunction.h" 37 #include "util/SubString.h" 37 38 38 39 #include "interfaces/NotificationListener.h" … … 41 42 { 42 43 43 const std::string NotificationListener::ALL("all");44 const std::string NotificationListener::NONE("none");44 /*static*/ const std::string NotificationListener::ALL("all"); 45 /*static*/ const std::string NotificationListener::NONE("none"); 45 46 46 registerStaticNetworkFunction(NotificationListener::sendNotificationHelper); 47 // Commands 48 /*static*/ const std::string NotificationListener::COMMAND_CLEAR("clear"); 47 49 50 registerStaticNetworkFunction(NotificationListener::sendHelper); 48 51 49 52 NotificationListener::NotificationListener() … … 54 57 /** 55 58 @brief 56 Sends a Notification with the specified message to the specified client from the specified sender.59 Helper method to send both notifications and commands over the network. 57 60 @param message 58 The message that should be sent.61 The message/command that should be sent. 59 62 @param sender 60 The sender that sent the notification. 61 @param messageMode 62 The mode of the message, can be either 'message' or 'command'. 63 The sender that sent the notification/command. 63 64 @param sendMode 64 The mode in which the notification 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.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. 65 66 @param clientId 66 The id of the client the notification should be sent to. 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'. 67 72 */ 68 /*static*/ void NotificationListener::sendN otification(const std::string& message, const std::string& sender, notificationMessageMode::Value messageMode, notificationSendMode::Value sendMode, unsigned int clientId)73 /*static*/ void NotificationListener::sendNetworkHelper(const std::string& message, const std::string& sender, notificationSendMode::Value sendMode, unsigned int clientId, bool isCommand, notificationMessageType::Value messageType) 69 74 { 70 // If we're in standalone mode or we're already no the right client we create and send the Notification.75 // If we're in standalone mode or we're already no the right client we create and send the notification/command. 71 76 if(GameMode::isStandalone() || sendMode == notificationSendMode::local || (sendMode == notificationSendMode::network && Host::getPlayerID() == clientId)) 72 77 { 73 send NotificationHelper(message, sender, messageMode);78 sendHelper(message, sender, isCommand, messageType); 74 79 } 75 // If we're on the server (and the server is not the intended recipient of the Notification) we send it over the network.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. 76 81 else if(GameMode::isServer() && sendMode == notificationSendMode::network && Host::getPlayerID() != clientId) 77 82 { 78 callStaticNetworkFunction(NotificationListener::send NotificationHelper, clientId, message, sender, (unsigned int)messageMode);83 callStaticNetworkFunction(NotificationListener::sendHelper, clientId, message, sender, (unsigned int)messageType); 79 84 } 80 85 else if(GameMode::isServer() && sendMode == notificationSendMode::broadcast) 81 86 { 82 87 // TODO: Works as intended? 83 callStaticNetworkFunction(NotificationListener::send NotificationHelper, NETWORK_PEER_ID_BROADCAST, message, sender, (unsigned int)messageMode);88 callStaticNetworkFunction(NotificationListener::sendHelper, NETWORK_PEER_ID_BROADCAST, message, sender, (unsigned int)messageType); 84 89 } 85 90 } 86 87 /*static*/ void NotificationListener::sendNotificationHelper(const std::string& message, const std::string& sender, unsigned int messageMode) 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) 88 105 { 89 106 // Iterate through all NotificationListeners and notify them by calling the method they overloaded. 90 107 for(ObjectList<NotificationListener>::iterator it = ObjectList<NotificationListener>::begin(); it != ObjectList<NotificationListener>::end(); ++it) 91 108 { 92 if(messageMode == 0 && it->registerNotification(message, sender)) 109 // If the notification is a message. 110 if(!isCommand && it->registerNotification(message, sender, notificationMessageType::Value(messageType))) 93 111 COUT(3) << "Notification \"" << message << "\" sent." << std::endl; 94 95 if(messageMode == 1) 96 it->executeCommand(message, sender); 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 } 97 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; 98 139 } 99 140
Note: See TracChangeset
for help on using the changeset viewer.