Commit | Line | Data |
---|---|---|
c4aaaeca | 1 | import java.io.*; |
da31e6da JB |
2 | import java.util.Calendar; |
3 | import java.util.Random; | |
c4aaaeca JB |
4 | |
5 | public class ThreadClientoSend implements Runnable { | |
6 | private SocketClient client; | |
da31e6da | 7 | //FIXME: use a random name by thread for now |
6b300998 | 8 | // should be setable |
da31e6da | 9 | private String randName; |
c4aaaeca JB |
10 | |
11 | ThreadClientoSend(SocketClient c) { | |
12 | client = c; | |
da31e6da JB |
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; | |
c4aaaeca JB |
30 | } |
31 | ||
32 | public void run() { | |
33 | BufferedReader userInput = null; | |
34 | try { | |
35 | userInput = new BufferedReader(new InputStreamReader(System.in)); | |
36 | boolean end = false; | |
6b300998 JB |
37 | while (!end) { |
38 | String line = userInput.readLine(); | |
39 | if (line.equals(".")) { | |
c4aaaeca JB |
40 | end = true; |
41 | } | |
da31e6da | 42 | Message oMsg = new Message(randName, line, Calendar.getInstance()); |
c4aaaeca JB |
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) { | |
6b300998 JB |
53 | try { |
54 | userInput.close(); | |
55 | } | |
56 | catch (IOException e) { | |
57 | System.err.println("IOException: " + e); | |
58 | } | |
59 | } | |
c4aaaeca JB |
60 | } |
61 | } | |
62 | } |