package communication.protocols;

import java.util.Set;

import communication.messages.HelloReplyMessage;
import communication.messages.HelloType;
import communication.messages.Message;
import data.system.NodeInfo;

public class HelloProtocol implements Protocol {

	private Set<NodeInfo> nodes;
	
	public HelloProtocol(Set<NodeInfo> nodes) {
		this.nodes = nodes;
	}
	
	@Override
	public String getAcceptedMessageType() {
		return "HelloMessage";
	}

	@Override
	public Message processMessage(Message message) {
		NodeInfo node = message.getSender();
		
		HelloReplyMessage reply = new HelloReplyMessage();
		
		HelloType type = (HelloType) message.getContents();
		if (type == HelloType.HI) {
			if (!nodes.contains(node)) {
				nodes.add(node);
			}
			
			reply.setContents(HelloType.HI);
		} else {
			if (!nodes.contains(node)) {
				nodes.remove(node);
			}
			
			reply.setContents(HelloType.BYE);
		}
		
		reply.setHelloer(message.getSender());
	
		return reply;
	}

	@Override
	public boolean hasReply() {
		return true;
	}
}

@@ -27,16 +27,43 @@
 ocols;%0A%0A
+import java.util.Iterator;%0A
 import j
@@ -719,38 +719,32 @@
 ) %7B%0A%09%09%09if (!
-nodes.
 contains
 (node)) %7B%0A%09%09
@@ -723,32 +723,36 @@
 %09%09%09if (!contains
+Node
 (node)) %7B%0A%09%09%09%09no
@@ -834,22 +834,16 @@
 f (!
-nodes.
 contains
 (nod
@@ -838,16 +838,20 @@
 contains
+Node
 (node)) 
@@ -1028,24 +1028,24 @@
 asReply() %7B%0A
-
 %09%09return tru
@@ -1050,10 +1050,218 @@
 rue;%0A%09%7D%0A
+%09%0A%09private boolean containsNode(NodeInfo node) %7B%0A%09%09Iterator%3CNodeInfo%3E _nodes = nodes.iterator();%0A%09%09while (_nodes.hasNext()) %7B%0A%09%09%09if (_nodes.next().equals(node)) %7B%0A%09%09%09%09return true;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return false;%0A%09%7D%0A
 %7D%0A

Fix NodeInfo filtering in HelloProtocol