package net.bourgau.philippe.concurrency.kata;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class TcpChatRoomServer extends SafeRunnable implements AutoCloseable {

    private final InProcessChatRoom chatRoom = new InProcessChatRoom();
    private final ExecutorService threadPool = Executors.newCachedThreadPool();
    private final ServerSocket serverSocket;

    private TcpChatRoomServer(int port) throws Exception {
        serverSocket = new ServerSocket(port);
        threadPool.submit(this);
    }

    @Override
    protected void unsafeRun() throws Exception {
        Socket socket = serverSocket.accept();
        TcpClientProxy clientProxy = new TcpClientProxy(socket, chatRoom);
        chatRoom.enter(clientProxy);
        threadPool.submit(clientProxy);
    }

    public static TcpChatRoomServer start(int port) throws Exception {
        return new TcpChatRoomServer(port);
    }

    @Override
    public void close() throws Exception {
        serverSocket.close();
        threadPool.shutdown();
        threadPool.shutdownNow();
        if (!threadPool.awaitTermination(1, TimeUnit.SECONDS)) {
            System.err.println("Failed to stop all server threads");
        }
    }
}

@@ -698,32 +698,76 @@
 ows Exception %7B%0A
+        while (!Thread.interrupted()) %7B%0A    
         Socket s
@@ -793,24 +793,28 @@
 t.accept();%0A
+    
         TcpC
@@ -884,16 +884,20 @@
         
+  
+  
 chatRoom
@@ -913,24 +913,28 @@
 ientProxy);%0A
+    
         thre
@@ -957,24 +957,34 @@
 ientProxy);%0A
+        %7D%0A
     %7D%0A%0A    p

Handle many clients