package test;

import java.util.Vector;

import org.apache.log4j.Logger;

import cz.vutbr.feec.burget.Receiver;
import cz.vutbr.feec.simlib.net.InetAddress;
import cz.vutbr.feec.simlib.Simulation;
import cz.vutbr.feec.sim.ha.FTHostState;
import cz.vutbr.feec.sim.ha.FTHost;
import cz.vutbr.feec.sim.ha.FTMHost;
import cz.vutbr.feec.sim.ha.FeedbackOutFT;
import cz.vutbr.feec.sim.ha.ListOfActions;

public class Main {

	/**
	 * @param args
	 */
	private static Logger logger = Logger.getLogger(Main.class.getName());

	public static void main(String[] args) {
		
				
		
			int a =0;
		
			final int interval_PERIOD = 5000; 
			
			Simulation sim = new Simulation();
			
			Vector<FTHost> ftHostVector = new Vector<FTHost>();
			
			Vector<InetAddress> address = new Vector<InetAddress>();
			//ip address of RFT
			InetAddress addrRFT = InetAddress.getUniqueAddress();
			//ip address of Multicast
			InetAddress addrMulti = InetAddress.getUniqueAddress();
			//ip address of Sender
			InetAddress addrS = InetAddress.getUniqueAddress();
			//ip address of FTM
			InetAddress addrFTM = InetAddress.getUniqueAddress();
			address.add(addrRFT);
			address.add(addrMulti);
			address.add(addrS);
			address.add(addrFTM);
			
			
			
			// Root feedback target
			FTHostState stateRFT = new FTHostState(addrRFT,1,2,6001,addrMulti,6002,
					10,addrS,0,6001,6000,interval_PERIOD,0);

			
			FTHost rft = new FTHost(stateRFT);
			
			// FTM
			FTHostState stateFTM = new FTHostState(addrFTM,1,99,6001,addrMulti,6002,
					15000,addrS,99,6001,6000,interval_PERIOD,99);
			
			FTHost ftm = new FTHost(stateFTM);
			
			
			ftHostVector.add(ftm);
			ftHostVector.add(rft);
			sim.addProcess(ftm, 0);
			sim.addProcess(rft, 0);

			//FT
			for (int i = 0; i < 10; i++) {
				//ip address of FT
				InetAddress addrFT = InetAddress.getUniqueAddress();
				address.add(addrFT);
				//state of each FT
				FTHostState stateFT = new FTHostState(addrFT, 1, 1, 6001, addrMulti, 6002, 
						100, addrRFT, i+1,6001,6000,interval_PERIOD,0);
				
				FTHost ft = new FTHost(stateFT);
				ftHostVector.add(ft);
				sim.addProcess(ft, 0);
				
			}
			
			rft.setFTgroup(ftHostVector);
			/*Definujeme listAkci , kter0 budou platn0 pro vstupni vektor funkce*/
			ListOfActions listOfAct = new ListOfActions(5000,ftHostVector);
			sim.addProcess(listOfAct, 0);
			
			
			System.out.println(a++);
			
			sim.start(100000);
		
			System.out.println(a++);
			
			int numRFT_FTD = rft.getSessionState().getNumberOfFTDreceived();
				
			int numRFT_FTI = rft.getSessionState().getNumberOfFTIreceived();
					
			int numRFT_FTS = rft.getSessionState().getNumberOfFTSreceived();
			
			//zjisteni jake zpravy byly poslany nebo prijaty
			for (FTHost r : ftHostVector) {
				
				logger.info("FT:"+r.getSessionState().getID()+" poslal:" + 
						r.getSessionState().getNumberOfFTDsent()+ " prijal:"+r.getSessionState()
						.getNumberOfFTDreceived() +" FTD paketu");
				
				numRFT_FTD = numRFT_FTD - r.getSessionState().getNumberOfFTDsent();
				r.getSessionState().clearNumberofFTDsend();
				
				logger.info("FT:"+r.getSessionState().getID()+" poslal:" + 
						r.getSessionState().getNumberOfFTIsent()+ " prijal:"+r.getSessionState()
						.getNumberOfFTIreceived() +" FTI paketu");
				
				numRFT_FTI = numRFT_FTI - r.getSessionState().getNumberOfFTIsent();
				r.getSessionState().clearNumberofFTIsend();
				
				logger.info("FT:"+r.getSessionState().getID()+" poslal:" + 
						r.getSessionState().getNumberOfFTSsent()+ " prijal:"+r.getSessionState()
						.getNumberOfFTSreceived() +" FTS paketu");
				
				numRFT_FTS = numRFT_FTS - r.getSessionState().getNumberOfFTSsent();
				r.getSessionState().clearNumberofFTSsend();
			}
			rft.getSessionState().clearNumberofFTDreceived();
			rft.getSessionState().clearNumberofFTIreceived();
			rft.getSessionState().clearNumberofFTSreceived();
			
			System.out.println("Rozdil FTD je:"+ numRFT_FTD);
			System.out.println("Rozdil FTI je:"+ numRFT_FTI);
			System.out.println("Rozdil FTS je:"+ numRFT_FTS);
			
		}

}
