Code cleanups.
[TD_SR.git] / TD2 / client / ThreadClientoSend.java
index 267918041efd6b6f4aca10120d8529576df6484b..dd8d03b65a1679e2745a5974164fb0248dfadc87 100644 (file)
@@ -1,10 +1,32 @@
 import java.io.*;
+import java.util.Calendar;
+import java.util.Random;
 
 public class ThreadClientoSend implements Runnable {
     private SocketClient client;
+    //FIXME: use a random name by thread for now
+    //       should be setable
+    private String randName;
 
     ThreadClientoSend(SocketClient c) {
         client = c;
+        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 run() {
@@ -12,12 +34,12 @@ public class ThreadClientoSend implements Runnable {
         try {
             userInput = new BufferedReader(new InputStreamReader(System.in));
             boolean end = false;
-                       while (!end) {
-                               String line = userInput.readLine();
-                               if (line.equals(".")) {
+            while (!end) {
+                String line = userInput.readLine();
+                if (line.equals(".")) {
                     end = true;
                 }
-                Message oMsg = new Message("Name", line);
+                Message oMsg = new Message(randName, line, Calendar.getInstance());
                 client.sendoMsg(oMsg);
                 System.out.println(Thread.currentThread().getName() + " a envoye " + oMsg);
             }
@@ -28,13 +50,13 @@ public class ThreadClientoSend implements Runnable {
         }
         finally {
             if (userInput != null) {
-                               try {
-                                       userInput.close();
-                               }
-                               catch (IOException e) {
-                                       System.err.println("IOException: " + e);
-                               }
-                       }
+                try {
+                    userInput.close();
+                }
+                catch (IOException e) {
+                    System.err.println("IOException: " + e);
+                }
+            }
         }
     }
 }