Browse Source

Revert "outdoor users(17)"

This reverts commit 6d384512a6.
Louis Farina 3 years ago
parent
commit
3ce1752ddd

BIN
Projet.jar View File


+ 1
- 1
src/chat/ClientWindow.java View File

@@ -137,7 +137,7 @@ public class ClientWindow implements ActionListener {
137 137
     	if("".compareTo(message) != 0)
138 138
     	{
139 139
         	messageField.setText("");
140
-            network.send(new Message(network.getUser(), message));
140
+            network.send(new Message(network.getUser(), message, false));
141 141
     	}
142 142
     }
143 143
 }

+ 9
- 2
src/chat/ConnectNotification.java View File

@@ -1,9 +1,16 @@
1 1
 package chat;
2 2
 
3 3
 public class ConnectNotification extends Notification {
4
-	ConnectNotification (User in_author)
4
+	private Boolean outdoor;
5
+	ConnectNotification (User in_author, Boolean in_redirected, Boolean in_outdoor)
5 6
 	{
6
-		super(in_author);
7
+		author = in_author;
8
+		redirected = in_redirected;
9
+		outdoor = in_outdoor;
7 10
 	}
8 11
 	
12
+	public Boolean isOutdoor()
13
+	{
14
+		return outdoor;
15
+	}
9 16
 }

+ 3
- 2
src/chat/DisconnectNotification.java View File

@@ -1,8 +1,9 @@
1 1
 package chat;
2 2
 
3 3
 public class DisconnectNotification extends Notification {
4
-	DisconnectNotification (User in_author)
4
+	DisconnectNotification (User in_author, Boolean in_redirected)
5 5
 	{
6
-		super(in_author);
6
+		author = in_author;
7
+		redirected = in_redirected;
7 8
 	}
8 9
 }

+ 3
- 2
src/chat/Message.java View File

