Overview
Features
Download
Documentation
Community
Add-Ons & Services

Filter notifications on the Target content

Please post support and help requests here.

Filter notifications on the Target content

Postby dbarzo » 17 Jan 2013, 14:06

Hi,

in my server application I have to dispatch received messages to various observers.
But each of them should receive notifications only for the messages they are subscribed.
I not have found a way to "filter" the notifications so, for now, my solution (I don't know if it is correct) is to post notifications to all and, inside the Target, discard the messages it doesn't care.

Code: Select all
class MessageNotification : public Notification {
  public:
    MessageNotification(IMessage & msg) : m_msg(msg) {};
    IMessage & getMessage() { return m_msg; }
  private:
    IMessage & m_msg;
}

class MessageEventHandler {
  public:
    virtual void OnMsgReceived(IMessage msg) = 0;

    ERROR_CODE RegisterMsg(IMessage & msg) {
      m_MessagesMap.insert(msg.getOpCode());
    }
   
    ERROR_CODE UnRegisterMsg(IMessage & msg) {
      m_MessagesMap.erase(m_MessagesMap.find(msg.getOpCode()));
    }
   
    ERROR_CODE UnRegisterAllMsg() {
      m_MessagesMap.clear();
    }

    void HandleNotification(const AutoPtr<MessageNotification>& pNf) {
      IMessage& msg = pNf->getMessage();
      std::set<int>::iterator it = m_MessagesMap.find(msg.getOpCode());
      if (it != m_MessagesMap.end())
        this->OnMsgReceived(msg);
    }

  private:
    std::set<INT>  m_MessagesMap;
};

class IMessageDispatcher {
  public:       
 
    virtual ERROR_CODE AddMsgHandler(MessageEventHandler * pHandler) {
      nc.addObserver(
          NObserver<MessageEventHandler, MessageNotification>(*pHandler, &MessageEventHandler::HandleNotification)
      );
    };

    void MessageReceived(char* buffer) {
      // Create a message
      IMessage msg;
      nc.postNotification(new MessageNotification(msg));
    }
   
  private: 
    NotificationCenter nc;
}


Is there a way to "filtering" before send notifications?

Regards,
Daniele.
dbarzo
 
Posts: 39
Joined: 14 Jan 2013, 17:15
Location: Italy

Re: Filter notifications on the Target content

Postby alex » 20 Jan 2013, 18:58

alex
 
Posts: 1143
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 3 guests

cron