From: Jérôme Benoit Date: Fri, 16 Mar 2018 14:17:18 +0000 (+0100) Subject: TD2: Fix the remaining bugs in the IHM chat client. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=92d3f605b46c717c1f8dc7aa30dd25db518b7582;p=TD_SR.git TD2: Fix the remaining bugs in the IHM chat client. Signed-off-by: Jérôme Benoit --- diff --git a/TD2/IHM/IHM.java b/TD2/IHM/IHM.java index 08cbf2c..a10c23d 100644 --- a/TD2/IHM/IHM.java +++ b/TD2/IHM/IHM.java @@ -12,6 +12,7 @@ import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.ScrollPaneConstants; +import java.util.Random; /** * Applications réparties @@ -28,13 +29,32 @@ public class IHM implements ActionListener { private JTextArea entrants; private JTextField sortants; private ArrayList sendMessages; + //FIXME: use a random name by thread for now, should be setable + private String randName; IHM() { sendMessages = new ArrayList(); + randName = randomName(); + } + + private String randomName() { + + int leftLimit = 97; // letter 'a' + int rightLimit = 122; // letter 'z' + int targetStringLength = 8; + Random random = new Random(); + StringBuilder buffer = new StringBuilder(targetStringLength); + for (int i = 0; i < targetStringLength; i++) { + int randomLimitedInt = leftLimit + (int)(random.nextFloat() * (rightLimit - leftLimit + 1)); + buffer.append((char)randomLimitedInt); + } + String generatedString = buffer.toString(); + + return generatedString; } public void go() { - JFrame cadre = new JFrame("Client de discussion"); + JFrame cadre = new JFrame("Client de discussion de " + randName); JPanel panneau = new JPanel(); entrants = new JTextArea(15, 30); entrants.setLineWrap(true); @@ -75,7 +95,7 @@ public class IHM implements ActionListener { } String mess = (String)sendMessages.remove(0); System.out.println("IHM -> message a envoyer : " + mess); - return mess; + return randName + "> " + mess; } public void writeMessage(String mess) { diff --git a/TD2/client/ThreadClientoSend.java b/TD2/client/ThreadClientoSend.java index d6462a7..52db7d5 100644 --- a/TD2/client/ThreadClientoSend.java +++ b/TD2/client/ThreadClientoSend.java @@ -4,8 +4,7 @@ import java.util.Random; public class ThreadClientoSend implements Runnable { private SocketClient client; - //FIXME: use a random name by thread for now - // should be setable + //FIXME: use a random name by thread for now, should be setable private String randName; ThreadClientoSend(SocketClient c) { diff --git a/TD2/server/BroadcastThreadService.java b/TD2/server/BroadcastThreadService.java index 4bb2943..9ea5cdd 100644 --- a/TD2/server/BroadcastThreadService.java +++ b/TD2/server/BroadcastThreadService.java @@ -54,7 +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()); + 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 3431b77..fb4737c 100644 --- a/TD2/server/BroadcastoThreadService.java +++ b/TD2/server/BroadcastoThreadService.java @@ -58,7 +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()); + 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 68ef78e..12aed42 100644 --- a/TD2/server/Main.java +++ b/TD2/server/Main.java @@ -5,7 +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 :"); + System.out.println("Demarrage du serveur sur le port " + Integer.parseInt(args[0]) + " et 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();