@@ -0,0 +1,3021 @@
+package pl.edu.elka.raj;%0A%0Aimport pl.edu.elka.models.Node;%0Aimport pl.edu.elka.util.Log;%0A%0Aimport java.io.DataOutputStream;%0Aimport java.io.IOException;%0Aimport java.net.ServerSocket;%0Aimport java.net.Socket;%0A%0A/**%0A * Simple TCP server used to control the robot remotely. Allows multiple and concurrent clients.%0A *%0A * @author Gregorio%0A */%0Apublic class TCPServer implements Runnable %7B%0A    private ServerSocket socket;%0A    private boolean active = true;%0A%0A%0A    public boolean isActive() %7B%0A        return active;%0A    %7D%0A%0A    public void setActive(boolean active) %7B%0A        this.active = active;%0A    %7D%0A%0A    public TCPServer(int port) %7B%0A        try %7B%0A            socket = new ServerSocket(port);%0A        %7D catch (Exception e) %7B%0A            Log.LogError(Log.SUBTYPE.SYSTEM, %22Error creating server: %22 + e.getMessage());%0A        %7D%0A    %7D%0A%0A    /**%0A     * Writes data to all clients, checking if they're still alive.%0A     *%0A     * @param data%0A     */%0A    public void write(String data) %7B%0A        for (Node client : NetworkController.clients.values()) %7B%0A            if (client.getSocket().isClosed()) %7B%0A                NetworkController.clients.remove(client.getPid());%0A                continue;%0A            %7D%0A            try %7B%0A                DataOutputStream outToClient = new DataOutputStream(client.getSocket().getOutputStream());%0A                outToClient.writeBytes(data);%0A            %7D catch (IOException e) %7B%0A                NetworkController.clients.remove(client.getPid());%0A                client.close();%0A                Log.LogError(Log.SUBTYPE.SYSTEM, %22Error writing to clients: %22 + e.getMessage());%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * Closes the server%0A     */%0A    public void close() %7B%0A        try %7B%0A            socket.close();%0A            Log.LogError(Log.SUBTYPE.SYSTEM, %22TCP server shutting down%22);%0A        %7D catch (IOException e) %7B%0A            Log.LogError(Log.SUBTYPE.SYSTEM, %22Error stopping server: %22 + e.getMessage());%0A        %7D%0A    %7D%0A%0A    /*%0A     * Implementation of the run method that listens for connections and launches a worker thread for%0A     * each client.%0A     *%0A     * @see java.lang.Runnable#run()%0A     */%0A%0A    public void run() %7B%0A        Log.LogEvent(Log.SUBTYPE.SYSTEM, %22Server starting%22);%0A        while (active) %7B%0A            Socket clientSocket = null;%0A            try %7B%0A                clientSocket = socket.accept();%0A                Node newClient = new Node(null, clientSocket, false);%0A                Thread workerThread = new Thread(new TCPClientWorker(newClient));%0A                workerThread.start();%0A                Log.LogEvent(Log.SUBTYPE.SYSTEM, %22Client connected to server%22);%0A                for (Node client : NetworkController.clients.values()) %7B%0A                    if (client.getSocket().isClosed()) %7B%0A                        NetworkController.clients.remove(client.getPid());%0A                    %7D%0A                %7D%0A            %7D catch (IOException e) %7B%0A                Log.LogError(Log.SUBTYPE.SYSTEM, %22Connection error: %22 + e.getMessage());%0A            %7D%0A        %7D%0A    %7D%0A%7D%0A

Create TCPServer.java