package server;

import server.Packet.AppPacket;
import server.Packet.LeaderPacket;

import java.io.IOException;
import java.util.Random;

/**
 * Created by zsabin on 4/28/16.
 */
public class TimeoutThread implements Runnable
{
    public static final int MIN_TIMEOUT = 2500;
    public static final int MAX_TIMEOUT = 3000;
    private final MulticastServer server;

    public TimeoutThread(MulticastServer server)
    {
        this.server = server;
    }

    @Override
    public void run()
    {
        while (!server.isLeader() && !server.getDebugKill())
        {
            server.resetTimeout();
            long timeLeft;
            do {
                timeLeft = System.currentTimeMillis() - server.getStartTime();
                if(server.isLeader() || server.getDebugKill()){
                    server.consoleMessage("Killing Timeout Thread", 2);
                    return;
                }
            }
            while (timeLeft < server.getTimeout());

            server.consoleMessage("Server timed out", 2);
            if (server.isLeader()) {return; }
            server.changeServerState(MulticastServer.ServerState.CANIDATE);

            //start election
            server.consoleMessage("Sending Vote Requests", 2);
            AppPacket voteRequest = new AppPacket(server.getId(), AppPacket.PacketType.VOTE_REQUEST, server.getLeaderId(),
                    server.getTerm(), -1, LeaderPacket.getNextSequenceNumber(), -1,AppPacket.PacketType.VOTE_REQUEST.ordinal(), "");

            try
            {
                server.getMulticastSocket().send(voteRequest.getDatagram(server.getGroup(), server.getPort()));
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        server.consoleMessage("Killing Timeout Thread", 2);
    }
}

@@ -161,19 +161,111 @@
 bin 
-on 4/28/16.
+and adavis20 on 4/28/16.%0A * This thread is used to control the timeout for FOLLOWERS and CANDIDATES%0A * 
 %0A */
@@ -452,16 +452,20 @@
 server;%0A
+    
 %0A    pub
@@ -603,70 +603,312 @@
     
-while (!server.isLeader() && !server.getDebugKill())%0A        %7B
+//This while loop checks to see if the current server is not A LEADER (Leaders do not need timeout threads)%0A        // Also checks to see if the sever is currently %22Alive%22%0A        while (!server.isLeader() && !server.getDebugKill())%0A        %7B%0A            //This initializes the first timeOut for A server
 %0A   

Update TimeoutThread.java