package ServerMainPackage;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import Constants.ServerConstants;

public class ServerDispatcher implements Runnable {
	
	private static ServerDispatcher serverDispatcher = new ServerDispatcher();
	private static List<MatchConnection> activeConnections =
			Collections.synchronizedList(new LinkedList<MatchConnection>());
	
	private ServerDispatcher() {}
	
	public ServerDispatcher getInstance() {
		return serverDispatcher;
	}
	
	@Override
	public void run() {
		Timer connectionCleaner = new Timer();
		TimerTask removeInactiveConnections = new TimerTask() {
			@Override
			public void run() {
				for (int i = 0; i < activeConnections.size(); i++)
					if (!activeConnections.get(i).isActive()) {
						activeConnections.remove(i);
						i--;
					}
			}
		};
		connectionCleaner.scheduleAtFixedRate(removeInactiveConnections, 0,
				ServerConstants.PACKET_DELAY * 2);
		
		try (ServerSocket serverSocket = new ServerSocket(ServerConstants.PORT)) {
			Socket clientSocket = serverSocket.accept();
			MatchConnection clientConnection = new MatchConnection(clientSocket);
			activeConnections.add(clientConnection);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

@@ -65,48 +65,157 @@
 ava.
-net.ServerSocket;%0Aimport java.net.Socket
+io.ObjectInputStream;%0Aimport java.io.ObjectOutputStream;%0Aimport java.net.ServerSocket;%0Aimport java.net.Socket;%0Aimport java.net.SocketTimeoutException
 ;%0Aim
@@ -521,23 +521,16 @@
 private 
-static 
 List%3CMat
@@ -631,16 +631,52 @@
 on%3E());%0A
+%09private boolean isRunning = false;%0A
 %09%0A%09priva
@@ -775,16 +775,296 @@
 r;%0A%09%7D%0A%09%0A
+%09public boolean start() %7B%0A%09%09if (!isRunning) %7B%0A%09%09%09new Thread(this).start();%0A%09%09%09isRunning = true;%0A%09%09%09return true;%0A%09%09%7D%0A%09%09%0A%09%09return false;%0A%09%7D%0A%09%0A%09public boolean stop() %7B%0A%09%09if (isRunning) %7B%0A%09%09%09isRunning = false;%0A%09%09%09activeConnections.clear();%0A%09%09%09return true;%0A%09%09%7D%0A%09%09%0A%09%09return false;%0A%09%7D%0A%09%0A
 %09@Overri
@@ -1501,16 +1501,66 @@
  2);%0A%09%09%0A
+%09%09Object object = null;%0A%09%09%0A%09%09while (isRunning) %7B%0A%09
 %09%09try (S
@@ -1631,16 +1631,17 @@
 )) %7B%0A%09%09%09
+%09
 Socket c
@@ -1684,169 +1684,1079 @@
 %0A%09%09%09
-MatchConnection clientConnection = new MatchConnection(clientSocket);%0A%09%09%09activeConnections.add(clientConnection);%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09e.printStackTrac
+%09clientSocket.setSoTimeout(2000);%0A%09%09%09%09%0A%09%09%09%09ObjectInputStream objectInputStream = new ObjectInputStream(clientSocket.getInputStream());%0A%09%09%09%09object = objectInputStream.readObject();%0A%09%09%09%09if (object instanceof ClientRequest) %0A%09%09%09%09%09processRequest(clientSocket, (ClientRequest)object);%0A%09%09%09%7D catch (SocketTimeoutException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D catch (ClassNotFoundException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%7D%0A%09%0A%09private void processRequest(Socket clientSocket, ClientRequest clientRequest) throws IOException %7B%0A%09%09if (clientRequest.getType() == RequestType.HostMatch) %7B%0A%09%09%09clientSocket.setSoTimeout(0);%0A%09%09%09MatchConnection clientConnection = new MatchConnection(clientSocket);%0A%09%09%09activeConnections.add(clientConnection);%0A%09%09%7D else if (clientRequest.getType() == RequestType.UpdateServerList) %7B%0A%09%09%09ObjectOutputStream objectOutputStream = %0A%09%09%09%09%09new ObjectOutputStream(clientSocket.getOutputStream());%0A%09%09%09objectOutputStream.writeObject(activeConnections);%0A%09%09%09objectOutputStream.flush();%0A%09%09%09clientSocket.clos
 e();

Enhance ServerDispatcher and make use of client request system