package com.github.versatilevelociraptor.safevelociraptorserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;


public class Server {
	public static final int PORT = 2585;
	private ServerSocket serverSocket;
	private Controller controller;
	private RobotController roboController;
	public static final int MAX_CLIENTS = 2;
	public Server() {
		try {
			serverSocket = new ServerSocket(PORT);
			serverSocket.setReuseAddress(true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		initializeConnections();
	}
	
	public void initializeConnections() {
		Socket client;
		int connectedClients = 0;
		boolean controlerConnected = false , robotControllerConnected = false;
		while(connectedClients < MAX_CLIENTS) {
			try {
				System.out.println("Looking for clients..");
				client = serverSocket.accept();
				System.out.println("Loocking for clients");
				BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
				String ID = reader.readLine();
				System.out.println(ID);
				if(Integer.parseInt(ID) == Controller.ID && !controlerConnected) {
					controller = new Controller(client);
					System.out.println("Client Connected!");
					connectedClients++;
					controlerConnected = true;
				} else if(Integer.parseInt(ID) == RobotController.ID && !robotControllerConnected) {
					roboController = new RobotController(client);
					System.out.println("Client Connected!");
					connectedClients++;
					robotControllerConnected = true;
				} else {
					System.out.println("NOT CORRECT ID");
				}
			} catch (IOException e) {
				e.printStackTrace();
			} 
		}
	}
	
	public void restart() {
		initializeConnections();
		createCommunicationSession();
	}
	
	public void createCommunicationSession() {
		new Thread(new ServerThread(roboController, controller)).start();
		new Thread(new ServerThread(controller, roboController)).start();
	}

	public static void main(String[] args) {
		Server server = new Server();
		server.createCommunicationSession();
	}
	
	private class ServerThread implements Runnable{
		private Client send;
		private Client recieve;
		
		public ServerThread(Client send, Client recieve) {
			this.send = send;
			this.recieve = recieve;
		}
		
		/* (non-Javadoc)
		 * @see java.lang.Runnable#run()
		 */
		@Override
		public void run() {
			recieve.sendCommand(send.recieveCommand());
		}	
	}
}

@@ -1123,38 +1123,29 @@
 %09%09%09%09if(I
-nteger.parseInt(ID) ==
+D.equals(%22%22 +
  Control
@@ -1146,24 +1146,25 @@
 ontroller.ID
+)
  && !control
@@ -1212,32 +1212,83 @@
 roller(client);%0A
+%09%09%09%09%09controller.sendCommand(%22Michael is a gypsy%22);%0A
 %09%09%09%09%09System.out.
@@ -1393,30 +1393,21 @@
 if(I
-nteger.parseInt(ID) ==
+D.equals(%22%22 +
  Rob
@@ -1421,16 +1421,17 @@
 oller.ID
+)
  && !rob
@@ -1544,32 +1544,87 @@
 t Connected!%22);%0A
+%09%09%09%09%09roboController.sendCommand(%22Michael is a gypsy%22);%0A
 %09%09%09%09%09connectedCl
@@ -1882,32 +1882,84 @@
 Session();%0A%09%7D%0A%09%0A
+%09public synchronized void setController() %7B%0A%09%09%0A%09%7D%0A%09%0A
 %09public void cre

Remove parse ints