package org.kne.cloud.network.klalb;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class LineDecitionComparator implements Comparator<KLALBRemoteLink> {
	private Map<KLALBRemoteLink,Long> predictedlatencys=new HashMap<>();
	public LineDecitionComparator (List<KLALBRemoteLink> krs,KLALBPacket curr,int priority) {
		for (Iterator<KLALBRemoteLink> iterator = krs.iterator(); iterator.hasNext();) {
			KLALBRemoteLink klalbRemoteSocket = (KLALBRemoteLink) iterator.next();
			long x=klalbRemoteSocket.getMonitor().getOutDelay();
			//long x=klalbRemoteSocket.getMonitor().getLatency()>>1;
			/*long al=0;
			al=klalbRemoteSocket.statLengthBefore(priority)+curr.getLength();
			long speed=klalbRemoteSocket.getMonitor(). getOutSpeed();
			if(speed==0) {
				if(al>0) {
					x=Long.MAX_VALUE;
				}
			}else {
			x+=al*1000000000L/speed;
			}*/

			/*System.out.println(klalbRemoteSocket);
			System.out.println(klalbRemoteSocket.getSendQueue().size());
			System.out.println(x);*/
			predictedlatencys.put(klalbRemoteSocket, x);
		}
	}
	public Map<KLALBRemoteLink, Long> getPredictedlatencys() {
		return predictedlatencys;
	}

	@Override
	public int compare(KLALBRemoteLink o1, KLALBRemoteLink o2) {	
		long t1=predictedlatencys.get(o1);
		long t2=predictedlatencys.get(o2);
		if(t1>t2) {
			return 1;
		}else if(t1<t2){
			return -1;
		}else {
			return 0;
		}
	}

}
