X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TD2%2FIHM%2FIHM.java;h=22624fcc9a8749b12819655f7a33dbf260094993;hb=73a5e231a2e60b6a4e141b89ff56208dcf225b0b;hp=a414ec64c6c4f646258eb6b14daa163a39aef5dd;hpb=15850b4cdcf949dae5a150a7c209bd1b95061121;p=TD_SR.git diff --git a/TD2/IHM/IHM.java b/TD2/IHM/IHM.java index a414ec6..22624fc 100644 --- a/TD2/IHM/IHM.java +++ b/TD2/IHM/IHM.java @@ -12,7 +12,7 @@ import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.ScrollPaneConstants; -import java.io.IOException; +import java.util.Random; /** * Applications réparties @@ -29,15 +29,32 @@ public class IHM implements ActionListener { private JTextArea entrants; private JTextField sortants; private ArrayList sendMessages; - private SocketClient socketCl; + //FIXME: use a random name by thread for now, should be setable + private String randName; IHM() { sendMessages = new ArrayList(); - socketCl = new SocketClient(); + 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); @@ -67,7 +84,7 @@ public class IHM implements ActionListener { this.notify(); } - synchronized public void getAndSendNextMessage() { + synchronized public String getNextMessageToSend() { try { if (sendMessages.isEmpty()) this.wait(); @@ -77,36 +94,15 @@ public class IHM implements ActionListener { e.printStackTrace(); } String mess = (String)sendMessages.remove(0); - //System.out.println("IHM -> message a envoyer : " + mess); - socketCl.sendMsg(mess); + if (mess.length() == 0) + return ""; + System.out.println("IHM -> message a envoyer : " + mess); + return randName + "> " + 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