package cc.monitors;

import java.util.Vector;

import cc.Client;
import cc.Parameters;

public class BufferMonitor extends EventMonitor {
	private int maxBufferSize = Parameters.getMaxBucket();
	private int lastLabel = 0;
	private Vector bucketOfLostPackets = new Vector();

	public void evaluate() {
		int bufferSize = bucketOfLostPackets.size();
		
		if (bufferSize > maxBufferSize) {
			String result = new String();
		
			for (int i=0 ; i<bucketOfLostPackets.size() ; i++){
				result += bucketOfLostPackets.elementAt(i) + ";";
			}

			Client.requestPackets(result);
		}
	}

	public void update(Event event) {
		int label = event.getLabel();

		if (firstTime) {
			lastLabel = label;
			firstTime = false;
			return;
		}

		if (label != (lastLabel + 1)) {
			if (label > (lastLabel + 1)) {
				for (int i = (lastLabel + 1); i < label; i++)
					bucketOfLostPackets.add(new Integer(i));
			} else if (label < (lastLabel + 1)) {
				if (!(bucketOfLostPackets.remove(new Integer(label))))
					System.out.println("OPS");
			}
		}
		lastLabel = label;
		
		evaluate();
	}
}
