From ef87f1a8633012e6ba3ba20d095a619b7387ab49 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 16 Mar 2018 14:56:19 +0100 Subject: [PATCH] TD2: Make the IHM client with one receive thread and one send thread. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TD2/IHM/IHM.java | 36 ++++------------------ TD2/IHM/Main.java | 40 +++++++++++++++++++++++++ TD2/IHM/Makefile | 7 +++-- TD2/IHM/ThreadIHMReceive.java | 30 +++++++++++++++++++ TD2/IHM/ThreadIHMSend.java | 30 +++++++++++++++++++ TD2/server/BroadcastThreadService.java | 1 + TD2/server/BroadcastoThreadService.java | 1 + TD2/server/Main.java | 1 + 8 files changed, 113 insertions(+), 33 deletions(-) create mode 100644 TD2/IHM/Main.java create mode 100644 TD2/IHM/ThreadIHMReceive.java create mode 100644 TD2/IHM/ThreadIHMSend.java diff --git a/TD2/IHM/IHM.java b/TD2/IHM/IHM.java index a414ec6..08cbf2c 100644 --- a/TD2/IHM/IHM.java +++ b/TD2/IHM/IHM.java @@ -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 sendMessages; - private SocketClient socketCl; IHM() { sendMessages = new ArrayList(); - 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 index 0000000..af925f1 --- /dev/null +++ b/TD2/IHM/Main.java @@ -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(); + } + } + +} diff --git a/TD2/IHM/Makefile b/TD2/IHM/Makefile index 631ba1a..d3d7d4a 100644 --- a/TD2/IHM/Makefile +++ b/TD2/IHM/Makefile @@ -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 index 0000000..3a57d5c --- /dev/null +++ b/TD2/IHM/ThreadIHMReceive.java @@ -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 index 0000000..f173996 --- /dev/null +++ b/TD2/IHM/ThreadIHMSend.java @@ -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(); + } + } +} diff --git a/TD2/server/BroadcastThreadService.java b/TD2/server/BroadcastThreadService.java index fd05e17..4bb2943 100644 --- a/TD2/server/BroadcastThreadService.java +++ b/TD2/server/BroadcastThreadService.java @@ -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); diff --git a/TD2/server/BroadcastoThreadService.java b/TD2/server/BroadcastoThreadService.java index df56d31..3431b77 100644 --- a/TD2/server/BroadcastoThreadService.java +++ b/TD2/server/BroadcastoThreadService.java @@ -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); diff --git a/TD2/server/Main.java b/TD2/server/Main.java index 4e3da51..68ef78e 100644 --- a/TD2/server/Main.java +++ b/TD2/server/Main.java @@ -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(); -- 2.34.1