package br.ufrj.cos.redes.receiver;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import br.ufrj.cos.redes.delayLossSimulator.DelayLossSimulator;

public class ReceiverBufferPlayerExample {
	public static void main(String[] args) {
		boolean isSender = (args.length == 3);
	
		int B = !isSender ? Integer.parseInt(args[2]) : 10;
		double F = !isSender ? Double.parseDouble(args[3]) : 0.0;
		int B_WINDOW = B + (int) Math.ceil(F*B);
		
		double LAMBDA = 5;
		long RTT = !isSender ? Long.parseLong(args[4]) : Long.parseLong(args[0]);
		
//		String REQUESTED_FILE_NAME = "C:\\Users\\Joao&Duda\\Desktop\\JP\\2015-1\\redes\\testinput.txt";
//		String RECEIVED_FILE_NAME = "C:\\Users\\Joao&Duda\\Desktop\\JP\\2015-1\\redes\\testinputrecv.txt";
//		String LOG_FILE_NAME = "C:\\Users\\Joao&Duda\\Desktop\\JP\\2015-1\\redes\\projetoLog.txt";

		String REQUESTED_FILE_NAME = !isSender ? args[0] : "C:\\Users\\Joao&Duda\\Desktop\\JP\\2015-1\\redes\\testinput.txt";
		String RECEIVED_FILE_NAME = !isSender ? args[1] : "C:\\Users\\Joao&Duda\\Desktop\\JP\\2015-1\\redes\\testinputrecv.txt";
		String TIMESTAMP_LOG_FILE_NAME = !isSender ? args[5] : args[1];
		String LATENCY_LOG_FILE_NAME = !isSender ? args[6] : args[2];
		
		InetAddress SERVER_ADDRESS = InetAddress.getLoopbackAddress();
		int SERVER_PORT = 29920;
		
		Buffer buffer = new Buffer(B, B_WINDOW);
		DelayLossSimulator simulator = new DelayLossSimulator(F, LAMBDA, RTT, buffer);
		Player player = null;
		try {
			player = new Player(new FileOutputStream(RECEIVED_FILE_NAME),
								new FileOutputStream(TIMESTAMP_LOG_FILE_NAME),
								new FileOutputStream(LATENCY_LOG_FILE_NAME));
		} catch (FileNotFoundException e) {
			System.out.println("Error in player, FileNotFoundException"); 
			e.printStackTrace();
		}		
		DatagramSocket clientSocket = null;
		try {
			clientSocket = new DatagramSocket();
		} catch (SocketException e) {
			System.out.println("Error in clientSocket, SocketException"); 
			e.printStackTrace();
		}
		Receiver receiver = new Receiver(buffer, REQUESTED_FILE_NAME, simulator, player);
		try {
			receiver.startReceiver(clientSocket, SERVER_ADDRESS, SERVER_PORT);
		} catch (IOException e) {
			System.out.println("Error in receiver, IOException"); 
			e.printStackTrace();
		}
	}
}

@@ -377,73 +377,19 @@
 s) %7B
-%0A%09%09boolean isSender = (args.length == 3);%0A%09%0A%09%09int B = !isSender ?
+%09%0A%09%09int B =
  Int
@@ -414,13 +414,8 @@
 %5B2%5D)
- : 10
 ;%0A%09%09
@@ -424,28 +424,16 @@
 uble F =
- !isSender ?
  Double.
@@ -456,14 +456,8 @@
 %5B3%5D)
- : 0.0
 ;%0A%09%09
@@ -533,28 +533,16 @@
 ng RTT =
- !isSender ?
  Long.pa
@@ -561,41 +561,13 @@
 %5B4%5D)
- : Long.parseLong(args%5B0%5D)
 ;%0A%09%09%0A
-//
 %09%09St
@@ -597,515 +597,54 @@
 E = 
-%22C:%5C%5CUsers%5C%5CJoao&Duda%5C%5CDesktop%5C%5CJP%5C%5C2015-1%5C%5Credes%5C%5Ctestinput.txt%22;%0A//%09%09String RECEIVED_FILE_NAME = %22C:%5C%5CUsers%5C%5CJoao&Duda%5C%5CDesktop%5C%5CJP%5C%5C2015-1%5C%5Credes%5C%5Ctestinputrecv.txt%22;%0A//%09%09String LOG_FILE_NAME = %22C:%5C%5CUsers%5C%5CJoao&Duda%5C%5CDesktop%5C%5CJP%5C%5C2015-1%5C%5Credes%5C%5CprojetoLog.txt%22;%0A%0A%09%09String REQUESTED_FILE_NAME = !isSender ? args%5B0%5D : %22C:%5C%5CUsers%5C%5CJoao&Duda%5C%5CDesktop%5C%5CJP%5C%5C2015-1%5C%5Credes%5C%5Ctestinput.txt%22;%0A%09%09String RECEIVED_FILE_NAME = !isSender ? args%5B1%5D : %22C:%5C%5CUsers%5C%5CJoao&Duda%5C%5CDesktop%5C%5CJP%5C%5C2015-1%5C%5Credes%5C%5Ctestinputrecv.txt%22
+args%5B0%5D;%0A%09%09String RECEIVED_FILE_NAME = args%5B1%5D
 ;%0A%09%09
@@ -680,36 +680,14 @@
 E = 
-!isSender ? args%5B5%5D : args%5B1
+args%5B5
 %5D;%0A%09
@@ -722,36 +722,14 @@
 E = 
-!isSender ? args%5B6%5D : args%5B2
+args%5B6
 %5D;%0A%09

no longer need the variable isSender