package adaptive;

import java.io.*;
import java.net.*;

public class Sender {

	public static void main(String argv[]) throws Exception {
		DatagramSocket socket = new DatagramSocket(6787);
		DatagramPacket packetBig, packetSmall;
		byte[] bufSmall = new byte[1000];
		byte[] bufBig = new byte[2000];

		SenderMonitor monitor = new SenderMonitor();
		monitor.start();

		InetAddress receiverAddress = InetAddress.getByName(argv[0]);

		packetSmall =
			new DatagramPacket(
				bufSmall,
				bufSmall.length,
				receiverAddress,
				6787);
		packetBig =
			new DatagramPacket(bufBig, bufBig.length, receiverAddress, 6787);

		int timeToSleep = (new Integer(argv[1])).intValue();

		long last = System.currentTimeMillis();
		long current = 0;
		
		int auxLastStatus = -1;
		long lastChange = System.currentTimeMillis();
		while (true) {
			try {
				int status = monitor.getStatus();
				if (auxLastStatus != status){
					long change = System.currentTimeMillis();
					//Deixou de enviar
					float economia = change-lastChange;
					economia /= 1000;
					if (auxLastStatus == 2){
						System.out.println("Deixei de mandar: " + economia);
					}
					else if (auxLastStatus == 3){
						System.out.println("Deixei de mandar: " + economia*2);
					}
//					System.out.println(System.currentTimeMillis() + " " + status);
					System.out.println("Alterei: " + change + " " + status);
					auxLastStatus = status;
					lastChange = change;
				}
				if (status == 0) {
					do {
						current = System.currentTimeMillis();
					} while ((last + timeToSleep) > current);
					socket.send(packetBig);
				}
				else if (status == 1){
					do {
						current = System.currentTimeMillis();
					} while ((last + (timeToSleep / 2)) > current);
					socket.send(packetSmall);
				}
				else if (status == 2){
					do {
						current = System.currentTimeMillis();
					} while ((last + timeToSleep) > current);
					socket.send(packetSmall);
				}
				else if  (status == 3){
					Thread.sleep(300);
				}
				last = current;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main2(String argv[]) throws Exception {
		DatagramSocket socket = new DatagramSocket(6787);
		DatagramPacket packetBig, packetSmall;
		byte[] bufSmall = new byte[1000];
		byte[] bufBig = new byte[2000];
	
		SenderMonitor monitor = new SenderMonitor();
		monitor.start();
	
		InetAddress receiverAddress = InetAddress.getByName(argv[0]);
	
		packetSmall =
			new DatagramPacket(
				bufSmall,
				bufSmall.length,
				receiverAddress,
				6787);
		packetBig =
			new DatagramPacket(bufBig, bufBig.length, receiverAddress, 6787);
	
		int timeToSleep = (new Integer(argv[1])).intValue();
	
		long last = System.currentTimeMillis();
		long current = 0;
		
		int auxLastStatus = -1;
		while (true) {
			try {
				int status = monitor.getStatus();
				if (auxLastStatus != status){ 
					System.out.println(System.currentTimeMillis() + " " + status);
					auxLastStatus = status;
				}
				if (status == 0) {
					socket.send(packetBig);
					Thread.sleep(timeToSleep);
				}
				else if (status == 1){
					socket.send(packetSmall);
					Thread.sleep(timeToSleep/2);
				}
				else if (status == 2){
					socket.send(packetSmall);
					Thread.sleep(timeToSleep);
				}
				else if  (status == 3){
					Thread.sleep(300);
				}
				last = current;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
