X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TD2%2Fserver%2FBroadcastThreadService.java;h=fd05e176447fa230421792430274ec96fcecc80d;hb=c4d2497c0d03e16d57bc40f26124a5d0ef4a5b75;hp=180f735b49fa442ed5982afa0a286d8c3f0ff879;hpb=b9a33c97f77a1eae45b91426ca8061e18f163d4e;p=TD_SR.git diff --git a/TD2/server/BroadcastThreadService.java b/TD2/server/BroadcastThreadService.java index 180f735..fd05e17 100644 --- a/TD2/server/BroadcastThreadService.java +++ b/TD2/server/BroadcastThreadService.java @@ -18,10 +18,9 @@ public class BroadcastThreadService implements Runnable { public void run() { try { doService(clientSocket, listWriter); - //FIXME: also close the BR and PW? clientSocket.close(); - //FIXME: remove the associated PW from the ArrayList - } catch (IOException e) { + } + catch (IOException e) { System.err.println("IOException : " + e); e.printStackTrace(); } @@ -29,24 +28,39 @@ 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 sharedList) throws IOException { + PrintWriter OWriter = new PrintWriter(clientSocket.getOutputStream()); BufferedReader in; in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); - sharedList.add(new PrintWriter(clientSocket.getOutputStream())); + sharedList.add(OWriter); boolean end = false; while (!end) { String theLine = in.readLine(); - if (theLine.equals(".")) + if (theLine.equals(".")) { end = true; // le thread de service doit terminer + break; // do not broadcast the dot that will close all clients threads + } broadcastMsg(theLine); } + sharedList.remove(OWriter); + if (OWriter != null) + OWriter.close(); + if (in != null) + in.close(); System.out.println("Fin du thread repondant au client, port " + clientSocket.getPort()); }