TD2: Fix the remaining bugs in the IHM chat client.
[TD_SR.git] / TD2 / server / BroadcastThreadService.java
index daf753f665ab16d9e798dcbc45428ce2b0732774..9ea5cdd5f90af52d4ee7ba2b23286f2bc61512f4 100644 (file)
@@ -19,7 +19,8 @@ public class BroadcastThreadService implements Runnable {
                try {
                        doService(clientSocket, listWriter);
                        clientSocket.close();
-               } catch (IOException e) {
+               }
+               catch (IOException e) {
                        System.err.println("IOException : " + e);
                        e.printStackTrace();
                }
@@ -27,32 +28,40 @@ public class BroadcastThreadService implements Runnable {
                        try {
                                if (this.clientSocket != null)
                                        this.clientSocket.close();
-                       } catch (IOException e) {
+                       }
+                       catch (IOException e) {
                                System.err.println("IOException : " + e);
                                e.printStackTrace();
                        }
                }
        }
 
+       /**
+        * Be careful, this function must be thread-safe
+        * @param  clientSocket [description]
+        * @param  sharedList   [description]
+        * @throws IOException  [description]
+        */
        public void doService(Socket clientSocket, ArrayList<PrintWriter> sharedList) throws IOException {
+               PrintWriter OWriter = new PrintWriter(clientSocket.getOutputStream());
                BufferedReader in;
                in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
-               PrintWriter OWriter = new PrintWriter(clientSocket.getOutputStream());
                sharedList.add(OWriter);
                boolean end = false;
                while (!end) {
                        String theLine = in.readLine();
                        if (theLine.equals(".")) {
                                end = true; // le thread de service doit terminer
-                               break; // do not broadcast the dot that will close clients threads
+                               break; // do not broadcast the dot that will close all clients threads
                        }
+                       System.out.println("Broadcasting the message \"" + theLine + "\" received from " + clientSocket.toString());
                        broadcastMsg(theLine);
                }
                sharedList.remove(OWriter);
-               if (in != null)
-                       in.close();
                if (OWriter != null)
                        OWriter.close();
+               if (in != null)
+                       in.close();
                System.out.println("Fin du thread repondant au client, port "
                                                        + clientSocket.getPort());
        }