TD1: Refactor the client side code.
[TD_SR.git] / TD2 / client / ThreadClientSend.java
index 69cd51a445a3879caa270103dd8a7056dd08c9f6..59ffbd0bab538c80a6b6f82f3ab094089fa7f7d1 100644 (file)
@@ -1,36 +1,40 @@
 import java.util.concurrent.ThreadLocalRandom;
+import java.io.*;
 
 public class ThreadClientSend implements Runnable {
     private SocketClient client;
-    private String msg = new String();
 
     ThreadClientSend(SocketClient c) {
         client = c;
     }
 
-    /**
-     * Message to sent setter
-     * @param m the text message to sent
-     */
-    public void setMsg(String m) {
-        msg = m;
-    }
-
     public void run() {
-        while (true) {
-            try {
-                client.sendMsg(msg);
-                System.out.println (Thread.currentThread().getName() + " a envoye " + msg);
-                try {
-                               Thread.sleep(ThreadLocalRandom.current().nextInt(101));
-                       }
-                       catch (InterruptedException e) {
-                               System.err.println("InterruptedException: " + e);
-                       }
-            }
-            catch (Exception e) {
-                System.err.println("Exception: " + e);
+        BufferedReader userInput = null;
+        try {
+            userInput = new BufferedReader(new InputStreamReader(System.in));
+            boolean end = false;
+                       while (!end) {
+                               String line = userInput.readLine();
+                               if (line.equals(".")) {
+                    end = true;
+                }
+                client.sendMsg(line);
+                System.out.println(Thread.currentThread().getName() + " a envoye " + line);
             }
+           }
+        catch (IOException e) {
+            System.err.println("IOException: " + e);
+            e.printStackTrace();
+        }
+        finally {
+            if (userInput != null) {
+                               try {
+                                       userInput.close();
+                               }
+                               catch (IOException e) {
+                                       System.err.println("IOException: " + e);
+                               }
+                       }
         }
     }
 }