package br.ufrj.cos.redes.delayLossSimulator;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import br.ufrj.cos.redes.fileAccess.Chunk;
import br.ufrj.cos.redes.packet.EndPacket;
import br.ufrj.cos.redes.packet.Package;

public class DelayLossSimulator {

	public static void receive(DatagramSocket clientSocket) throws IOException, ClassNotFoundException {		
		double fail = 0.1;
		double LAMBDA = 50;
		double RTT = 600;
		
		Map<Double, Long> tnXseqNum = new TreeMap<Double, Long>();
		
		boolean keep = true;
		
		try {
			while(keep) {				
				System.out.println("Waiting for chunk...");
				byte[] recvBytes = new byte[1024];
				DatagramPacket recvPkt = new DatagramPacket(recvBytes, recvBytes.length);
				clientSocket.receive(recvPkt);
				
				ObjectInputStream objIStream = new ObjectInputStream(new ByteArrayInputStream(recvPkt.getData()));
				
				Package pkg = null;
				Object obj = objIStream.readObject();
				try {					
					pkg = (Package) obj;					
				} catch (ClassCastException e) {
					EndPacket endPkt = null;					
					try {
						endPkt = (EndPacket) obj;
					} catch (Exception ee) {
						System.out.println("Error in init object");
					}					
					keep = !endPkt.getSendedEndMsg().equals(EndPacket.END_MSG);					
				}
				
				if(keep) {				
					Chunk chunk = pkg.getChunk();
				
					boolean available = (Math.random() < fail) ? false : true;			
					chunk.setAvailable(available);
					
					if(chunk.isAvailable()) {
						ExponentialSampleGenerator generator = new ExponentialSampleGenerator(LAMBDA);
						double x = generator.getSample();
						
						double tn = pkg.getTimeStamp() + RTT/2 + x;		
						pkg.setTimeStamp(tn);
						
						tnXseqNum.put(pkg.getTimeStamp(), chunk.getSeqNum());
						
						//TODO Send to Buffer when the current time was equal to the new timestamp

						System.out.println("chunk number " + pkg.getChunk().getSeqNum() + " received");
						
					} else {
						System.out.println("chunk number " + chunk.getSeqNum() + " was lost");
					}					
				}				
			}
		
			printMap(tnXseqNum);
			System.out.println("number of chunks received: " + tnXseqNum.size());
			System.out.println("All chunks received");
			
		} finally {
			clientSocket.close();
		}
		
	}	
	
	public static void printMap(Map map) {
		Iterator iterator = map.entrySet().iterator(); 
		while(iterator.hasNext()) {
			Map.Entry entry = (Map.Entry) iterator.next();
			System.out.println("TimeStamp: " + entry.getKey() + " SeqNum: " + entry.getValue());
		}
	}
	
}

@@ -424,59 +424,297 @@
 e;%0D%0A
-%0D%0Apublic class DelayLossSimulator %7B%0D%0A%0D%0A%09public stat
+import br.ufrj.cos.redes.receiver.Buffer;%0D%0A%0D%0Apublic class DelayLossSimulator %7B%0D%0A%09%0D%0A%09private double F;%0D%0A%09private double LAMBDA;%0D%0A%09private double RTT;%0D%0A%0D%0A%09public DelayLossSimulator(double F, double LAMBDA, double RTT) %7B%0D%0A%09%09this.F = F;%0D%0A%09%09this.LAMBDA = LAMBDA;%0D%0A%09%09this.RTT = RTT;%0D%0A%09%7D%0D%0A%0D%0A%09publ
 ic v
@@ -752,16 +752,31 @@
 ntSocket
+, Buffer buffer
 ) throws
@@ -821,78 +821,8 @@
 %09%09%0D%0A
-%09%09double fail = 0.1;%0D%0A%09%09double LAMBDA = 50;%0D%0A%09%09double RTT = 600;%0D%0A%09%09%0D%0A
 %09%09Ma
@@ -946,57 +946,8 @@
 %09%09%0D%0A
-%09%09%09%09System.out.println(%22Waiting for chunk...%22);%0D%0A
 %09%09%09%09
@@ -1735,12 +1735,9 @@
 ) %3C 
-fail
+F
 ) ? 

change static mehtod to instance method