Browse Source

outdoor users(17)

Louis Farina 3 years ago
parent
commit
6d384512a6

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, false));
140
+            network.send(new Message(network.getUser(), message));
141 141
     	}
142 142
     }
143 143
 }

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

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

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

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

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

@@ -6,11 +6,10 @@ 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, Boolean in_redirected)
9
+	Message (User in_author, String in_text)
10 10
 	{
11
-		author = in_author;
11
+		super(in_author);
12 12
 		text = in_text;
13
-		redirected = in_redirected;
14 13
 	}
15 14
 	public void setText(String new_text)
16 15
 	{

+ 16
- 6
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 = new User("");
41
+		user = null;
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));
126
+                		known_users.add(new User (u, false));
127 127
                 	}
128 128
             		for(String u:outdoorUsernameList)
129 129
                 	{
130
-                		known_outdoor_users.add(new User (u));
130
+                		known_outdoor_users.add(new User (u, true));
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.setName(username);
196
+          user = new User(username, outdoor);
197 197
           
198
-          send(new ConnectNotification(user, false, outdoor));
198
+          send(new ConnectNotification(user));
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, false));
232
+		send(new DisconnectNotification(user));
233 233
 		try
234 234
 		{
235 235
 			Thread.sleep(1000);
@@ -244,6 +244,16 @@ 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();
247 257
 	}
248 258
 	
249 259
 	User getUser()

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

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

+ 11
- 16
src/chat/ReceiveThread.java View File

@@ -45,16 +45,16 @@ class ReceiveThread extends Thread {
45 45
         			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
46 46
 			        Notification notif = (Notification) in.readObject();
47 47
 			        
48
-			        if(!(notif.getAuthor().equals(user)) && !notif.isRedirected())
48
+			        if(!(notif.getAuthor().equals(user)) && !(notif.getAuthor().isOutdoor() && !isOutdoor))
49 49
 	            	{
50 50
 	            		for(Socket s:outdoor_dest_sockets)
51 51
 				    	{
52 52
 				    		try
53 53
 				    		{
54 54
 				    			ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
55
-				    			notif.setRedirected(true);
55
+				    			
56 56
 				    		    out.writeObject(notif);
57
-				    		    notif.setRedirected(false);
57
+				    		    
58 58
 				    		}
59 59
 				    		catch(IOException e){}
60 60
 				    	}
@@ -67,11 +67,12 @@ class ReceiveThread extends Thread {
67 67
 			    			try
68 68
 			    			{
69 69
 			    				ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
70
-			    				notif.setRedirected(true);
70
+			    				
71 71
 			    			    out.writeObject(notif);
72 72
 			    			}
73 73
 			    			catch(IOException e){}
74 74
 			    		}
75
+
75 76
 		            }
76 77
 	            	else
77 78
 	            	{
@@ -89,7 +90,7 @@ class ReceiveThread extends Thread {
89 90
 				            known_users.remove(dn.getAuthor());
90 91
 				            known_outdoor_users.remove(dn.getAuthor());
91 92
 				            
92
-				            if(!dn.isRedirected() && !dn.getAuthor().equals(user))
93
+				          /*if(!dn.getAuthor().equals(user) && !(dn.getAuthor().isOutdoor() && !isOutdoor))
93 94
 				            {
94 95
 				            	exit = true;
95 96
 				            	
@@ -98,7 +99,7 @@ class ReceiveThread extends Thread {
98 99
 				            	dest_sockets.remove(socket);
99 100
 				            	else
100 101
 				            	outdoor_dest_sockets.remove(socket);
101
-				            }
102
+				            }*/
102 103
 			        	}
103 104
 			        	
104 105
 			        	else if (notif instanceof ConnectNotification)
@@ -106,7 +107,7 @@ class ReceiveThread extends Thread {
106 107
 			        		ConnectNotification cn = (ConnectNotification) notif;
107 108
 			        		
108 109
 				            displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n");
109
-				            if(cn.isOutdoor())
110
+				            if(cn.getAuthor().isOutdoor())
110 111
 				            {
111 112
 				            	if(!known_outdoor_users.contains(cn.getAuthor()))
112 113
 				            	{
@@ -138,17 +139,11 @@ class ReceiveThread extends Thread {
138 139
 	            	}
139 140
 		      }
140 141
     	}
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);
148
-		}
149 142
 		catch(Exception e)
150 143
     	{
151
-    		e.printStackTrace();
144
+			System.out.println("Socket closed");
145
+        	dest_sockets.remove(socket);
146
+        	outdoor_dest_sockets.remove(socket);
152 147
     	}
153 148
 	}
154 149
 }

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

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

Loading…
Cancel
Save