TD2: Ensure chat clients will not send empty message.
[TD_SR.git] / TD2 / IHM / IHM.java
index a414ec64c6c4f646258eb6b14daa163a39aef5dd..22624fcc9a8749b12819655f7a33dbf260094993 100644 (file)
@@ -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<String> sendMessages;
-    private SocketClient socketCl;
+    //FIXME: use a random name by thread for now, should be setable
+    private String randName;
 
     IHM() {
         sendMessages = new ArrayList<String>();
-        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