package cz.vutbr.feec.sim.ha;



import org.apache.log4j.Logger;
import cz.vutbr.feec.packets.ttp.*;


import cz.vutbr.feec.packets.PacketGenerateException;

import cz.vutbr.feec.simlib.net.DatagramPacket;
import cz.vutbr.feec.simlib.net.InetAddress;
import cz.vutbr.feec.simlib.net.NetProc;
import cz.vutbr.feec.simlib.net.Socket;

import cz.vutbr.feec.sim.ha.FTHostState;

public class FeedbackOutFT extends NetProc {
	private static Logger logger = Logger.getLogger(FeedbackOutFT.class
			.getName());
	
	private final Socket socket;

	
	private FTHostState sessionState;

	public FeedbackOutFT(FTHostState sessionState, Socket localSocket) {
		super(localSocket.getAddr());
		
		this.socket = localSocket;
		
		setName("FeedbackOutFT-Thr");
		this.sessionState = sessionState;
	}

	@Override
	public void action() {
		final int MAX_LENGTH = 1500;
		byte[] buffer = new byte[MAX_LENGTH];
		
		
		while (!getSimulation().isFinished()) {

		sleepProc(100);

			
			if (sessionState.isActive()) {
				
				final int portToSend = (int)sessionState.getPortInOfNextFT();
				final InetAddress sendAddr = sessionState.getSendIPAddr();
				
				try { //nejprve zjisti jaky paket se bude posílat
						if (sessionState.readyToSendFTD()) {
							
							FTDPacket packetFTD = sessionState.getFTDpacket();
							
							DatagramPacket pktFTD = new DatagramPacket(buffer, packetFTD.getLength(), 
									sendAddr, portToSend);
							
							int lengthFTD = packetFTD.generate(buffer, 0);
							pktFTD.setLength(lengthFTD);
							assert sendAddr != null;
							socket.send(pktFTD, sendAddr, portToSend);
							
							sessionState.addFTDsend();
							sessionState.statOfSendFTD(false);
							
							logger.debug("SEND FTD MESSAGE: t=" + getSimulation().getTime()
									+ " length:" + lengthFTD + " TO:" + socket.getAddr());
							
						}
						
						if (sessionState.readyToSendFTI()) {
							
							FTIPacket packetFTI = sessionState.getFTIpacket();
							
							DatagramPacket pktFTI = new DatagramPacket(buffer, packetFTI.getLength(), 
									sendAddr, portToSend);
							
							int lengthFTI = packetFTI.generate(buffer, 0);
							pktFTI.setLength(lengthFTI);
							assert sendAddr != null;
							socket.send(pktFTI, sendAddr, portToSend);
							
							sessionState.addFTIsend();
							sessionState.statOfSendFTI(false);
							
							logger.debug("SEND FTI MESSAGE: t=" + getSimulation().getTime()
									+ " length:" + lengthFTI + " TO:" + socket.getAddr());
							
						}
						
						if (sessionState.readyToSendFTS()) {
							
							FTSPacket packetFTS = sessionState.getFTSpacket();
							
							DatagramPacket pktFTS = new DatagramPacket(buffer, packetFTS.getLength(), 
									sendAddr, portToSend);
							
							int lengthFTS = packetFTS.generate(buffer, 0);
							pktFTS.setLength(lengthFTS);
							assert sendAddr != null;
							socket.send(pktFTS, sendAddr, portToSend);
							
							sessionState.addFTSsend();
							sessionState.statOfSendFTS(false);
							
							logger.debug("SEND FTS MESSAGE: t=" + getSimulation().getTime()
									+ " length:" + lengthFTS + " TO:" + socket.getAddr());
							
						}
	

				} catch (PacketGenerateException e) {
				} 
				
			}
		}
		logger.debug("Sending finished.");
		System.out.println("Sending finished.");
	}

	
}
