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

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

Github user edwardcapriolo commented on a diff in the pull request:

    https://github.com/apache/incubator-gossip/pull/34#discussion_r101566867
  
    --- Diff: src/main/java/org/apache/gossip/manager/GossipCore.java ---
    @@ -139,74 +130,20 @@ public void shutdown(){
         }
         service.shutdownNow();
       }
    -  
    +
       public void receive(Base base){
    -    if (base instanceof Response){
    -      if (base instanceof Trackable){
    -        Trackable t = (Trackable) base;
    -        requests.put(t.getUuid() + "/" + t.getUriFrom(), (Base) t);
    -      }
    -    }
    -    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);
    -    }
    -    if (base instanceof SharedGossipDataMessage){
    -      UdpSharedGossipDataMessage message = (UdpSharedGossipDataMessage) 
base;
    -      addSharedData(message);
    -    }
    -    if (base instanceof ActiveGossipMessage){
    -      List<GossipMember> remoteGossipMembers = new ArrayList<>();
    -      RemoteGossipMember senderMember = null;
    -      UdpActiveGossipMessage activeGossipMessage = 
(UdpActiveGossipMessage) base;
    -      for (int i = 0; i < activeGossipMessage.getMembers().size(); i++) {
    -        URI u = null;
    -        try {
    -          u = new URI(activeGossipMessage.getMembers().get(i).getUri());
    -        } catch (URISyntaxException e) {
    -          LOGGER.debug("Gossip message with faulty URI", e);
    -          continue;
    -        }
    -        RemoteGossipMember member = new RemoteGossipMember(
    -                activeGossipMessage.getMembers().get(i).getCluster(),
    -                u,
    -                activeGossipMessage.getMembers().get(i).getId(),
    -                activeGossipMessage.getMembers().get(i).getHeartbeat(),
    -                activeGossipMessage.getMembers().get(i).getProperties());
    -        if (i == 0) {
    -          senderMember = member;
    -        } 
    -        if 
(!(member.getClusterName().equals(gossipManager.getMyself().getClusterName()))){
    -          UdpNotAMemberFault f = new UdpNotAMemberFault();
    -          f.setException("Not a member of this cluster " + i);
    -          f.setUriFrom(activeGossipMessage.getUriFrom());
    -          f.setUuid(activeGossipMessage.getUuid());
    -          LOGGER.warn(f);
    -          sendOneWay(f, member.getUri());
    -          continue;
    -        }
    -        remoteGossipMembers.add(member);
    +    for (Map.Entry<Class, MessageHandler> handler : handlers.entrySet()) {
    --- End diff --
    
    Add some testing in this patch. What happens for example if a handler is 
not found? Is the NullPointerException caught and logged in a meaningful way. 
Create your own custom handler for testing show that you can attach it at 
runtime etc.


> 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