@@ -6,10 +6,11 @@ import java.util.*;
6 6
 public class Message extends Notification {
7 7
 	private String text;
8 8
 	
9
-	Message (User in_author, String in_text)
9
+	Message (User in_author, String in_text, Boolean in_redirected)
10 10
 	{
11
-		super(in_author);
11
+		author = in_author;
12 12
 		text = in_text;
13
+		redirected = in_redirected;
13 14
 	}
14 15
 	public void setText(String new_text)
15 16
 	{

+ 6
- 16
src/chat/NetworkClient.java View File

@@ -38,7 +38,7 @@ public class NetworkClient {
38 38
 	
39 39
 	NetworkClient(JTextArea in_chatText, JTextArea in_knownUsersPanel)
40 40
 	{
41
-		user = null;
41
+		user = new User("");
42 42
 		chatText = in_chatText;
43 43
 		knownUsersPanel = in_knownUsersPanel;
44 44
 		known_users = new ArrayList<User>();
@@ -123,11 +123,11 @@ public class NetworkClient {
123 123
             		System.out.println(usernameList.length + " users currently connected");
124 124
             		for(String u:usernameList)
125 125
                 	{
126
-                		known_users.add(new User (u, false));
126
+                		known_users.add(new User (u));
127 127
                 	}
128 128
             		for(String u:outdoorUsernameList)
129 129
                 	{
130
-                		known_outdoor_users.add(new User (u, true));
130
+                		known_outdoor_users.add(new User (u));
131 131
                 	}
132 132
             		for(String a:addressList)
133 133
                 	{
@@ -193,9 +193,9 @@ public class NetworkClient {
193 193
 	          }
194 194
           }
195 195
           
196
-          user = new User(username, outdoor);
196
+          user.setName(username);
197 197
           
198
-          send(new ConnectNotification(user));
198
+          send(new ConnectNotification(user, false, outdoor));
199 199
       }
200 200
       
201 201
       return connected;
@@ -229,7 +229,7 @@ public class NetworkClient {
229 229
 	
230 230
 	void disconnect()
231 231
 	{
232
-		send(new DisconnectNotification(user));
232
+		send(new DisconnectNotification(user, false));
233 233
 		try
234 234
 		{
235 235
 			Thread.sleep(1000);
@@ -244,16 +244,6 @@ public class NetworkClient {
244 244
 			}
245 245
 			catch(IOException e){}
246 246
 		}
247
-		for(Socket s:outdoor_dest_sockets)
248
-		{
249
-			try
250
-			{
251
-				s.close();
252
-			}
253
-			catch(IOException e){}
254
-		}
255
-		dest_sockets.clear();
256
-		outdoor_dest_sockets.clear();
257 247
 	}
258 248
 	
259 249
 	User getUser()

+ 10
- 6
src/chat/Notification.java View File

@@ -4,12 +4,8 @@ import java.io.Serializable;
4 4
 
5 5
 abstract class Notification implements Serializable{
6 6
 
7
-	private User author;
8
-	
9
-	Notification(User in_author)
10
-	{
11
-		author = in_author;
12
-	}
7
+	protected User author;
8
+	protected Boolean redirected;
13 9
 
14 10
 	public void setAuthor(User new_author)
15 11
 	{
@@ -19,4 +15,12 @@ abstract class Notification implements Serializable{
19 15
 	{
20 16
 		return author;
21 17
 	}
18
+	public Boolean isRedirected()
19
+	{
20
+		return redirected;
21
+	}
22
+	public void setRedirected(Boolean in_redirected)
23
+	{
24
+		redirected = in_redirected;
25
+	}
22 26
 }

+ 104
- 102
src/chat/ReceiveThread.java View File

@@ -37,116 +37,118 @@ class ReceiveThread extends Thread {
37 37
 	}
38 38
 	public void run()
39 39
 	{
40
-		ObjectInputStream in;
41 40
 		boolean exit = false;
42
-		while(!exit)
43
-		{
44
-			try
41
+		try
42
+    	{
43
+			while(!exit)
45 44
 			{
46
-				in = new ObjectInputStream(socket.getInputStream());
47
-		        Notification notif = (Notification) in.readObject();
48
-		        
49
-		        if(!(notif.getAuthor().equals(user)) && !(notif.getAuthor().isOutdoor() && !isOutdoor))
50
-            	{
51
-            		for(Socket s:outdoor_dest_sockets)
52
-			    	{
53
-			    		try
45
+        			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
46
+			        Notification notif = (Notification) in.readObject();
47
+			        
48
+			        if(!(notif.getAuthor().equals(user)) && !notif.isRedirected())
49
+	            	{
50
+	            		for(Socket s:outdoor_dest_sockets)
51
+				    	{
52
+				    		try
53
+				    		{
54
+				    			ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
55
+				    			notif.setRedirected(true);
56
+				    		    out.writeObject(notif);
57
+				    		    notif.setRedirected(false);
58
+				    		}
59
+				    		catch(IOException e){}
60
+				    	}
61
+	            	}
62
+			        
63
+	            	if(isOutdoor)
64
+		            {
65
+		            	for(Socket s:dest_sockets)
54 66
 			    		{
55
-			    			ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
56
-			    			
57
-			    		    out.writeObject(notif);
58
-			    		    
67
+			    			try
68
+			    			{
69
+			    				ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
70
+			    				notif.setRedirected(true);
71
+			    			    out.writeObject(notif);
72
+			    			}
73
+			    			catch(IOException e){}
59 74
 			    		}
60
-			    		catch(IOException e){}
61
-			    	}
62
-            	}
63
-		        
64
-            	if(isOutdoor)
65
-	            {
66
-	            	for(Socket s:dest_sockets)
67
-		    		{
68
-		    			try
69
-		    			{
70
-		    				ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
71
-		    				
72
-		    			    out.writeObject(notif);
73
-		    			}
74
-		    			catch(IOException e){}
75
-		    		}
76
-
77
-	            }
78
-            	else
79
-            	{
80
-            		if(notif instanceof Message)
81
-			        {
82
-			        	Message m = (Message) notif;
83
-				        displayArea.append(m.getAuthor().getName() + " : " + m.getText() + "\n");
84
-			        }
85
-		        	else if (notif instanceof DisconnectNotification)
86
-		        	{
87
-		        		DisconnectNotification dn = (DisconnectNotification) notif;
88
-		        		
89
-			            displayArea.append(dn.getAuthor().getName() + " has left the chat.\n");
90
-			            
91
-			            known_users.remove(dn.getAuthor());
92
-			            known_outdoor_users.remove(dn.getAuthor());
93
-			            
94
-			          if(!dn.getAuthor().equals(user) && !(dn.getAuthor().isOutdoor() && !isOutdoor))
95
-			            {
96
-			            	exit = true;
97
-			            	
98
-			            	socket.close();
99
-			            	if(!isOutdoor)
100
-			            	dest_sockets.remove(socket);
101
-			            	else
102
-			            	outdoor_dest_sockets.remove(socket);
103
-			            }
104
-		        	}
105
-		        	
106
-		        	else if (notif instanceof ConnectNotification)
107
-		        	{
108
-		        		ConnectNotification cn = (ConnectNotification) notif;
109
-		        		
110
-			            displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n");
111
-			            if(cn.getAuthor().isOutdoor())
75
+		            }
76
+	            	else
77
+	            	{
78
+	            		if(notif instanceof Message)
79
+				        {
80
+				        	Message m = (Message) notif;
81
+					        displayArea.append(m.getAuthor().getName() + " : " + m.getText() + "\n");
82
+				        }
83
+			        	else if (notif instanceof DisconnectNotification)
84
+			        	{
85
+			        		DisconnectNotification dn = (DisconnectNotification) notif;
86
+			        		
87
+				            displayArea.append(dn.getAuthor().getName() + " has left the chat.\n");
88
+				            
89
+				            known_users.remove(dn.getAuthor());
90
+				            known_outdoor_users.remove(dn.getAuthor());
91
+				            
92
+				            if(!dn.isRedirected() && !dn.getAuthor().equals(user))
93
+				            {
94
+				            	exit = true;
95
+				            	
96
+				            	socket.close();
97
+				            	if(!isOutdoor)
98
+				            	dest_sockets.remove(socket);
99
+				            	else
100
+				            	outdoor_dest_sockets.remove(socket);
101
+				            }
102
+			        	}
103
+			        	
104
+			        	else if (notif instanceof ConnectNotification)
105
+			        	{
106
+			        		ConnectNotification cn = (ConnectNotification) notif;
107
+			        		
108
+				            displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n");
109
+				            if(cn.isOutdoor())
110
+				            {
111
+				            	if(!known_outdoor_users.contains(cn.getAuthor()))
112
+				            	{
113
+				            		known_outdoor_users.add(cn.getAuthor());
114
+						            Collections.sort(known_outdoor_users);
115
+				            	}
116
+				            }
117
+				            else
118
+				            {
119
+				            	if(!known_users.contains(cn.getAuthor()))
120
+				            	{
121
+				            		known_users.add(cn.getAuthor());
122
+						            Collections.sort(known_users);
123
+				            	}
124
+				            }
125
+			        	}
126
+	            		knownUsersPanel.setText("");
127
+			            knownUsersPanel.append("Indoor users:\n");
128
+			            for(User a:known_users)
112 129
 			            {
113
-			            	if(!known_outdoor_users.contains(cn.getAuthor()))
114
-			            	{
115
-			            		known_outdoor_users.add(cn.getAuthor());
116
-					            Collections.sort(known_outdoor_users);
117
-			            	}
130
+			            	knownUsersPanel.append(" " + a.getName() + "  \n");
118 131
 			            }
119
-			            else
132
+			            knownUsersPanel.append("\nOutdoor users:\n");
133
+			            for(User a:known_outdoor_users)
120 134
 			            {
121
-			            	if(!known_users.contains(cn.getAuthor()))
122
-			            	{
123
-			            		known_users.add(cn.getAuthor());
124
-					            Collections.sort(known_users);
125
-			            	}
135
+			            	knownUsersPanel.append(" " + a.getName() + "  \n");
126 136
 			            }
127
-		        	}
128
-            		knownUsersPanel.setText("");
129
-		            knownUsersPanel.append("Indoor users:\n");
130
-		            for(User a:known_users)
131
-		            {
132
-		            	knownUsersPanel.append(" " + a.getName() + "  \n");
133
-		            }
134
-		            knownUsersPanel.append("\nOutdoor users:\n");
135
-		            for(User a:known_outdoor_users)
136
-		            {
137
-		            	knownUsersPanel.append(" " + a.getName() + "  \n");
138
-		            }
139
-		            displayArea.setCaretPosition(displayArea.getDocument().getLength());
140
-            	}
141
-			}
142
-			catch(Exception e)
143
-	    	{
144
-				in.reset();
145
-				e.printStackTrace();
146
-				/*System.out.println("Socket closed");
147
-	        	dest_sockets.remove(socket);
148
-	        	outdoor_dest_sockets.remove(socket);*/
149
-	    	}
137
+			            displayArea.setCaretPosition(displayArea.getDocument().getLength());
138
+	            	}
139
+		      }
140
+    	}
141
+		catch(EOFException e) {}
142
+		catch(SocketException e)
143
+		{
144
+            	if(!isOutdoor)
145
+            	dest_sockets.remove(socket);
146
+            	else
147
+            	outdoor_dest_sockets.remove(socket);
150 148
 		}
149
+		catch(Exception e)
150
+    	{
151
+    		e.printStackTrace();
152
+    	}
151 153
 	}
152 154
 }

+ 1
- 8
src/chat/User.java View File

@@ -6,14 +6,12 @@ import java.util.*;
6 6
 public class User implements Comparable<User>, Serializable{
7 7
 
8 8
 	private String name;
9
-	private Boolean outdoor;
10 9
 //	private String address;
11 10
 
12 11
 	
13
-	public User(String in_name, Boolean in_outdoor)
12
+	public User(String in_name)
14 13
 	{
15 14
 		name = in_name;
16
-		outdoor = in_outdoor;
17 15
 	//	address = in_address;
18 16
 	}
19 17
 	
@@ -25,10 +23,6 @@ public class User implements Comparable<User>, Serializable{
25 23
 	{
26 24
 		return name;
27 25
 	}
28
-	public Boolean isOutdoor()
29
-	{
30
-		return outdoor;
31
-	}
32 26
 	/*public void setAddress(String new_address)
33 27
 	{
34 28
 		name = new_address;
@@ -51,5 +45,4 @@ public class User implements Comparable<User>, Serializable{
51 45
 	{
52 46
 		return name.compareTo(otherUser.getName());
53 47
 	}
54
-	
55 48
 }

Loading…
Cancel
Save