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
 					{
82
 					{
83
 						portNumber++;
83
 						portNumber++;
84
 						Socket s = (new ServerSocket(portNumber)).accept();
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
 			        	outdoor_dest_sockets.add(s);
86
 			        	outdoor_dest_sockets.add(s);
87
 		        	  	
87
 		        	  	
88
 						known_users.add(new User(username));
88
 						known_users.add(new User(username));
118
 	JTextArea displayArea;
118
 	JTextArea displayArea;
119
 	JTextArea knownUsersPanel;
119
 	JTextArea knownUsersPanel;
120
 	Boolean isOutdoor;
120
 	Boolean isOutdoor;
121
+	Boolean self;
121
 	List<User> known_users;
122
 	List<User> known_users;
122
 	List<Socket> dest_sockets;
123
 	List<Socket> dest_sockets;
123
 	List<Socket> outdoor_dest_sockets;
124
 	List<Socket> outdoor_dest_sockets;
124
 
125
 
125
 	ReceiveThread(User in_user, Socket in_socket, JTextArea in_displayArea, List<User> in_known_users, JTextArea in_knownUsersPanel,
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
 		user = in_user;
129
 		user = in_user;
129
 		socket = in_socket;
130
 		socket = in_socket;
133
 		dest_sockets = in_dest_sockets;
134
 		dest_sockets = in_dest_sockets;
134
 		outdoor_dest_sockets = in_outdoor_dest_sockets;
135
 		outdoor_dest_sockets = in_outdoor_dest_sockets;
135
 		isOutdoor = in_isOutdoor;
136
 		isOutdoor = in_isOutdoor;
137
+		self = in_self;
136
 	}
138
 	}
137
 	public void run()
139
 	public void run()
138
 	{
140
 	{
141
     	{
143
     	{
142
 			while(!exit)
144
 			while(!exit)
143
 			{
145
 			{
144
-					ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
146
+        			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
145
 			        Message message = (Message) in.readObject();
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
 		            	for(Socket s:dest_sockets)
150
 		            	for(Socket s:dest_sockets)
185
 			    		{
151
 			    		{
191
 			    			catch(IOException e){}
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
 		catch(EOFException e) {}
214
 		catch(EOFException e) {}
209
 		catch(Exception e)
215
 		catch(Exception e)
292
 					{
298
 					{
293
 						portNumber++;
299
 						portNumber++;
294
 						Socket s = (new ServerSocket(portNumber)).accept();
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
 			        	dest_sockets.add(s);
302
 			        	dest_sockets.add(s);
297
 					
303
 					
298
 		        	  	
304
 		        	  	
425
             	}
431
             	}
426
                 else
432
                 else
427
 	          	{
433
 	          	{
428
-	          		  Socket requestSocket = new Socket(destinationIP, 1234);
434
+	          		  Socket requestSocket = new Socket(destinationIP, 1233);
429
 	          		  BufferedReader in = new BufferedReader(new InputStreamReader(requestSocket.getInputStream()));
435
 	          		  BufferedReader in = new BufferedReader(new InputStreamReader(requestSocket.getInputStream()));
430
 	          		  PrintWriter out = new PrintWriter(requestSocket.getOutputStream(), true);
436
 	          		  PrintWriter out = new PrintWriter(requestSocket.getOutputStream(), true);
431
 	          		  out.println(username);
437
 	          		  out.println(username);
450
                 	{
456
                 	{
451
             			Socket s = new Socket(a, portNumber);
457
             			Socket s = new Socket(a, portNumber);
452
                 		dest_sockets.add(s);
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
 					Collections.sort(known_users);
461
 					Collections.sort(known_users);
456
             		connected = true;
462
             		connected = true;
489
 	        	  	/*Attention, getLocalIP ne marche que sur un même réseau physique
495
 	        	  	/*Attention, getLocalIP ne marche que sur un même réseau physique
490
 	        	  	 * pour tester avec Hamachi, il faut hardcoder les IP
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
 	          catch(IOException e)
500
 	          catch(IOException e)
495
 	          {
501
 	          {
502
         	  {
508
         	  {
503
         		  Socket s = new Socket(destinationIP, portNumber);
509
         		  Socket s = new Socket(destinationIP, portNumber);
504
             	  dest_sockets.add(s);
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
         	  catch(IOException e)
513
         	  catch(IOException e)
508
 	          {
514
 	          {
532
 	{
538
 	{
533
 		for(Socket s:dest_sockets)
539
 		for(Socket s:dest_sockets)
534
 		{
540
 		{
541
+			System.out.println("Indoor users:");
535
 			try
542
 			try
536
 			{
543
 			{
537
 				System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
544
 				System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
540
 			}
547
 			}
541
 			catch(IOException e){}
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
 	void disconnect()
563
 	void disconnect()

Loading…
Cancel
Save