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.





