package net.tomp2p.holep;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.tomp2p.connection.Dispatcher;
import net.tomp2p.message.Message;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.ChannelHandler.Sharable;

@Sharable
public class DuplicatesHandler extends SimpleChannelInboundHandler<Message>{

	private static final Logger LOG = LoggerFactory.getLogger(DuplicatesHandler.class);
	private final Dispatcher dispatcher;
	int messageId = 0;
	boolean first = true;
	
	public DuplicatesHandler(Dispatcher dispatcher) {
		this.dispatcher = dispatcher;
	}
	
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {
		if (msg.intList() != null && !msg.intList().isEmpty()) {
			if (first) {
				first = false;
				messageId = msg.intAt(0);
				dispatcher.channelRead(ctx, msg);
			} else if (msg.intAt(0) != messageId) {
				LOG.error("THIS PART SHOULD NEVER BE CALLED!!!!");
				dispatcher.channelRead(ctx, msg);
			} else {
				// ignore the duplicate message
			}
		}
	}
}

@@ -916,188 +916,378 @@
 %0A%09%09%09
-%7D else if (msg.intAt(0) != messageId) %7B%0A%09%09%09%09LOG.error(%22THIS PART SHOULD NEVER BE CALLED!!!!%22);%0A%09%09%09%09dispatcher.channelRead(ctx, msg);%0A%09%09%09%7D else %7B%0A%09%09%09%09// ignore the duplicate message
+%09LOG.debug(%22message with original messageId = %22 + messageId + %22 has been received!%22);%0A%09%09%09%7D else if (msg.intAt(0) != messageId) %7B%0A%09%09%09%09LOG.debug(%22message with messageId = %22 + msg.intAt(0) + %22 has been received! This wasn't planned!%22);%0A%09%09%09%09dispatcher.channelRead(ctx, msg);%0A%09%09%09%7D else %7B%0A%09%09%09%09LOG.trace(%22message with original messageId = %22 + messageId + %22 has been ignored!%22);
 %0A%09%09%09

fix and added LOG's