X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TD2%2Fclient%2FThreadClientoSend.java;h=dd8d03b65a1679e2745a5974164fb0248dfadc87;hb=6b300998692af2dca53cf1fe97305d27b56430b0;hp=267918041efd6b6f4aca10120d8529576df6484b;hpb=c4aaaecaa30716d0bc52f859f799880e5a37b027;p=TD_SR.git diff --git a/TD2/client/ThreadClientoSend.java b/TD2/client/ThreadClientoSend.java index 2679180..dd8d03b 100644 --- a/TD2/client/ThreadClientoSend.java +++ b/TD2/client/ThreadClientoSend.java @@ -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); + } + } } } }