X-Git-Url: https://git.piment-noir.org/?p=TD_SR.git;a=blobdiff_plain;f=TD2%2Fserver%2FBroadcastThreadService.java;fp=TD2%2Fserver%2FEchoServerThreadService.java;h=180f735b49fa442ed5982afa0a286d8c3f0ff879;hp=257f15c4d654dd587b82720d9d59189fb5ef4a1b;hb=b9a33c97f77a1eae45b91426ca8061e18f163d4e;hpb=b1bd144dcdf623152885186e165a39ddd65afd88 diff --git a/TD2/server/EchoServerThreadService.java b/TD2/server/BroadcastThreadService.java similarity index 53% rename from TD2/server/EchoServerThreadService.java rename to TD2/server/BroadcastThreadService.java index 257f15c..180f735 100644 --- a/TD2/server/EchoServerThreadService.java +++ b/TD2/server/BroadcastThreadService.java @@ -2,20 +2,25 @@ import java.net.*; import java.io.*; import java.util.*; -public class EchoServerThreadService implements Runnable { +public class BroadcastThreadService implements Runnable { private Socket clientSocket; - private ArrayList listWriter; + private static ArrayList listWriter; - EchoServerThreadService(Socket clientSocket) { - System.out.println("Creation d'un thread pour repondre a un client, port " + clientSocket.getPort()); + BroadcastThreadService(Socket clientSocket) { + System.out.println("Creation d'un thread pour repondre a un client, port " + + clientSocket.getPort()); this.clientSocket = clientSocket; + if (listWriter == null) + listWriter = new ArrayList(); } public void run() { try { - doService(clientSocket); + doService(clientSocket, listWriter); + //FIXME: also close the BR and PW? clientSocket.close(); + //FIXME: remove the associated PW from the ArrayList } catch (IOException e) { System.err.println("IOException : " + e); e.printStackTrace(); @@ -31,28 +36,28 @@ public class EchoServerThreadService implements Runnable { } } - public void doService(Socket clientSocket) throws IOException { + public void doService(Socket clientSocket, ArrayList sharedList) throws IOException { BufferedReader in; - PrintStream out; in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); - out = new PrintStream(clientSocket.getOutputStream()); - //listWriter.add(new PrintWriter(clientSocket.getOutputStream())); + sharedList.add(new PrintWriter(clientSocket.getOutputStream())); boolean end = false; while (!end) { String theLine = in.readLine(); if (theLine.equals(".")) end = true; // le thread de service doit terminer - out.println(theLine); + broadcastMsg(theLine); } System.out.println("Fin du thread repondant au client, port " + clientSocket.getPort()); } - public void broadcastMsg(String msg) { - for (int i = 0; i < listWriter.size(); i++) { - listWriter.get(i); + private void broadcastMsg(String msg) { + ListIterator iter = listWriter.listIterator(); + while (iter.hasNext()) { + PrintWriter cursorPW = iter.next(); + cursorPW.println(msg); + cursorPW.flush(); } - } }