From: Jérôme Benoit Date: Tue, 13 Mar 2018 13:48:49 +0000 (+0100) Subject: TD2: Make the server almost work as expected. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b9a33c97f77a1eae45b91426ca8061e18f163d4e;p=TD_SR.git TD2: Make the server almost work as expected. Signed-off-by: Jérôme Benoit --- diff --git a/TD2/client/Main.java b/TD2/client/Main.java index 81a31fb..00d8924 100644 --- a/TD2/client/Main.java +++ b/TD2/client/Main.java @@ -4,7 +4,7 @@ import java.io.*; public class Main { /** - * Main for testing ClientSimplifie + * main for testing ClientSimplifie */ private static void main1() { ClientSimplifie client = null; diff --git a/TD2/client/ThreadClientReceive.java b/TD2/client/ThreadClientReceive.java index e3b727b..a3f28d8 100644 --- a/TD2/client/ThreadClientReceive.java +++ b/TD2/client/ThreadClientReceive.java @@ -1,4 +1,3 @@ -import java.util.concurrent.ThreadLocalRandom; import java.io.*; public class ThreadClientReceive implements Runnable { @@ -11,7 +10,7 @@ public class ThreadClientReceive implements Runnable { public void run() { try { boolean end = false; - //FIXME: Not exiting properly from that loop! + //FIXME: Not exiting properly randomly from that loop! while (!end) { String rline = client.receiveMsg(); if (rline.equals(".")) { diff --git a/TD2/client/ThreadClientSend.java b/TD2/client/ThreadClientSend.java index 59ffbd0..9d40e0d 100644 --- a/TD2/client/ThreadClientSend.java +++ b/TD2/client/ThreadClientSend.java @@ -1,4 +1,3 @@ -import java.util.concurrent.ThreadLocalRandom; import java.io.*; public class ThreadClientSend implements Runnable { diff --git a/TD2/client/concurrent_access.txt b/TD2/client/concurrent_access.txt index 377bcdc..506134b 100644 --- a/TD2/client/concurrent_access.txt +++ b/TD2/client/concurrent_access.txt @@ -1,6 +1,8 @@ Question 4: -Lors d'un accès simultané au server sur le même port de deux ClientSimplifie, on +Le ClientSimplifie ne peut envoyer et recevoir en même temps. + +De plus lors d'un accès vraiment simultané au server sur le même port de deux ClientSimplifie, on soulève une exception sur le serveur : java.util.ConcurrentModificationException 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(); } - } } diff --git a/TD2/server/Main.java b/TD2/server/Main.java index 00d4f9e..d46becc 100644 --- a/TD2/server/Main.java +++ b/TD2/server/Main.java @@ -9,7 +9,7 @@ public class Main { while (true) { // le dispatcher est le thread qui execute main() Socket clientSocket = listenSocket.accept(); System.out.println("Connexion de :" + clientSocket.getInetAddress()); - Thread serviceThread = new Thread(new EchoServerThreadService(clientSocket)); + Thread serviceThread = new Thread(new BroadcastThreadService(clientSocket)); serviceThread.start(); } } diff --git a/TD2/server/Makefile b/TD2/server/Makefile index e3a745a..ca95faa 100644 --- a/TD2/server/Makefile +++ b/TD2/server/Makefile @@ -46,7 +46,7 @@ JVM = java # NAME = Camilo Juan CLASSES = \ - EchoServerThreadService.java \ + BroadcastThreadService.java \ Main.java #