TD2: Make the IHM client with one receive thread and one send thread.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 16 Mar 2018 13:56:19 +0000 (14:56 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 16 Mar 2018 13:56:19 +0000 (14:56 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TD2/IHM/IHM.java
TD2/IHM/Main.java [new file with mode: 0644]
TD2/IHM/Makefile
TD2/IHM/ThreadIHMReceive.java [new file with mode: 0644]
TD2/IHM/ThreadIHMSend.java [new file with mode: 0644]
TD2/server/BroadcastThreadService.java
TD2/server/BroadcastoThreadService.java
TD2/server/Main.java

index a414ec64c6c4f646258eb6b14daa163a39aef5dd..08cbf2c8aba88b01c349fa57a2c31c5dfc3c9f55 100644 (file)
@@ -12,7 +12,6 @@ import javax.swing.JTextArea;
 import javax.swing.JTextField;
 import javax.swing.ScrollPaneConstants;
 
-import java.io.IOException;
 
 /**
  * Applications réparties
@@ -29,11 +28,9 @@ public class IHM implements ActionListener {
     private JTextArea entrants;
     private JTextField sortants;
     private ArrayList<String> sendMessages;
-    private SocketClient socketCl;
 
     IHM() {
         sendMessages = new ArrayList<String>();
-        socketCl = new SocketClient();
     }
 
     public void go() {
@@ -67,7 +64,7 @@ public class IHM implements ActionListener {
         this.notify();
     }
 
-    synchronized public void getAndSendNextMessage() {
+    synchronized public String getNextMessageToSend() {
         try {
             if (sendMessages.isEmpty())
                 this.wait();
@@ -77,36 +74,13 @@ public class IHM implements ActionListener {
             e.printStackTrace();
         }
         String mess = (String)sendMessages.remove(0);
-        //System.out.println("IHM -> message a envoyer : " + mess);
-        socketCl.sendMsg(mess);
+        System.out.println("IHM -> message a envoyer : " + mess);
+        return mess;
     }
 
-    public void writeMessage() throws IOException {
-        String mess = socketCl.receiveMsg();
-        //System.out.println("IHM -> message a ecrire : " + mess);
+    public void writeMessage(String mess) {
+        System.out.println("IHM -> message a ecrire : " + mess);
         entrants.append(mess + "\n");
     }
 
-    public void close() {
-        socketCl.closeRWIO();
-    }
-
-    public static void main (String[] args) {
-        IHM client = new IHM();
-        try {
-            client.go();
-            while (true) {
-                client.getAndSendNextMessage();
-                client.writeMessage();
-            }
-        }
-        catch (IOException e) {
-            System.err.println("IOException : " + e);
-            e.printStackTrace();
-        }
-        finally {
-            client.close();
-        }
-    }
-
 } // fin classe SimpleClientDiscussion
diff --git a/TD2/IHM/Main.java b/TD2/IHM/Main.java
new file mode 100644 (file)
index 0000000..af925f1
--- /dev/null
@@ -0,0 +1,40 @@
+import java.io.*;
+
+
+public class Main {
+
+       public static void main(String[] args) {
+               SocketClient client = null;
+               Thread thS = null;
+               Thread thR = null;
+               IHM clientIHM = null;
+
+               try {
+                       clientIHM = new IHM();
+                       clientIHM.go();
+                       client = new SocketClient();
+                       thS = new Thread(new ThreadIHMSend(client, clientIHM));
+                       thR = new Thread(new ThreadIHMReceive(client, clientIHM));
+                       thS.setName("thS");
+                       thS.start();
+                       thR.setName("thR");
+                       thR.start();
+               }
+               catch (Exception e) {
+                       System.err.println("Exception : " + e);
+                       e.printStackTrace();
+               }
+               finally {
+                       try {
+                               thS.join();
+                               thR.join();
+                       }
+                       catch (InterruptedException e) {
+                               System.err.println("InterruptedException: " + e);
+                               e.printStackTrace();
+                       }
+                       client.closeRWIO();
+               }
+       }
+
+}
index 631ba1acc429f7aa97af7dbbe7d74806a0c5837a..d3d7d4a27817217258b1d2c581ab75f1642fb685 100644 (file)
@@ -48,13 +48,16 @@ JVM = java
 CLASSES = \
                Message.java \
                SocketClient.java \
-               IHM.java
+               ThreadIHMReceive.java \
+               ThreadIHMSend.java \
+               IHM.java \
+               Main.java
 
 #
 # MAIN is a variable with the name of the file containing the main method
 #
 
-MAIN = IHM
+MAIN = Main
 
 #
 # the default make target entry
diff --git a/TD2/IHM/ThreadIHMReceive.java b/TD2/IHM/ThreadIHMReceive.java
new file mode 100644 (file)
index 0000000..3a57d5c
--- /dev/null
@@ -0,0 +1,30 @@
+import java.io.*;
+
+public class ThreadIHMReceive implements Runnable {
+    private SocketClient client;
+    private IHM clientIHM;
+
+    ThreadIHMReceive(SocketClient c, IHM ui) {
+        client = c;
+        clientIHM = ui;
+    }
+
+    public void run() {
+        try {
+            boolean end = false;
+            //FIXME: not exiting properly randomly from that loop!
+            while (!end) {
+                String rline = client.receiveMsg();
+                if (rline.equals(".")) {
+                    end = true;
+                }
+                clientIHM.writeMessage(rline);
+                System.out.println(Thread.currentThread().getName() + " a recu " + rline);
+            }
+        }
+        catch (IOException e) {
+            System.err.println("IOException: " + e);
+            e.printStackTrace();
+        }
+    }
+}
diff --git a/TD2/IHM/ThreadIHMSend.java b/TD2/IHM/ThreadIHMSend.java
new file mode 100644 (file)
index 0000000..f173996
--- /dev/null
@@ -0,0 +1,30 @@
+import java.io.*;
+
+public class ThreadIHMSend implements Runnable {
+    private SocketClient client;
+    private IHM clientIHM;
+
+    ThreadIHMSend(SocketClient c, IHM ui) {
+        client = c;
+        clientIHM = ui;
+    }
+
+    public void run() {
+        BufferedReader userInput = null;
+        try {
+            boolean end = false;
+            while (!end) {
+                String line = clientIHM.getNextMessageToSend();
+                if (line.equals(".")) {
+                    end = true;
+                }
+                client.sendMsg(line);
+                System.out.println(Thread.currentThread().getName() + " a envoye " + line);
+            }
+        }
+        catch (Exception e) {
+            System.err.println("Exception: " + e);
+            e.printStackTrace();
+        }
+    }
+}
index fd05e176447fa230421792430274ec96fcecc80d..4bb29435b74ebcd62f554aa4399586cd209a513a 100644 (file)
@@ -54,6 +54,7 @@ public class BroadcastThreadService implements Runnable {
                                end = true; // le thread de service doit terminer
                                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);
index df56d31ed4c87c18a7041241be3d870f258fde23..3431b77e0f5bd7bce763013435f1263d706e8571 100644 (file)
@@ -58,6 +58,7 @@ public class BroadcastoThreadService implements Runnable {
                                end = true; // le thread de service doit terminer
                                break; // do not broadcast the dot that will close all clients threads
                        }
+                       System.out.println("Broadcasting the message <" + roMsg + "> received from " + clientSocket.toString());
                        broadcastoMsg(roMsg);
                }
                sharedList.remove(OWriter);
index 4e3da51ddfa4dee353ec5d7598fcc2ecfbab834a..68ef78e817d11a1a1f3c698e011a8bfe626af630 100644 (file)
@@ -5,6 +5,7 @@ public class Main {
        public static void main(String[] args) {
                ServerSocket listenSocket = null;
                try {
+                       System.out.println("Demarrage du serveur sur le port " + Integer.parseInt(args[0]) + ", mise en attente de connexion :");
                        listenSocket = new ServerSocket(Integer.parseInt(args[0])); // port
                        while (true) { // le dispatcher est le thread qui execute main()
                                Socket clientSocket = listenSocket.accept();