Browse Source

outdoor users(6)

Louis Farina 11 months ago
parent
commit
1edff06f81
2 changed files with 147 additions and 95 deletions
  1. 7
    1
      src/chat/Message.java
  2. 140
    94
      src/chat/NetworkClient.java

+ 7
- 1
src/chat/Message.java View File

@@ -7,11 +7,13 @@ public class Message implements Serializable{
7 7
 
8 8
 	private String text;
9 9
 	private String author;
10
+	private Boolean redirected;
10 11
 	
11
-	public Message(String in_author, String in_text)
12
+	public Message(String in_author, String in_text, Boolean in_redirected)
12 13
 	{
13 14
 		text = in_text;
14 15
 		author = in_author;
16
+		redirected = in_redirected;
15 17
 	}
16 18
 	public void setText(String new_text)
17 19
 	{
@@ -29,5 +31,9 @@ public class Message implements Serializable{
29 31
 	{
30 32
 		return author;
31 33
 	}
34
+	public Boolean isRedirected()
35
+	{
36
+		return redirected;
37
+	}
32 38
 }
33 39
 

+ 140
- 94
src/chat/NetworkClient.java View File

@@ -19,15 +19,17 @@ class OutdoorListenerThread extends Thread {
19 19
 	JTextArea displayArea;
20 20
 	JTextArea knownUsersPanel;
21 21
 	List<User> known_users;
22
+	List<User> known_outdoor_users;
22 23
 	List<Socket> dest_sockets;
23 24
 	List<Socket> outdoor_dest_sockets;
24 25
 	int portNumber;
25 26
 	
26
-	OutdoorListenerThread(User in_user, List<User> in_known_users, List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets,
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,
27 28
 			JTextArea in_displayArea, JTextArea in_knownUsersPanel)
28 29
 	{
29 30
 		user = in_user;
30 31
 		known_users = in_known_users;
32
+		known_outdoor_users = in_known_outdoor_users;
31 33
 		dest_sockets = in_dest_sockets;
32 34
 		outdoor_dest_sockets = in_outdoor_dest_sockets;
33 35
 		displayArea = in_displayArea;
@@ -39,10 +41,15 @@ class OutdoorListenerThread extends Thread {
39 41
 		boolean exit = false;
40 42
 		boolean accepted = true;
41 43
 
42
-		
44
+		String notification;
43 45
 		String username;
44 46
 		String response = "";
45 47
 		
48
+		byte[] buffer = new byte[100];
49
+		DatagramPacket notifPacket = new DatagramPacket(buffer, buffer.length);
50
+		DatagramSocket notifSocket;
51
+		byte[] notifBytes;
52
+		
46 53
 		try
47 54
 		{
48 55
 			while(!exit)
@@ -64,6 +71,11 @@ class OutdoorListenerThread extends Thread {
64 71
 					}
65 72
 					response = response.trim();
66 73
 					response += ";";
74
+					for(User u:known_outdoor_users)
75
+					{
76
+						response += u.getName() + " ";
77
+					}
78
+					response = response.trim();
67 79
 					for(Socket s:dest_sockets)
68 80
 					{
69 81
 						response += s.getInetAddress().getHostAddress() + " ";
@@ -78,25 +90,22 @@ class OutdoorListenerThread extends Thread {
78 90
 					{
79 91
 						accepted = accepted && (!username.equals(a.getName()));
80 92
 					}
93
+					for(User a:known_outdoor_users)
94
+					{
95
+						accepted = accepted && (!username.equals(a.getName()));
96
+					}
81 97
 					if(accepted)
82 98
 					{
99
+						notification = "+" + username;
100
+						notifBytes = notification.getBytes();
101
+						notifSocket = new DatagramSocket();
102
+						notifPacket = new DatagramPacket(notifBytes, notifBytes.length, InetAddress.getByName("192.168.1.255"), 1234);
103
+						notifSocket.send(notifPacket);
104
+						
83 105
 						portNumber++;
84 106
 						Socket s = (new ServerSocket(portNumber)).accept();
85
-						(new ReceiveThread(user, s, displayArea, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, true, false)).start();
107
+						(new ReceiveThread(user, s, displayArea, known_users, known_outdoor_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, true)).start();
86 108
 			        	outdoor_dest_sockets.add(s);
87
-		        	  	
88
-						known_users.add(new User(username));
89
-						
90
-						Collections.sort(known_users);
91
-						
92
-			            displayArea.append(username + " has joined the chat.\n");
93
-			            displayArea.setCaretPosition(displayArea.getDocument().getLength());
94
-			            knownUsersPanel.setText("");
95
-			            knownUsersPanel.append("Online:\n");
96
-			            for(User a:known_users)
97
-			            {
98
-			            	knownUsersPanel.append(" " + a.getName() + "  \n");
99
-			            }
100 109
 					}
101 110
 					servSocket.close();
102 111
 				}
@@ -111,30 +120,29 @@ class OutdoorListenerThread extends Thread {
111 120
 	}
112 121
 }
113 122
 
114
-
115 123
 class ReceiveThread extends Thread {
116 124
 	User user;
117 125
 	Socket socket;
118 126
 	JTextArea displayArea;
119 127
 	JTextArea knownUsersPanel;
120 128
 	Boolean isOutdoor;
121
-	Boolean self;
122 129
 	List<User> known_users;
130
+	List<User> known_outdoor_users;
123 131
 	List<Socket> dest_sockets;
124 132
 	List<Socket> outdoor_dest_sockets;
125 133
 
126
-	ReceiveThread(User in_user, Socket in_socket, JTextArea in_displayArea, List<User> in_known_users, JTextArea in_knownUsersPanel,
127
-			List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets, Boolean in_isOutdoor, Boolean in_self)
134
+	ReceiveThread(User in_user, Socket in_socket, JTextArea in_displayArea, List<User> in_known_users,  List<User> in_known_outdoor_users,
135
+			JTextArea in_knownUsersPanel, List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets, Boolean in_isOutdoor)
128 136
 	{
129 137
 		user = in_user;
130 138
 		socket = in_socket;
131 139
 		displayArea = in_displayArea;
132 140
 		knownUsersPanel = in_knownUsersPanel;
133 141
 		known_users = in_known_users;
142
+		known_outdoor_users = in_known_outdoor_users;
134 143
 		dest_sockets = in_dest_sockets;
135 144
 		outdoor_dest_sockets = in_outdoor_dest_sockets;
136 145
 		isOutdoor = in_isOutdoor;
137
-		self = in_self;
138 146
 	}
139 147
 	public void run()
140 148
 	{
@@ -152,7 +160,7 @@ class ReceiveThread extends Thread {
152 160
 			    			try
153 161
 			    			{
154 162
 			    				ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
155
-			    			    out.writeObject(message);
163
+			    			    out.writeObject(new Message(message.getAuthor(), message.getText(), true));
156 164
 			    			}
157 165
 			    			catch(IOException e){}
158 166
 			    		}
@@ -162,14 +170,12 @@ class ReceiveThread extends Thread {
162 170
 			        	if(message.getText() != null)
163 171
 			        	{
164 172
 				            displayArea.append(message.getAuthor() + " : " + message.getText() + "\n");
165
-				            
166 173
 			        	}
167 174
 			        	else
168 175
 			        	{
169 176
 				            displayArea.append(message.getAuthor() + " has left the chat.\n");
170 177
 				            for(int i = 0;i < known_users.size();i ++)
171 178
 				            {
172
-				            	
173 179
 				            	if (known_users.get(i).getName().equals(message.getAuthor()))
174 180
 				            	{
175 181
 				            		known_users.remove(i);
@@ -177,39 +183,52 @@ class ReceiveThread extends Thread {
177 183
 				            		//System.out.println("Removing " + message.getAuthor());
178 184
 				            	}
179 185
 				            }
180
-				            knownUsersPanel.setText("");
181
-				            knownUsersPanel.append("Online:\n");
182
-				            for(User u:known_users)
186
+				            for(int i = 0;i < known_outdoor_users.size();i ++)
183 187
 				            {
184
-				            	knownUsersPanel.append(" " + u.getName() + "  \n");
188
+				            	if (known_outdoor_users.get(i).getName().equals(message.getAuthor()))
189
+				            	{
190
+				            		known_outdoor_users.remove(i);
191
+				            		break;
192
+				            		//System.out.println("Removing " + message.getAuthor());
193
+				            	}
185 194
 				            }
186
-				            if(!self)
187
-				            {  	
195
+				            
196
+				            if(!message.isRedirected() && message.getAuthor() != user.getName())
197
+				            {
188 198
 				            	socket.close();
189 199
 				            	if(!isOutdoor)
190
-								dest_sockets.remove(socket);
200
+				            	dest_sockets.remove(socket);
191 201
 				            	else
192
-								outdoor_dest_sockets.remove(socket);
202
+				            	outdoor_dest_sockets.remove(socket);
193 203
 				            }
194 204
 				            
205
+				            knownUsersPanel.setText("");
206
+				            knownUsersPanel.append("Indoor users:\n");
207
+				            for(User a:known_users)
208
+				            {
209
+				            	knownUsersPanel.append(" " + a.getName() + "  \n");
210
+				            }
211
+				            knownUsersPanel.append("\nOutdoor users:\n");
212
+				            for(User a:known_outdoor_users)
213
+				            {
214
+				            	knownUsersPanel.append(" " + a.getName() + "  \n");
215
+				            }
195 216
 			        	}
196
-			        	
197 217
 			            displayArea.setCaretPosition(displayArea.getDocument().getLength());
198 218
 	            	}
199
-	            	
200
-		            if(!self)
201
-		            {
202
-		            	for(Socket s:outdoor_dest_sockets)
203
-			    		{
204
-			    			try
205
-			    			{
206
-			    				ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
207
-			    			    out.writeObject(message);
208
-			    			}
209
-			    			catch(IOException e){}
210
-			    		}
211
-		            }
212
-	        }
219
+	            	if(message.getAuthor() != user.getName())
220
+	            	{
221
+	            		for(Socket s:outdoor_dest_sockets)
222
+				    	{
223
+				    		try
224
+				    		{
225
+				    			ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
226
+				    		    out.writeObject(new Message(message.getAuthor(), message.getText(), true));
227
+				    		}
228
+				    		catch(IOException e){}
229
+				    	}
230
+	            	}
231
+		      }
213 232
     	}
214 233
 		catch(EOFException e) {}
215 234
 		catch(Exception e)
@@ -224,11 +243,12 @@ class ConnectionListenerThread extends Thread {
224 243
 	JTextArea displayArea;
225 244
 	JTextArea knownUsersPanel;
226 245
 	List<User> known_users;
246
+	List<User> known_outdoor_users;
227 247
 	List<Socket> dest_sockets;
228 248
 	List<Socket> outdoor_dest_sockets;
229 249
 	int portNumber;
230 250
 	
231
-	ConnectionListenerThread(User in_user, List<User> in_known_users, List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets,
251
+	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,
232 252
 			JTextArea in_displayArea, JTextArea in_knownUsersPanel, int in_portNumber)
233 253
 	{
234 254
 		user = in_user;
@@ -243,6 +263,7 @@ class ConnectionListenerThread extends Thread {
243 263
 	{
244 264
 		boolean exit = false;
245 265
 		boolean accepted = true;
266
+		boolean outdoor;
246 267
 		byte[] buffer = new byte[100];
247 268
 		DatagramPacket request = new DatagramPacket(buffer, buffer.length);
248 269
 		DatagramPacket responsePacket;
@@ -267,53 +288,78 @@ class ConnectionListenerThread extends Thread {
267 288
 					requestSocket.receive(request);
268 289
 					username = new String(request.getData(), 0, request.getLength());
269 290
 					
270
-					InetAddress clientAddress= request.getAddress();
271
-					System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
272
-					
273
-					response = "";
274
-					for(User u:known_users)
291
+					if(username.charAt(0) == '+')
275 292
 					{
276
-						response += u.getName() + " ";
293
+						username = username.substring(1);
294
+						outdoor = true;
277 295
 					}
278
-					response = response.trim();
279
-					response += ";";
280
-					for(Socket s:dest_sockets)
296
+					else
281 297
 					{
282
-						response += s.getInetAddress().getHostAddress() + " ";
298
+						outdoor = false;
299
+						InetAddress clientAddress= request.getAddress();
300
+						System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
301
+						
302
+						response = "";
303
+						for(User u:known_users)
304
+						{
305
+							response += u.getName() + " ";
306
+						}
307
+						response = response.trim();
308
+						response += ";";
309
+						for(Socket s:dest_sockets)
310
+						{
311
+							response += s.getInetAddress().getHostAddress() + " ";
312
+						}
313
+						response = response.trim();
314
+						response += ";" + (portNumber+1);
315
+						
316
+						System.out.println("Response :" + response);
317
+						responseBytes = response.getBytes();
318
+						responsePacket = new DatagramPacket(responseBytes, responseBytes.length, clientAddress, 1337);
319
+						responseSocket.send(responsePacket);
283 320
 					}
284
-					response = response.trim();
285
-					response += ";" + (portNumber+1);
286
-					
287
-					System.out.println("Response :" + response);
288
-					responseBytes = response.getBytes();
289
-					responsePacket = new DatagramPacket(responseBytes, responseBytes.length, clientAddress, 1337);
290
-					responseSocket.send(responsePacket);
291
-					
292 321
 					accepted = true;
293 322
 					for(User a:known_users)
294 323
 					{
295 324
 						accepted = accepted && (!username.equals(a.getName()));
296 325
 					}
326
+					for(User a:known_outdoor_users)
327
+					{
328
+						accepted = accepted && (!username.equals(a.getName()));
329
+					}
297 330
 					if(accepted)
298 331
 					{
299
-						portNumber++;
300
-						Socket s = (new ServerSocket(portNumber)).accept();
301
-						(new ReceiveThread(user, s, displayArea, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false, false)).start();
302
-			        	dest_sockets.add(s);
303
-					
304
-		        	  	
305
-						known_users.add(new User(username));
306
-						
307
-						Collections.sort(known_users);
332
+						if(outdoor)
333
+						{
334
+							known_outdoor_users.add(new User(username));
335
+							Collections.sort(known_outdoor_users);
336
+						}
337
+						else
338
+						{
339
+							portNumber++;
340
+							Socket s = (new ServerSocket(portNumber)).accept();
341
+							(new ReceiveThread(user, s, displayArea, known_users, known_outdoor_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
342
+				        	dest_sockets.add(s);
343
+			        	  	
344
+							known_users.add(new User(username));
345
+							
346
+							Collections.sort(known_users);
347
+						}
308 348
 						
309 349
 			            displayArea.append(username + " has joined the chat.\n");
310 350
 			            displayArea.setCaretPosition(displayArea.getDocument().getLength());
351
+			            
311 352
 			            knownUsersPanel.setText("");
312
-			            knownUsersPanel.append("Online:\n");
353
+			            knownUsersPanel.append("Indoor users:\n");
313 354
 			            for(User a:known_users)
314 355
 			            {
315 356
 			            	knownUsersPanel.append(" " + a.getName() + "  \n");
316 357
 			            }
358
+			            knownUsersPanel.append("\nOutdoor users:\n");
359
+			            for(User a:known_outdoor_users)
360
+			            {
361
+			            	knownUsersPanel.append(" " + a.getName() + "  \n");
362
+			            }
317 363
 					}
318 364
 				}
319 365
 				catch(SocketTimeoutException e) {}
@@ -349,6 +395,7 @@ class ListenerStartThread extends Thread {
349 395
 public class NetworkClient {
350 396
 	 private User user;
351 397
 	 private List<User> known_users;
398
+	 private List<User> known_outdoor_users;
352 399
 	 private List<Socket> dest_sockets;
353 400
 	 private List<Socket> outdoor_dest_sockets;
354 401
 	 private JTextArea chatText;
@@ -373,6 +420,7 @@ public class NetworkClient {
373 420
 		chatText = in_chatText;
374 421
 		knownUsersPanel = in_knownUsersPanel;
375 422
 		known_users = new ArrayList<User>();
423
+		known_outdoor_users = new ArrayList<User>();
376 424
 		dest_sockets = new ArrayList<Socket>();
377 425
 		outdoor_dest_sockets = new ArrayList<Socket>();
378 426
 	}
@@ -456,7 +504,7 @@ public class NetworkClient {
456 504
                 	{
457 505
             			Socket s = new Socket(a, portNumber);
458 506
                 		dest_sockets.add(s);
459
-            			(new ReceiveThread(user, s, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false, false)).start();
507
+            			(new ReceiveThread(user, s, chatText, known_users, known_outdoor_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
460 508
                 	}
461 509
 					Collections.sort(known_users);
462 510
             		connected = true;
@@ -482,8 +530,8 @@ public class NetworkClient {
482 530
       {
483 531
           if(!outdoor)
484 532
           {
485
-        	  (new ConnectionListenerThread(user, known_users, dest_sockets, outdoor_dest_sockets, chatText, knownUsersPanel, portNumber)).start();
486
-        	  (new OutdoorListenerThread(user, known_users, dest_sockets, outdoor_dest_sockets, chatText, knownUsersPanel)).start();
533
+        	  (new ConnectionListenerThread(user, known_users, known_outdoor_users, dest_sockets, outdoor_dest_sockets, chatText, knownUsersPanel, portNumber)).start();
534
+        	  (new OutdoorListenerThread(user, known_users, known_outdoor_users, dest_sockets, outdoor_dest_sockets, chatText, knownUsersPanel)).start();
487 535
 	          
488 536
 	          try
489 537
 	          {
@@ -495,7 +543,7 @@ public class NetworkClient {
495 543
 	        	  	/*Attention, getLocalIP ne marche que sur un même réseau physique
496 544
 	        	  	 * pour tester avec Hamachi, il faut hardcoder les IP
497 545
 	        	  	 */
498
-		  			(new ReceiveThread(user, s2, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false, true)).start();
546
+		  			(new ReceiveThread(user, s2, chatText, known_users, known_outdoor_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
499 547
 	          }
500 548
 	          catch(IOException e)
501 549
 	          {
@@ -508,7 +556,7 @@ public class NetworkClient {
508 556
         	  {
509 557
         		  Socket s = new Socket(destinationIP, portNumber);
510 558
             	  dest_sockets.add(s);
511
-		  		  (new ReceiveThread(user, s, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false, false)).start();
559
+		  		  (new ReceiveThread(user, s, chatText, known_users, known_outdoor_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
512 560
         	  }
513 561
         	  catch(IOException e)
514 562
 	          {
@@ -538,26 +586,24 @@ public class NetworkClient {
538 586
 	{
539 587
 		for(Socket s:dest_sockets)
540 588
 		{
541
-			System.out.println("Indoor users:");
542 589
 			try
543 590
 			{
544
-				System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
591
+				//System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
545 592
 				ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
546
-			    out.writeObject(new Message(user.getName(), message));
593
+			    out.writeObject(new Message(user.getName(), message, false));
547 594
 			}
548 595
 			catch(IOException e){}
549 596
 		}
550
-		System.out.println("Outdoor users:");
551 597
 		for(Socket s:outdoor_dest_sockets)
552
- 		{
553
- 			try
554
- 			{
555
-				System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
556
- 				ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
557
- 			    out.writeObject(new Message(user.getName(), message));
558
- 			}
559
- 			catch(IOException e){}
560
- 		}
598
+    	{
599
+    		try
600
+    		{
601
+    			//System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
602
+    			ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
603
+			    out.writeObject(new Message(user.getName(), message, false));
604
+    		}
605
+    		catch(IOException e){}
606
+    	}
561 607
 	}
562 608
 	
563 609
 	void disconnect()

Loading…
Cancel
Save