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() {
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);
}
}
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);
+ e.printStackTrace();
+ }
+ }
}
}
}