| 1 | import java.io.*; |
| 2 | import java.util.Calendar; |
| 3 | import java.util.Random; |
| 4 | |
| 5 | public class ThreadClientoSend implements Runnable { |
| 6 | private SocketClient client; |
| 7 | //FIXME: use a random name by thread for now |
| 8 | // should be setable |
| 9 | private String randName; |
| 10 | |
| 11 | ThreadClientoSend(SocketClient c) { |
| 12 | client = c; |
| 13 | randName = randomName(); |
| 14 | } |
| 15 | |
| 16 | private String randomName() { |
| 17 | |
| 18 | int leftLimit = 97; // letter 'a' |
| 19 | int rightLimit = 122; // letter 'z' |
| 20 | int targetStringLength = 8; |
| 21 | Random random = new Random(); |
| 22 | StringBuilder buffer = new StringBuilder(targetStringLength); |
| 23 | for (int i = 0; i < targetStringLength; i++) { |
| 24 | int randomLimitedInt = leftLimit + (int)(random.nextFloat() * (rightLimit - leftLimit + 1)); |
| 25 | buffer.append((char)randomLimitedInt); |
| 26 | } |
| 27 | String generatedString = buffer.toString(); |
| 28 | |
| 29 | return generatedString; |
| 30 | } |
| 31 | |
| 32 | public void run() { |
| 33 | BufferedReader userInput = null; |
| 34 | try { |
| 35 | userInput = new BufferedReader(new InputStreamReader(System.in)); |
| 36 | boolean end = false; |
| 37 | while (!end) { |
| 38 | String line = userInput.readLine(); |
| 39 | if (line.equals(".")) { |
| 40 | end = true; |
| 41 | } |
| 42 | Message oMsg = new Message(randName, line, Calendar.getInstance()); |
| 43 | client.sendoMsg(oMsg); |
| 44 | System.out.println(Thread.currentThread().getName() + " a envoye " + oMsg); |
| 45 | } |
| 46 | } |
| 47 | catch (IOException e) { |
| 48 | System.err.println("IOException: " + e); |
| 49 | e.printStackTrace(); |
| 50 | } |
| 51 | finally { |
| 52 | if (userInput != null) { |
| 53 | try { |
| 54 | userInput.close(); |
| 55 | } |
| 56 | catch (IOException e) { |
| 57 | System.err.println("IOException: " + e); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |