TD2: Make the server almost work as expected.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 13 Mar 2018 13:48:49 +0000 (14:48 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 13 Mar 2018 13:48:49 +0000 (14:48 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TD2/client/Main.java
TD2/client/ThreadClientReceive.java
TD2/client/ThreadClientSend.java
TD2/client/concurrent_access.txt
TD2/server/BroadcastThreadService.java [moved from TD2/server/EchoServerThreadService.java with 53% similarity]
TD2/server/Main.java
TD2/server/Makefile

index 81a31fb2e99b07607d82e5e10065039b5c2f2249..00d8924007536f9abc71d99c88d3fccdccece996 100644 (file)
@@ -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;
index e3b727b7e3b0abc7cf619b1380ccc149a44218a5..a3f28d87f29a28a2f3701fbce7d501deb10522d9 100644 (file)
@@ -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(".")) {
index 59ffbd0bab538c80a6b6f82f3ab094089fa7f7d1..9d40e0d6a7effddb01cd3e585fc7a1db0caabd33 100644 (file)
@@ -1,4 +1,3 @@
-import java.util.concurrent.ThreadLocalRandom;
 import java.io.*;
 
 public class ThreadClientSend implements Runnable {
index 377bcdc13732e7fdfb4b3b66dca32467fcef24de..506134b0aaa539496c59bbf3f74c52746e9adb84 100644 (file)
@@ -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
similarity index 53%
rename from TD2/server/EchoServerThreadService.java
rename to TD2/server/BroadcastThreadService.java
index 257f15c4d654dd587b82720d9d59189fb5ef4a1b..180f735b49fa442ed5982afa0a286d8c3f0ff879 100644 (file)
@@ -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<PrintWriter> listWriter;
+       private static ArrayList<PrintWriter> 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<PrintWriter>();
        }
 
        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<PrintWriter> 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<PrintWriter> iter = listWriter.listIterator();
+               while (iter.hasNext()) {
+                       PrintWriter cursorPW = iter.next();
+                       cursorPW.println(msg);
+                       cursorPW.flush();
                }
-
        }
 
 }
index 00d4f9eee7e4264cc8ed340a29fe641f311cfd29..d46becce8c45f9993b74a0022364485321d8e2d0 100644 (file)
@@ -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();
                        }
                }
index e3a745abaf289367cbc462dfa8a1330d9dd28801..ca95faac14fef397b9061a1930f1165ffd7030f7 100644 (file)
@@ -46,7 +46,7 @@ JVM = java
 # NAME = Camilo        Juan
 
 CLASSES = \
-               EchoServerThreadService.java \
+               BroadcastThreadService.java \
                Main.java
 
 #