package cs451.net.handler;

import cs451.net.event.*;
import cs451.tools.Pair;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * NetEventHandler for Perfect Link
 *
 * Deliver: Drop Duplicate Packets
 *
 * @author C. Hölzl
 */
public class NetHandlerPL extends NetEventHandlerAbstract {

    private final Set<Pair<Integer,Message>> delivered = ConcurrentHashMap.newKeySet();

    public NetHandlerPL(Class<? extends NetEventHandlerAbstract> deliverLayer, Class<? extends NetEventHandlerAbstract> broadcastLayer) {
        super(deliverLayer,broadcastLayer);
    }

    @Override
    public void deliver(NetEvent event) {
        if(delivered.add(new Pair<>(event.peer.getId(),event.message))){
            deliverNextSync(event);
        }
    }

}
