Browse Source

outdoor users(5)

Louis Farina 3 years ago
parent
commit
7971f14ad5
1 changed files with 75 additions and 57 deletions
  1. 75
    57
      src/chat/NetworkClient.java

+ 75
- 57
src/chat/NetworkClient.java View File

@@ -82,7 +82,7 @@ class OutdoorListenerThread extends Thread {
82 82
 					{
83 83
 						portNumber++;
84 84
 						Socket s = (new ServerSocket(portNumber)).accept();
85
-						(new ReceiveThread(user, s, displayArea, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
85
+						(new ReceiveThread(user, s, displayArea, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, true, false)).start();
86 86
 			        	outdoor_dest_sockets.add(s);
87 87
 		        	  	
88 88
 						known_users.add(new User(username));
@@ -118,12 +118,13 @@ class ReceiveThread extends Thread {
118 118
 	JTextArea displayArea;
119 119
 	JTextArea knownUsersPanel;
120 120
 	Boolean isOutdoor;
121
+	Boolean self;
121 122
 	List<User> known_users;
122 123
 	List<Socket> dest_sockets;
123 124
 	List<Socket> outdoor_dest_sockets;
124 125
 
125 126
 	ReceiveThread(User in_user, Socket in_socket, JTextArea in_displayArea, List<User> in_known_users, JTextArea in_knownUsersPanel,
126
-			List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets, Boolean in_isOutdoor)
127
+			List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets, Boolean in_isOutdoor, Boolean in_self)
127 128
 	{
128 129
 		user = in_user;
129 130
 		socket = in_socket;
@@ -133,6 +134,7 @@ class ReceiveThread extends Thread {
133 134
 		dest_sockets = in_dest_sockets;
134 135
 		outdoor_dest_sockets = in_outdoor_dest_sockets;
135 136
 		isOutdoor = in_isOutdoor;
137
+		self = in_self;
136 138
 	}
137 139
 	public void run()
138 140
 	{
@@ -141,45 +143,9 @@ class ReceiveThread extends Thread {
141 143
     	{
142 144
 			while(!exit)
143 145
 			{
144
-					ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
146
+        			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
145 147
 			        Message message = (Message) in.readObject();
146
-		        	if(message.getText() != null)
147
-		        	{
148
-			            displayArea.append(message.getAuthor() + " : " + message.getText() + "\n");
149
-			            
150
-		        	}
151
-		        	else
152
-		        	{
153
-			            displayArea.append(message.getAuthor() + " has left the chat.\n");
154
-			            for(int i = 0;i < known_users.size();i ++)
155
-			            {
156
-			            	
157
-			            	if (known_users.get(i).getName().equals(message.getAuthor()))
158
-			            	{
159
-			            		known_users.remove(i);
160
-			            		break;
161
-			            		//System.out.println("Removing " + message.getAuthor());
162
-			            	}
163
-			            }
164
-			            knownUsersPanel.setText("");
165
-			            knownUsersPanel.append("Online:\n");
166
-			            for(User u:known_users)
167
-			            {
168
-			            	knownUsersPanel.append(" " + u.getName() + "  \n");
169
-			            }
170
-			            if(!message.getAuthor().equals(user.getName()))
171
-			            {
172
-			            	socket.close();
173
-			            	if(!isOutdoor)
174
-							dest_sockets.remove(socket);
175
-			            	else
176
-							outdoor_dest_sockets.remove(socket);
177
-			            }
178
-			            exit = true;
179
-		        	}
180
-		        	
181
-		            displayArea.setCaretPosition(displayArea.getDocument().getLength());
182
-		            if(isOutdoor)
148
+	            	if(isOutdoor)
183 149
 		            {
184 150
 		            	for(Socket s:dest_sockets)
185 151
 			    		{
@@ -191,19 +157,59 @@ class ReceiveThread extends Thread {
191 157
 			    			catch(IOException e){}
192 158
 			    		}
193 159
 		            }
194
-		            
195
-		            for(Socket s:outdoor_dest_sockets)
196
-		    		{
197
-		    			try
198
-		    			{
199
-		    				ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
200
-		    			    out.writeObject(message);
201
-		    			}
202
-		    			catch(IOException e){}
203
-		    		}
160
+	            	else
161
+	            	{
162
+			        	if(message.getText() != null)
163
+			        	{
164
+				            displayArea.append(message.getAuthor() + " : " + message.getText() + "\n");
165
+				            
166
+			        	}
167
+			        	else
168
+			        	{
169
+				            displayArea.append(message.getAuthor() + " has left the chat.\n");
170
+				            for(int i = 0;i < known_users.size();i ++)
171
+				            {
172
+				            	
173
+				            	if (known_users.get(i).getName().equals(message.getAuthor()))
174
+				            	{
175
+				            		known_users.remove(i);
176
+				            		break;
177
+				            		//System.out.println("Removing " + message.getAuthor());
178
+				            	}
179
+				            }
180
+				            knownUsersPanel.setText("");
181
+				            knownUsersPanel.append("Online:\n");
182
+				            for(User u:known_users)
183
+				            {
184
+				            	knownUsersPanel.append(" " + u.getName() + "  \n");
185
+				            }
186
+				            if(!self)
187
+				            {  	
188
+				            	socket.close();
189
+				            	if(!isOutdoor)
190
+								dest_sockets.remove(socket);
191
+				            	else
192
+								outdoor_dest_sockets.remove(socket);
193
+				            }
194
+				            
195
+			        	}
196
+			        	
197
+			            displayArea.setCaretPosition(displayArea.getDocument().getLength());
198
+	            	}
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
+		            }
204 212
 	        }
205
-		
206
-			
207 213
     	}
208 214
 		catch(EOFException e) {}
209 215
 		catch(Exception e)
@@ -292,7 +298,7 @@ class ConnectionListenerThread extends Thread {
292 298
 					{
293 299
 						portNumber++;
294 300
 						Socket s = (new ServerSocket(portNumber)).accept();
295
-						(new ReceiveThread(user, s, displayArea, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
301
+						(new ReceiveThread(user, s, displayArea, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false, false)).start();
296 302
 			        	dest_sockets.add(s);
297 303
 					
298 304
 		        	  	
@@ -425,7 +431,7 @@ public class NetworkClient {
425 431
             	}
426 432
                 else
427 433
 	          	{
428
-	          		  Socket requestSocket = new Socket(destinationIP, 1234);
434
+	          		  Socket requestSocket = new Socket(destinationIP, 1233);
429 435
 	          		  BufferedReader in = new BufferedReader(new InputStreamReader(requestSocket.getInputStream()));
430 436
 	          		  PrintWriter out = new PrintWriter(requestSocket.getOutputStream(), true);
431 437
 	          		  out.println(username);
@@ -450,7 +456,7 @@ public class NetworkClient {
450 456
                 	{
451 457
             			Socket s = new Socket(a, portNumber);
452 458
                 		dest_sockets.add(s);
453
-            			(new ReceiveThread(user, s, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
459
+            			(new ReceiveThread(user, s, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false, false)).start();
454 460
                 	}
455 461
 					Collections.sort(known_users);
456 462
             		connected = true;
@@ -489,7 +495,7 @@ public class NetworkClient {
489 495
 	        	  	/*Attention, getLocalIP ne marche que sur un même réseau physique
490 496
 	        	  	 * pour tester avec Hamachi, il faut hardcoder les IP
491 497
 	        	  	 */
492
-		  			(new ReceiveThread(user, s2, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
498
+		  			(new ReceiveThread(user, s2, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false, true)).start();
493 499
 	          }
494 500
 	          catch(IOException e)
495 501
 	          {
@@ -502,7 +508,7 @@ public class NetworkClient {
502 508
         	  {
503 509
         		  Socket s = new Socket(destinationIP, portNumber);
504 510
             	  dest_sockets.add(s);
505
-		  		  (new ReceiveThread(user, s, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
511
+		  		  (new ReceiveThread(user, s, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false, false)).start();
506 512
         	  }
507 513
         	  catch(IOException e)
508 514
 	          {
@@ -532,6 +538,7 @@ public class NetworkClient {
532 538
 	{
533 539
 		for(Socket s:dest_sockets)
534 540
 		{
541
+			System.out.println("Indoor users:");
535 542
 			try
536 543
 			{
537 544
 				System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
@@ -540,6 +547,17 @@ public class NetworkClient {
540 547
 			}
541 548
 			catch(IOException e){}
542 549
 		}
550
+		System.out.println("Outdoor users:");
551
+		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
+ 		}
543 561
 	}
544 562
 	
545 563
 	void disconnect()

Loading…
Cancel
Save