|
@@ -14,387 +14,6 @@ import java.util.*;
|
14
|
14
|
import chat.User;
|
15
|
15
|
import chat.Message;
|
16
|
16
|
|
17
|
|
-class OutdoorListenerThread extends Thread {
|
18
|
|
- User user;
|
19
|
|
- JTextArea displayArea;
|
20
|
|
- JTextArea knownUsersPanel;
|
21
|
|
- List<User> known_users;
|
22
|
|
- List<User> known_outdoor_users;
|
23
|
|
- List<Socket> dest_sockets;
|
24
|
|
- List<Socket> outdoor_dest_sockets;
|
25
|
|
- int portNumber;
|
26
|
|
-
|
27
|
|
-OutdoorListenerThread(User in_user, List<User> in_known_users, List<User> in_known_outdoor_users, List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets,
|
28
|
|
- JTextArea in_displayArea, JTextArea in_knownUsersPanel)
|
29
|
|
- {
|
30
|
|
- user = in_user;
|
31
|
|
- known_users = in_known_users;
|
32
|
|
- known_outdoor_users = in_known_outdoor_users;
|
33
|
|
- dest_sockets = in_dest_sockets;
|
34
|
|
- outdoor_dest_sockets = in_outdoor_dest_sockets;
|
35
|
|
- displayArea = in_displayArea;
|
36
|
|
- knownUsersPanel = in_knownUsersPanel;
|
37
|
|
- portNumber = 2344;
|
38
|
|
- }
|
39
|
|
- public void run()
|
40
|
|
- {
|
41
|
|
- boolean exit = false;
|
42
|
|
- boolean accepted = true;
|
43
|
|
-
|
44
|
|
- String notification;
|
45
|
|
- String username;
|
46
|
|
- String response = "";
|
47
|
|
-
|
48
|
|
- byte[] buffer = new byte[100];
|
49
|
|
- DatagramPacket notifPacket = new DatagramPacket(buffer, buffer.length);
|
50
|
|
- DatagramSocket notifSocket;
|
51
|
|
- byte[] notifBytes;
|
52
|
|
-
|
53
|
|
- try
|
54
|
|
- {
|
55
|
|
- while(!exit)
|
56
|
|
- {
|
57
|
|
- try
|
58
|
|
- {
|
59
|
|
- ServerSocket servSocket = new ServerSocket(1233);
|
60
|
|
- System.out.println("Waiting for connection request");
|
61
|
|
- Socket link = servSocket.accept();
|
62
|
|
- BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
|
63
|
|
- PrintWriter out = new PrintWriter(link.getOutputStream(), true);
|
64
|
|
- username = in.readLine();
|
65
|
|
- System.out.println("Received a request from " + username + "@" + link.getRemoteSocketAddress().toString());
|
66
|
|
-
|
67
|
|
- response = "";
|
68
|
|
- for(User u:known_users)
|
69
|
|
- {
|
70
|
|
- response += u.getName() + " ";
|
71
|
|
- }
|
72
|
|
- response = response.trim();
|
73
|
|
- response += ";";
|
74
|
|
- for(User u:known_outdoor_users)
|
75
|
|
- {
|
76
|
|
- response += u.getName() + " ";
|
77
|
|
- }
|
78
|
|
- response = response.trim();
|
79
|
|
- response += ";" + (portNumber+1);
|
80
|
|
-
|
81
|
|
- out.println(response);
|
82
|
|
-
|
83
|
|
- accepted = true;
|
84
|
|
- for(User a:known_users)
|
85
|
|
- {
|
86
|
|
- accepted = accepted && (!username.equals(a.getName()));
|
87
|
|
- }
|
88
|
|
- for(User a:known_outdoor_users)
|
89
|
|
- {
|
90
|
|
- accepted = accepted && (!username.equals(a.getName()));
|
91
|
|
- }
|
92
|
|
- if(accepted)
|
93
|
|
- {
|
94
|
|
- notification = "+" + username;
|
95
|
|
- notifBytes = notification.getBytes();
|
96
|
|
- notifSocket = new DatagramSocket();
|
97
|
|
- notifPacket = new DatagramPacket(notifBytes, notifBytes.length, InetAddress.getByName("192.168.1.255"), 1234);
|
98
|
|
- notifSocket.send(notifPacket);
|
99
|
|
-
|
100
|
|
- portNumber++;
|
101
|
|
- Socket s = (new ServerSocket(portNumber)).accept();
|
102
|
|
- (new ReceiveThread(user, s, displayArea, known_users, known_outdoor_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, true)).start();
|
103
|
|
- outdoor_dest_sockets.add(s);
|
104
|
|
- }
|
105
|
|
- servSocket.close();
|
106
|
|
- }
|
107
|
|
- catch(SocketTimeoutException e) {}
|
108
|
|
- }
|
109
|
|
- }
|
110
|
|
- catch(Exception e)
|
111
|
|
- {
|
112
|
|
- e.printStackTrace();
|
113
|
|
- System.out.println(e.getMessage());
|
114
|
|
- }
|
115
|
|
- }
|
116
|
|
-}
|
117
|
|
-
|
118
|
|
-class ReceiveThread extends Thread {
|
119
|
|
- User user;
|
120
|
|
- Socket socket;
|
121
|
|
- JTextArea displayArea;
|
122
|
|
- JTextArea knownUsersPanel;
|
123
|
|
- Boolean isOutdoor;
|
124
|
|
- List<User> known_users;
|
125
|
|
- List<User> known_outdoor_users;
|
126
|
|
- List<Socket> dest_sockets;
|
127
|
|
- List<Socket> outdoor_dest_sockets;
|
128
|
|
-
|
129
|
|
- ReceiveThread(User in_user, Socket in_socket, JTextArea in_displayArea, List<User> in_known_users, List<User> in_known_outdoor_users,
|
130
|
|
- JTextArea in_knownUsersPanel, List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets, Boolean in_isOutdoor)
|
131
|
|
- {
|
132
|
|
- user = in_user;
|
133
|
|
- socket = in_socket;
|
134
|
|
- displayArea = in_displayArea;
|
135
|
|
- knownUsersPanel = in_knownUsersPanel;
|
136
|
|
- known_users = in_known_users;
|
137
|
|
- known_outdoor_users = in_known_outdoor_users;
|
138
|
|
- dest_sockets = in_dest_sockets;
|
139
|
|
- outdoor_dest_sockets = in_outdoor_dest_sockets;
|
140
|
|
- isOutdoor = in_isOutdoor;
|
141
|
|
- }
|
142
|
|
- public void run()
|
143
|
|
- {
|
144
|
|
- boolean exit = false;
|
145
|
|
- try
|
146
|
|
- {
|
147
|
|
- while(!exit)
|
148
|
|
- {
|
149
|
|
- ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
150
|
|
- Message message = (Message) in.readObject();
|
151
|
|
- if(isOutdoor)
|
152
|
|
- {
|
153
|
|
- for(Socket s:dest_sockets)
|
154
|
|
- {
|
155
|
|
- try
|
156
|
|
- {
|
157
|
|
- ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
158
|
|
- out.writeObject(new Message(message.getAuthor(), message.getText(), true));
|
159
|
|
- }
|
160
|
|
- catch(IOException e){}
|
161
|
|
- }
|
162
|
|
- }
|
163
|
|
- else
|
164
|
|
- {
|
165
|
|
- if(message.getText() != null)
|
166
|
|
- {
|
167
|
|
- displayArea.append(message.getAuthor() + " : " + message.getText() + "\n");
|
168
|
|
- }
|
169
|
|
- else
|
170
|
|
- {
|
171
|
|
- displayArea.append(message.getAuthor() + " has left the chat.\n");
|
172
|
|
- for(int i = 0;i < known_users.size();i ++)
|
173
|
|
- {
|
174
|
|
- if (known_users.get(i).getName().equals(message.getAuthor()))
|
175
|
|
- {
|
176
|
|
- known_users.remove(i);
|
177
|
|
- break;
|
178
|
|
- //System.out.println("Removing " + message.getAuthor());
|
179
|
|
- }
|
180
|
|
- }
|
181
|
|
- for(int i = 0;i < known_outdoor_users.size();i ++)
|
182
|
|
- {
|
183
|
|
- if (known_outdoor_users.get(i).getName().equals(message.getAuthor()))
|
184
|
|
- {
|
185
|
|
- known_outdoor_users.remove(i);
|
186
|
|
- break;
|
187
|
|
- //System.out.println("Removing " + message.getAuthor());
|
188
|
|
- }
|
189
|
|
- }
|
190
|
|
-
|
191
|
|
- if(!message.isRedirected() && !message.getAuthor().equals(user.getName()))
|
192
|
|
- {
|
193
|
|
- socket.close();
|
194
|
|
- if(!isOutdoor)
|
195
|
|
- dest_sockets.remove(socket);
|
196
|
|
- else
|
197
|
|
- outdoor_dest_sockets.remove(socket);
|
198
|
|
- }
|
199
|
|
-
|
200
|
|
- knownUsersPanel.setText("");
|
201
|
|
- knownUsersPanel.append("Indoor users:\n");
|
202
|
|
- for(User a:known_users)
|
203
|
|
- {
|
204
|
|
- knownUsersPanel.append(" " + a.getName() + " \n");
|
205
|
|
- }
|
206
|
|
- knownUsersPanel.append("\nOutdoor users:\n");
|
207
|
|
- for(User a:known_outdoor_users)
|
208
|
|
- {
|
209
|
|
- knownUsersPanel.append(" " + a.getName() + " \n");
|
210
|
|
- }
|
211
|
|
- }
|
212
|
|
- displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
213
|
|
- }
|
214
|
|
- if(!message.getAuthor().equals(user.getName()) && !message.isRedirected())
|
215
|
|
- {
|
216
|
|
- for(Socket s:outdoor_dest_sockets)
|
217
|
|
- {
|
218
|
|
- try
|
219
|
|
- {
|
220
|
|
- ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
221
|
|
- out.writeObject(new Message(message.getAuthor(), message.getText(), true));
|
222
|
|
- }
|
223
|
|
- catch(IOException e){}
|
224
|
|
- }
|
225
|
|
- }
|
226
|
|
- }
|
227
|
|
- }
|
228
|
|
- catch(EOFException e) {}
|
229
|
|
- catch(Exception e)
|
230
|
|
- {
|
231
|
|
- e.printStackTrace();
|
232
|
|
- }
|
233
|
|
- }
|
234
|
|
-}
|
235
|
|
-
|
236
|
|
-class ConnectionListenerThread extends Thread {
|
237
|
|
- User user;
|
238
|
|
- JTextArea displayArea;
|
239
|
|
- JTextArea knownUsersPanel;
|
240
|
|
- List<User> known_users;
|
241
|
|
- List<User> known_outdoor_users;
|
242
|
|
- List<Socket> dest_sockets;
|
243
|
|
- List<Socket> outdoor_dest_sockets;
|
244
|
|
- int portNumber;
|
245
|
|
-
|
246
|
|
- ConnectionListenerThread(User in_user, List<User> in_known_users, List<User> in_known_outdoor_users, List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets,
|
247
|
|
- JTextArea in_displayArea, JTextArea in_knownUsersPanel, int in_portNumber)
|
248
|
|
- {
|
249
|
|
- user = in_user;
|
250
|
|
- known_users = in_known_users;
|
251
|
|
- known_outdoor_users = in_known_outdoor_users;
|
252
|
|
- dest_sockets = in_dest_sockets;
|
253
|
|
- outdoor_dest_sockets = in_outdoor_dest_sockets;
|
254
|
|
- displayArea = in_displayArea;
|
255
|
|
- knownUsersPanel = in_knownUsersPanel;
|
256
|
|
- portNumber = in_portNumber;
|
257
|
|
- }
|
258
|
|
- public void run()
|
259
|
|
- {
|
260
|
|
- boolean exit = false;
|
261
|
|
- boolean accepted = true;
|
262
|
|
- boolean outdoor;
|
263
|
|
- byte[] buffer = new byte[100];
|
264
|
|
- DatagramPacket request = new DatagramPacket(buffer, buffer.length);
|
265
|
|
- DatagramPacket responsePacket;
|
266
|
|
-
|
267
|
|
- DatagramSocket requestSocket;
|
268
|
|
- DatagramSocket responseSocket;
|
269
|
|
-
|
270
|
|
- String username;
|
271
|
|
- String response = "";
|
272
|
|
-
|
273
|
|
- byte[] responseBytes;
|
274
|
|
- try
|
275
|
|
- {
|
276
|
|
- requestSocket = new DatagramSocket(1234);
|
277
|
|
- responseSocket = new DatagramSocket();
|
278
|
|
-
|
279
|
|
- while(!exit)
|
280
|
|
- {
|
281
|
|
- try
|
282
|
|
- {
|
283
|
|
- System.out.println("Waiting for connection request");
|
284
|
|
- requestSocket.receive(request);
|
285
|
|
- username = new String(request.getData(), 0, request.getLength());
|
286
|
|
-
|
287
|
|
- if(username.charAt(0) == '+')
|
288
|
|
- {
|
289
|
|
- username = username.substring(1);
|
290
|
|
- outdoor = true;
|
291
|
|
- }
|
292
|
|
- else
|
293
|
|
- {
|
294
|
|
- outdoor = false;
|
295
|
|
- InetAddress clientAddress= request.getAddress();
|
296
|
|
- System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
|
297
|
|
-
|
298
|
|
- response = "";
|
299
|
|
- for(User u:known_users)
|
300
|
|
- {
|
301
|
|
- response += u.getName() + " ";
|
302
|
|
- }
|
303
|
|
- response = response.trim();
|
304
|
|
- response += ";";
|
305
|
|
- for(User u:known_outdoor_users)
|
306
|
|
- {
|
307
|
|
- response += u.getName() + " ";
|
308
|
|
- }
|
309
|
|
- response = response.trim();
|
310
|
|
- response += ";";
|
311
|
|
- for(Socket s:dest_sockets)
|
312
|
|
- {
|
313
|
|
- response += s.getInetAddress().getHostAddress() + " ";
|
314
|
|
- }
|
315
|
|
- response = response.trim();
|
316
|
|
-
|
317
|
|
- response += ";" + (portNumber+1);
|
318
|
|
-
|
319
|
|
- System.out.println("Response :" + response);
|
320
|
|
- responseBytes = response.getBytes();
|
321
|
|
- responsePacket = new DatagramPacket(responseBytes, responseBytes.length, clientAddress, 1337);
|
322
|
|
- responseSocket.send(responsePacket);
|
323
|
|
- }
|
324
|
|
- accepted = true;
|
325
|
|
- for(User a:known_users)
|
326
|
|
- {
|
327
|
|
- accepted = accepted && (!username.equals(a.getName()));
|
328
|
|
- }
|
329
|
|
- for(User a:known_outdoor_users)
|
330
|
|
- {
|
331
|
|
- accepted = accepted && (!username.equals(a.getName()));
|
332
|
|
- }
|
333
|
|
- if(accepted)
|
334
|
|
- {
|
335
|
|
- if(outdoor)
|
336
|
|
- {
|
337
|
|
- known_outdoor_users.add(new User(username));
|
338
|
|
- Collections.sort(known_outdoor_users);
|
339
|
|
- }
|
340
|
|
- else
|
341
|
|
- {
|
342
|
|
- portNumber++;
|
343
|
|
- Socket s = (new ServerSocket(portNumber)).accept();
|
344
|
|
- (new ReceiveThread(user, s, displayArea, known_users, known_outdoor_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
|
345
|
|
- dest_sockets.add(s);
|
346
|
|
-
|
347
|
|
- known_users.add(new User(username));
|
348
|
|
-
|
349
|
|
- Collections.sort(known_users);
|
350
|
|
- }
|
351
|
|
-
|
352
|
|
- displayArea.append(username + " has joined the chat.\n");
|
353
|
|
- displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
354
|
|
-
|
355
|
|
- knownUsersPanel.setText("");
|
356
|
|
- knownUsersPanel.append("Indoor users:\n");
|
357
|
|
- for(User a:known_users)
|
358
|
|
- {
|
359
|
|
- knownUsersPanel.append(" " + a.getName() + " \n");
|
360
|
|
- }
|
361
|
|
- knownUsersPanel.append("\nOutdoor users:\n");
|
362
|
|
- for(User a:known_outdoor_users)
|
363
|
|
- {
|
364
|
|
- knownUsersPanel.append(" " + a.getName() + " \n");
|
365
|
|
- }
|
366
|
|
- }
|
367
|
|
- }
|
368
|
|
- catch(SocketTimeoutException e) {}
|
369
|
|
- }
|
370
|
|
- }
|
371
|
|
- catch(Exception e)
|
372
|
|
- {
|
373
|
|
- e.printStackTrace();
|
374
|
|
- System.out.println(e.getMessage());
|
375
|
|
- }
|
376
|
|
- }
|
377
|
|
-}
|
378
|
|
-
|
379
|
|
-class ListenerStartThread extends Thread {
|
380
|
|
-
|
381
|
|
- ServerSocket s;
|
382
|
|
- List<Socket> dest_sockets;
|
383
|
|
- ListenerStartThread(ServerSocket in_s, List<Socket> in_dest_sockets)
|
384
|
|
- {
|
385
|
|
- s = in_s;
|
386
|
|
- dest_sockets = in_dest_sockets;
|
387
|
|
- }
|
388
|
|
- public void run()
|
389
|
|
- {
|
390
|
|
- try
|
391
|
|
- {
|
392
|
|
- dest_sockets.add(s.accept());
|
393
|
|
- }
|
394
|
|
- catch(IOException e) {}
|
395
|
|
- }
|
396
|
|
-}
|
397
|
|
-
|
398
|
17
|
public class NetworkClient {
|
399
|
18
|
private User user;
|
400
|
19
|
private List<User> known_users;
|
|
@@ -506,6 +125,10 @@ public class NetworkClient {
|
506
|
125
|
{
|
507
|
126
|
known_users.add(new User (u));
|
508
|
127
|
}
|
|
128
|
+ for(String u:outdoorUsernameList)
|
|
129
|
+ {
|
|
130
|
+ known_outdoor_users.add(new User (u));
|
|
131
|
+ }
|
509
|
132
|
for(String a:addressList)
|
510
|
133
|
{
|
511
|
134
|
Socket s = new Socket(a, portNumber);
|
|
@@ -571,32 +194,14 @@ public class NetworkClient {
|
571
|
194
|
}
|
572
|
195
|
|
573
|
196
|
user.setName(username);
|
574
|
|
- if(!outdoor)
|
575
|
|
- known_users.add(new User(username));
|
576
|
|
- else
|
577
|
|
- known_outdoor_users.add(new User(username));
|
578
|
|
- Collections.sort(known_users);
|
579
|
197
|
|
580
|
|
- chatText.append(username + " has joined the chat.\n");
|
581
|
|
- chatText.setCaretPosition(chatText.getDocument().getLength());
|
582
|
|
-
|
583
|
|
- knownUsersPanel.setText("");
|
584
|
|
- knownUsersPanel.append("Indoor users:\n");
|
585
|
|
- for(User a:known_users)
|
586
|
|
- {
|
587
|
|
- knownUsersPanel.append(" " + a.getName() + " \n");
|
588
|
|
- }
|
589
|
|
- knownUsersPanel.append("\nOutdoor users:\n");
|
590
|
|
- for(User a:known_outdoor_users)
|
591
|
|
- {
|
592
|
|
- knownUsersPanel.append(" " + a.getName() + " \n");
|
593
|
|
- }
|
|
198
|
+ send(new ConnectNotification(user, false, outdoor));
|
594
|
199
|
}
|
595
|
200
|
|
596
|
201
|
return connected;
|
597
|
202
|
}
|
598
|
203
|
|
599
|
|
- void send (String message)
|
|
204
|
+ void send (Notification notif)
|
600
|
205
|
{
|
601
|
206
|
for(Socket s:dest_sockets)
|
602
|
207
|
{
|
|
@@ -604,7 +209,7 @@ public class NetworkClient {
|
604
|
209
|
{
|
605
|
210
|
//System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
|
606
|
211
|
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
607
|
|
- out.writeObject(new Message(user.getName(), message, false));
|
|
212
|
+ out.writeObject(notif);
|
608
|
213
|
}
|
609
|
214
|
catch(IOException e){}
|
610
|
215
|
}
|
|
@@ -614,7 +219,7 @@ public class NetworkClient {
|
614
|
219
|
{
|
615
|
220
|
//System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
|
616
|
221
|
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
617
|
|
- out.writeObject(new Message(user.getName(), message, false));
|
|
222
|
+ out.writeObject(notif);
|
618
|
223
|
}
|
619
|
224
|
catch(IOException e){}
|
620
|
225
|
}
|
|
@@ -622,7 +227,7 @@ public class NetworkClient {
|
622
|
227
|
|
623
|
228
|
void disconnect()
|
624
|
229
|
{
|
625
|
|
- send(null);
|
|
230
|
+ send(new DisconnectNotification(user, false));
|
626
|
231
|
try
|
627
|
232
|
{
|
628
|
233
|
Thread.sleep(1000);
|
|
@@ -638,4 +243,9 @@ public class NetworkClient {
|
638
|
243
|
catch(IOException e){}
|
639
|
244
|
}
|
640
|
245
|
}
|
|
246
|
+
|
|
247
|
+ User getUser()
|
|
248
|
+ {
|
|
249
|
+ return user;
|
|
250
|
+ }
|
641
|
251
|
}
|