Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 10, 2011, 11:37:22 PM (14 years ago)
Author:
dafrick
Message:

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.

Location:
code/branches/tutoriallevel2/src/orxonox/interfaces
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/tutoriallevel2/src/orxonox/interfaces/NotificationListener.cc

    r8376 r8445  
    3535#include "network/Host.h"
    3636#include "network/NetworkFunction.h"
     37#include "util/SubString.h"
    3738
    3839#include "interfaces/NotificationListener.h"
     
    4142{
    4243   
    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");
    4546   
    46     registerStaticNetworkFunction(NotificationListener::sendNotificationHelper);
     47    // Commands
     48    /*static*/ const std::string NotificationListener::COMMAND_CLEAR("clear");
    4749   
     50    registerStaticNetworkFunction(NotificationListener::sendHelper);
    4851   
    4952    NotificationListener::NotificationListener()
     
    5457    /**
    5558    @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.
    5760    @param message
    58         The message that should be sent.
     61        The message/command that should be sent.
    5962    @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.
    6364    @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.
    6566    @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'.
    6772    */
    68     /*static*/ void NotificationListener::sendNotification(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)
    6974    {
    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.
    7176        if(GameMode::isStandalone() || sendMode == notificationSendMode::local || (sendMode ==  notificationSendMode::network && Host::getPlayerID() == clientId))
    7277        {
    73             sendNotificationHelper(message, sender, messageMode);
     78            sendHelper(message, sender, isCommand, messageType);
    7479        }
    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.
    7681        else if(GameMode::isServer() && sendMode == notificationSendMode::network && Host::getPlayerID() != clientId)
    7782        {
    78             callStaticNetworkFunction(NotificationListener::sendNotificationHelper, clientId, message, sender, (unsigned int)messageMode);
     83            callStaticNetworkFunction(NotificationListener::sendHelper, clientId, message, sender, (unsigned int)messageType);
    7984        }
    8085        else if(GameMode::isServer() && sendMode == notificationSendMode::broadcast)
    8186        {
    8287            // TODO: Works as intended?
    83             callStaticNetworkFunction(NotificationListener::sendNotificationHelper, NETWORK_PEER_ID_BROADCAST, message, sender, (unsigned int)messageMode);
     88            callStaticNetworkFunction(NotificationListener::sendHelper, NETWORK_PEER_ID_BROADCAST, message, sender, (unsigned int)messageType);
    8489        }
    8590    }
    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)
    88105    {
    89106        // Iterate through all NotificationListeners and notify them by calling the method they overloaded.
    90107        for(ObjectList<NotificationListener>::iterator it = ObjectList<NotificationListener>::begin(); it != ObjectList<NotificationListener>::end(); ++it)
    91108        {
    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)))
    93111                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            }
    97120        }
     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;
    98139    }
    99140   
  • code/branches/tutoriallevel2/src/orxonox/interfaces/NotificationListener.h

    r8378 r8445  
    4848namespace orxonox
    4949{
    50    
    51     namespace notificationMessageMode
     50    // TODO: Document.
     51    namespace notificationMessageType
    5252    {
    5353        enum Value {
    54             message,
    55             command
     54            info,
     55            important
    5656        };
    5757    }
     
    6565        };
    6666    }
     67   
     68    namespace notificationCommand
     69    {
     70        enum Value {
     71            none,
     72            clear
     73        };
     74    }
    6775
    6876    // TODO: Update doc.
     
    7179        NotificationListener interface.
    7280
    73         The NotificationListener interface presents a means to being informed when @ref orxonox::Notification "Notifications" in the target set of this NotificationListener change. (e.g. @ref orxonox::Notification "Notifications" were added or removed)
    74         When inheriting from a NotificationListener it is important to register (in the constructor) and unregister (in the destructor) it to and from the @ref orxonox::NotificationManager "NotificationManager".
     81        The NotificationListener interface (or more precisely abstract class) presents a means of being informed when a new @ref orxonox::Notification "Notification" is sent.
     82        The NotificationListener can be used to send a new notification message (with NotificationListener::sendNotification() ) or a new notification command (with NotificationListener::sendCommand() ). Each NotificationListener is then informed about the new @ref orxonox::Notification "Notification" and can take appropriate action. Currently the only NotificationListener ist the @ref orxonox::NotificationManager "NotificationManager" singleton.
     83
     84        When inheriting from a NotificationListener it is important to provide an appropriate implementation of registerNotification() and executeCommand().
    7585
    7686    @author
     
    7888       
    7989    @ingroup Notifications
     90    @todo Consistent terminology between message, notification and command.
    8091    */
    8192    class _OrxonoxExport NotificationListener : virtual public OrxonoxClass
     
    8495            NotificationListener();
    8596            virtual ~NotificationListener() {}
     97
     98            /**
     99            @brief Sends a Notification with the specified message to the specified client from the specified sender.
     100            @param message The message that should be sent.
     101            @param sender The sender that sent the notification. Default is 'none'.
     102            @param messageType The type of the message, can be either 'info' or 'important'. Default is 'info'.
     103            @param sendMode 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. Default is notificationSendMode::local.
     104            @param clientId The id of the client the notification should be sent to. Default is 0.
     105            */
     106            static void sendNotification(const std::string& message, const std::string& sender = NotificationListener::NONE, notificationMessageType::Value messageType = notificationMessageType::info, notificationSendMode::Value sendMode = notificationSendMode::local, unsigned int clientId = 0)
     107                { NotificationListener::sendNetworkHelper(message, sender, sendMode, clientId, false, messageType); }
     108            /**
     109            @brief Sends a specified command to the specified client from the specified sender.
     110            @param message The command that should be sent (and later executed).
     111            @param sender The sender that sent the notification. Default is 'none'.
     112            @param sendMode The mode in which the command is sent, can be 'local' to send the command to the client where this function is executed, 'network' if the command is to be sent to the client with the specified clientID, or 'broadcast' if the command should be sent to all hosts. Default is notificationSendMode::local.
     113            @param clientId The id of the client the command should be sent to. Default is 0.
     114            */
     115            static void sendCommand(const std::string& command, const std::string& sender = NotificationListener::NONE, notificationSendMode::Value sendMode = notificationSendMode::local, unsigned int clientId = 0)
     116                { NotificationListener::sendNetworkHelper(command, sender, sendMode, clientId, true); }
     117
     118            static void sendHelper(const std::string& message, const std::string& sender, bool isCommand = false, unsigned int messageMode = 0); // Helper method to register a notification/execute a command with all NotificationListeners after it has been sent over the network.
     119
     120            //TODO: Make protected?
    86121           
    87             static const std::string ALL; //!< Static string to indicate a sender that sends to all NotificationListeners.
    88             static const std::string NONE; //!< Static string to indicare a sender that sends to no specific NotificationListener.
     122            /**
     123            @brief Registers a notification with the NotificationListener.
     124                   This needs to be overloaded by each class inheriting from NotificationListener.
     125            @param message The notification's message.
     126            @param sender The sender of the notification.
     127            @param type The type of the notification.
     128            @return Returns true if the notification was successfully registered, false if not.
     129            */
     130            virtual bool registerNotification(const std::string& message, const std::string& sender, notificationMessageType::Value type)
     131                { return false; }
     132            /**
     133            @brief Executes a command with the NotificationListener
     134                   This needs to be overloaded by each class inheriting from NotificationListener.
     135            @param command The command to be executed.
     136            @param sender The sender of the command.
     137            @return Returns true if the command was successfully executed, false if not.
     138            */
     139            virtual bool executeCommand(notificationCommand::Value command, const std::string& sender) { return false; }
    89140           
    90             static void sendNotification(const std::string& message, const std::string& sender = NotificationListener::NONE, notificationMessageMode::Value messageMode = notificationMessageMode::message, notificationSendMode::Value sendMode = notificationSendMode::local, unsigned int clientId = 0);
    91             static void sendNotificationHelper(const std::string& message, const std::string& sender, unsigned int messageMode);
     141            static const std::string ALL; //!< Static string to indicate a sender that sends to all NotificationQueues.
     142            static const std::string NONE; //!< Static string to indicate a sender that sends to no specific NotificationQueues.
    92143           
    93             virtual bool registerNotification(const std::string& message, const std::string& sender) { return false; }
    94             virtual void executeCommand(const std::string& command, const std::string& sender) {}
     144            //! Commands
     145            static const std::string COMMAND_CLEAR;
     146           
     147        protected:
     148            static void sendNetworkHelper(const std::string& message, const std::string& sender, notificationSendMode::Value sendMode, unsigned int clientId, bool isCommand = false, notificationMessageType::Value messageType = notificationMessageType::info); // Helper method to send both notifications and commands over the network.
     149           
     150            static notificationCommand::Value str2Command(const std::string& string); // Helper method. Converts a string into the enum for a command.
    95151    };
    96152}
Note: See TracChangeset for help on using the changeset viewer.