[ 
https://issues.apache.org/jira/browse/GOSSIP-56?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15869626#comment-15869626
 ] 

ASF GitHub Bot commented on GOSSIP-56:
--------------------------------------

GitHub user makrusak opened a pull request:

    https://github.com/apache/incubator-gossip/pull/34

    GOSSIP-56 GossipCore should allow registration of handlers

    Current solution is:
    `for (Map.Entry<Class, MessageHandler> handler : handlers.entrySet()) {
         Class messageClass = handler.getKey();
         MessageHandler messageHandler = handler.getValue();
         if (messageClass.isAssignableFrom(base.getClass())){
           messageHandler.invoke(this, gossipManager, base);
           break;
    }`
    It's thread-safe and it's not just handlers.get(base.getClass()) because, 
for example, UdpActiveGossipMessage extends ActiveGossipMessage and this type 
of message have to be handled as ActiveGossipMessage, but getClass() returns 
UdpActiveGossipMessage.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/makrusak/incubator-gossip GOSSIP-56

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/incubator-gossip/pull/34.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #34
    
----
commit 2b9dae138953ce561ecba51d145c4720e208e3ba
Author: Maxim Rusak <mak-ru...@yandex.ru>
Date:   2017-02-16T09:35:56Z

    GOSSIP-56 GossipCore should allow registration of handlers

----


> GossipCore should allow registration of handlers
> ------------------------------------------------
>
>                 Key: GOSSIP-56
>                 URL: https://issues.apache.org/jira/browse/GOSSIP-56
>             Project: Gossip
>          Issue Type: New Feature
>            Reporter: Edward Capriolo
>            Assignee: Maxim Rusak
>
> Currently GossipCore is a case statement. It would be better if we registered 
> handlers. This would allows users to extend the protocol.
> # We need a hashmap of Class -> Handler
> # When GossipCore.receive(Base message) gets a message it gets the class of 
> the message and looks it up in the hashmap
> # Builder class allows users to specify other handlers
> # Common handlers are enabled by default
> Something like this:
> {noformat}
>   if (base instanceof ShutdownMessage){
>       ShutdownMessage s = (ShutdownMessage) base;
>       GossipDataMessage m = new GossipDataMessage();
>       m.setKey(ShutdownMessage.PER_NODE_KEY);
>       m.setNodeId(s.getNodeId());
>       m.setPayload(base);
>       m.setTimestamp(System.currentTimeMillis());
>       m.setExpireAt(System.currentTimeMillis() + 30L * 1000L);
>       addPerNodeData(m);
>     }
>     if (base instanceof GossipDataMessage) {
>       UdpGossipDataMessage message = (UdpGossipDataMessage) base;
>       addPerNodeData(message);
>     }
> {noformat}
> Replace with:
> {noformat}
> Map<Class,Handler> handlers...
> {
>   handlers.put(ShutdownMessage.class, new ShutdownHandler(GossipCore);
> }
> Handler h = handlers.get(base.class)
> h.invoke(base)
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to