Browse Source

outdoor users(12)

Louis Farina 11 months ago
parent
commit
972671ee8f

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

@@ -134,10 +134,10 @@ public class ClientWindow implements ActionListener {
134 134
     
135 135
     public void actionPerformed(ActionEvent event) {
136 136
     	String message = messageField.getText();
137
-    	if(message.compareTo("") != 0)
137
+    	if("".compareTo(message) != 0)
138 138
     	{
139 139
         	messageField.setText("");
140
-            network.send(message);
140
+            network.send(new Message(network.getUser(), message, false));
141 141
     	}
142 142
     }
143 143
 }

+ 16
- 0
src/chat/ConnectNotification.java View File

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

+ 136
- 0
src/chat/ConnectionListenerThread.java View File

@@ -0,0 +1,136 @@
1
+package chat;
2
+
3
+import java.net.DatagramPacket;
4
+import java.net.DatagramSocket;
5
+import java.net.InetAddress;
6
+import java.net.ServerSocket;
7
+import java.net.Socket;
8
+import java.net.SocketTimeoutException;
9
+import java.util.Collections;
10
+import java.util.List;
11
+
12
+import javax.swing.JTextArea;
13
+
14
+public class ConnectionListenerThread extends Thread {
15
+	User user;
16
+	JTextArea displayArea;
17
+	JTextArea knownUsersPanel;
18
+	List<User> known_users;
19
+	List<User> known_outdoor_users;
20
+	List<Socket> dest_sockets;
21
+	List<Socket> outdoor_dest_sockets;
22
+	int portNumber;
23
+	
24
+	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,
25
+			JTextArea in_displayArea, JTextArea in_knownUsersPanel, int in_portNumber)
26
+	{
27
+		user = in_user;
28
+		known_users = in_known_users;
29
+		known_outdoor_users = in_known_outdoor_users;
30
+		dest_sockets = in_dest_sockets;
31
+		outdoor_dest_sockets = in_outdoor_dest_sockets;
32
+		displayArea = in_displayArea;
33
+		knownUsersPanel = in_knownUsersPanel;
34
+		portNumber = in_portNumber;
35
+	}
36
+	public void run()
37
+	{
38
+		boolean exit = false;
39
+		boolean accepted = true;
40
+		boolean outdoor;
41
+		byte[] buffer = new byte[100];
42
+		DatagramPacket request = new DatagramPacket(buffer, buffer.length);
43
+		DatagramPacket responsePacket;
44
+		
45
+		DatagramSocket requestSocket;
46
+		DatagramSocket responseSocket;
47
+		
48
+		String username;
49
+		String response = "";
50
+		
51
+		byte[] responseBytes;
52
+		try
53
+		{
54
+			requestSocket = new DatagramSocket(1234);	
55
+			responseSocket = new DatagramSocket();
56
+
57
+			while(!exit)
58
+			{
59
+				try
60
+				{
61
+					System.out.println("Waiting for connection request");
62
+					requestSocket.receive(request);
63
+					username = new String(request.getData(), 0, request.getLength());
64
+					
65
+					if(username.charAt(0) == '+')
66
+					{
67
+						username = username.substring(1);
68
+						outdoor = true;
69
+					}
70
+					else
71
+					{
72
+						outdoor = false;
73
+						InetAddress clientAddress= request.getAddress();
74
+						System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
75
+						
76
+						response = "";
77
+						for(User u:known_users)
78
+						{
79
+							response += u.getName() + " ";
80
+						}
81
+						response = response.trim();
82
+						response += ";";
83
+						for(User u:known_outdoor_users)
84
+						{
85
+							response += u.getName() + " ";
86
+						}
87
+						response = response.trim();
88
+						response += ";";
89
+						for(Socket s:dest_sockets)
90
+						{
91
+							response += s.getInetAddress().getHostAddress() + " ";
92
+						}
93
+						response = response.trim();
94
+						
95
+						response += ";" + (portNumber+1);
96
+						
97
+						System.out.println("Response :" + response);
98
+						responseBytes = response.getBytes();
99
+						responsePacket = new DatagramPacket(responseBytes, responseBytes.length, clientAddress, 1337);
100
+						responseSocket.send(responsePacket);
101
+					}
102
+					accepted = true;
103
+					for(User a:known_users)
104
+					{
105
+						accepted = accepted && (!username.equals(a.getName()));
106
+					}
107
+					for(User a:known_outdoor_users)
108
+					{
109
+						accepted = accepted && (!username.equals(a.getName()));
110
+					}
111
+					if(accepted)
112
+					{
113
+						if(outdoor)
114
+						{
115
+							known_outdoor_users.add(new User(username));
116
+							Collections.sort(known_outdoor_users);
117
+						}
118
+						else
119
+						{
120
+							portNumber++;
121
+							Socket s = (new ServerSocket(portNumber)).accept();
122
+							(new ReceiveThread(user, s, displayArea, known_users, known_outdoor_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
123
+				        	dest_sockets.add(s);
124
+						}
125
+					}
126
+				}
127
+				catch(SocketTimeoutException e) {}
128
+	    	}
129
+		}
130
+		catch(Exception e)
131
+		{
132
+			e.printStackTrace();
133
+			System.out.println(e.getMessage());
134
+		}
135
+	}
136
+}

+ 9
- 0
src/chat/DisconnectNotification.java View File

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

+ 25
- 0
src/chat/ListenerStartThread.java View File

@@ -0,0 +1,25 @@
1
+package chat;
2
+
3
+import java.io.IOException;
4
+import java.net.ServerSocket;
5
+import java.net.Socket;
6
+import java.util.List;
7
+
8
+public class ListenerStartThread extends Thread {
9
+
10
+	ServerSocket s;
11
+	List<Socket> dest_sockets;
12
+	ListenerStartThread(ServerSocket in_s, List<Socket> in_dest_sockets)
13
+	{
14
+		s = in_s;
15
+		dest_sockets = in_dest_sockets;
16
+	}
17
+	public void run()
18
+	{
19
+		try
20
+		{
21
+			dest_sockets.add(s.accept());
22
+		}
23
+		catch(IOException e) {}
24
+	}
25
+}

+ 4
- 20
src/chat/Message.java View File

@@ -3,16 +3,13 @@ package chat;
3 3
 import java.io.*; 
4 4
 import java.util.*; 
5 5
 
6
-public class Message implements Serializable{
7
-
6
+public class Message extends Notification {
8 7
 	private String text;
9
-	private String author;
10
-	private Boolean redirected;
11 8
 	
12
-	public Message(String in_author, String in_text, Boolean in_redirected)
9
+	Message (User in_author, String in_text, Boolean in_redirected)
13 10
 	{
14
-		text = in_text;
15 11
 		author = in_author;
12
+		text = in_text;
16 13
 		redirected = in_redirected;
17 14
 	}
18 15
 	public void setText(String new_text)
@@ -23,17 +20,4 @@ public class Message implements Serializable{
23 20
 	{
24 21
 		return text;
25 22
 	}
26
-	public void setAuthor(String new_author)
27
-	{
28
-		author = new_author;
29
-	}
30
-	public String getAuthor()
31
-	{
32
-		return author;
33
-	}
34
-	public Boolean isRedirected()
35
-	{
36
-		return redirected;
37
-	}
38
-}
39
-
23
+}

+ 14
- 404
src/chat/NetworkClient.java View File

@@ -14,387 +14,6 @@ import java.util.*;
14 14
 import chat.User;
15 15
 import chat.Message;
16 16
 
17
-class OutdoorListenerThread extends Thread {
18
-	User user;
19
-	JTextArea displayArea;
20
-	JTextArea knownUsersPanel;
21
-	List<User> known_users;
22
-	List<User> known_outdoor_users;
23
-	List<Socket> dest_sockets;
24
-	List<Socket> outdoor_dest_sockets;
25
-	int portNumber;
26
-	
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,
28
-			JTextArea in_displayArea, JTextArea in_knownUsersPanel)
29
-	{
30
-		user = in_user;
31
-		known_users = in_known_users;
32
-		known_outdoor_users = in_known_outdoor_users;
33
-		dest_sockets = in_dest_sockets;
34
-		outdoor_dest_sockets = in_outdoor_dest_sockets;
35
-		displayArea = in_displayArea;
36
-		knownUsersPanel = in_knownUsersPanel;
37
-		portNumber = 2344;
38
-	}
39
-	public void run()
40
-	{
41
-		boolean exit = false;
42
-		boolean accepted = true;
43
-
44
-		String notification;
45
-		String username;
46
-		String response = "";
47
-		
48
-		byte[] buffer = new byte[100];
49
-		DatagramPacket notifPacket = new DatagramPacket(buffer, buffer.length);
50
-		DatagramSocket notifSocket;
51
-		byte[] notifBytes;
52
-		
53
-		try
54
-		{
55
-			while(!exit)
56
-			{
57
-				try
58
-				{
59
-					ServerSocket servSocket = new ServerSocket(1233);
60
-					System.out.println("Waiting for connection request");
61
-					Socket link = servSocket.accept();
62
-					BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
63
-					PrintWriter out = new PrintWriter(link.getOutputStream(), true);
64
-					username = in.readLine();
65
-					System.out.println("Received a request from " + username + "@" + link.getRemoteSocketAddress().toString());
66
-					
67
-					response = "";
68
-					for(User u:known_users)
69
-					{
70
-						response += u.getName() + " ";
71
-					}
72
-					response = response.trim();
73
-					response += ";";
74
-					for(User u:known_outdoor_users)
75
-					{
76
-						response += u.getName() + " ";
77
-					}
78
-					response = response.trim();
79
-					response += ";" + (portNumber+1);
80
-					
81
-					out.println(response);
82
-					
83
-					accepted = true;
84
-					for(User a:known_users)
85
-					{
86
-						accepted = accepted && (!username.equals(a.getName()));
87
-					}
88
-					for(User a:known_outdoor_users)
89
-					{
90
-						accepted = accepted && (!username.equals(a.getName()));
91
-					}
92
-					if(accepted)
93
-					{
94
-						notification = "+" + username;
95
-						notifBytes = notification.getBytes();
96
-						notifSocket = new DatagramSocket();
97
-						notifPacket = new DatagramPacket(notifBytes, notifBytes.length, InetAddress.getByName("192.168.1.255"), 1234);
98
-						notifSocket.send(notifPacket);
99
-						
100
-						portNumber++;
101
-						Socket s = (new ServerSocket(portNumber)).accept();
102
-						(new ReceiveThread(user, s, displayArea, known_users, known_outdoor_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, true)).start();
103
-			        	outdoor_dest_sockets.add(s);
104
-					}
105
-					servSocket.close();
106
-				}
107
-				catch(SocketTimeoutException e) {}
108
-	    	}
109
-		}
110
-		catch(Exception e)
111
-		{
112
-			e.printStackTrace();
113
-			System.out.println(e.getMessage());
114
-		}
115
-	}
116
-}
117
-
118
-class ReceiveThread extends Thread {
119
-	User user;
120
-	Socket socket;
121
-	JTextArea displayArea;
122
-	JTextArea knownUsersPanel;
123
-	Boolean isOutdoor;
124
-	List<User> known_users;
125
-	List<User> known_outdoor_users;
126
-	List<Socket> dest_sockets;
127
-	List<Socket> outdoor_dest_sockets;
128
-
129
-	ReceiveThread(User in_user, Socket in_socket, JTextArea in_displayArea, List<User> in_known_users,  List<User> in_known_outdoor_users,
130
-			JTextArea in_knownUsersPanel, List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets, Boolean in_isOutdoor)
131
-	{
132
-		user = in_user;
133
-		socket = in_socket;
134
-		displayArea = in_displayArea;
135
-		knownUsersPanel = in_knownUsersPanel;
136
-		known_users = in_known_users;
137
-		known_outdoor_users = in_known_outdoor_users;
138
-		dest_sockets = in_dest_sockets;
139
-		outdoor_dest_sockets = in_outdoor_dest_sockets;
140
-		isOutdoor = in_isOutdoor;
141
-	}
142
-	public void run()
143
-	{
144
-		boolean exit = false;
145
-		try
146
-    	{
147
-			while(!exit)
148
-			{
149
-        			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
150
-			        Message message = (Message) in.readObject();
151
-	            	if(isOutdoor)
152
-		            {
153
-		            	for(Socket s:dest_sockets)
154
-			    		{
155
-			    			try
156
-			    			{
157
-			    				ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
158
-			    			    out.writeObject(new Message(message.getAuthor(), message.getText(), true));
159
-			    			}
160
-			    			catch(IOException e){}
161
-			    		}
162
-		            }
163
-	            	else
164
-	            	{
165
-			        	if(message.getText() != null)
166
-			        	{
167
-				            displayArea.append(message.getAuthor() + " : " + message.getText() + "\n");
168
-			        	}
169
-			        	else
170
-			        	{
171
-				            displayArea.append(message.getAuthor() + " has left the chat.\n");
172
-				            for(int i = 0;i < known_users.size();i ++)
173
-				            {
174
-				            	if (known_users.get(i).getName().equals(message.getAuthor()))
175
-				            	{
176
-				            		known_users.remove(i);
177
-				            		break;
178
-				            		//System.out.println("Removing " + message.getAuthor());
179
-				            	}
180
-				            }
181
-				            for(int i = 0;i < known_outdoor_users.size();i ++)
182
-				            {
183
-				            	if (known_outdoor_users.get(i).getName().equals(message.getAuthor()))
184
-				            	{
185
-				            		known_outdoor_users.remove(i);
186
-				            		break;
187
-				            		//System.out.println("Removing " + message.getAuthor());
188
-				            	}
189
-				            }
190
-				            
191
-				            if(!message.isRedirected() && !message.getAuthor().equals(user.getName()))
192
-				            {
193
-				            	socket.close();
194
-				            	if(!isOutdoor)
195
-				            	dest_sockets.remove(socket);
196
-				            	else
197
-				            	outdoor_dest_sockets.remove(socket);
198
-				            }
199
-				            
200
-				            knownUsersPanel.setText("");
201
-				            knownUsersPanel.append("Indoor users:\n");
202
-				            for(User a:known_users)
203
-				            {
204
-				            	knownUsersPanel.append(" " + a.getName() + "  \n");
205
-				            }
206
-				            knownUsersPanel.append("\nOutdoor users:\n");
207
-				            for(User a:known_outdoor_users)
208
-				            {
209
-				            	knownUsersPanel.append(" " + a.getName() + "  \n");
210
-				            }
211
-			        	}
212
-			            displayArea.setCaretPosition(displayArea.getDocument().getLength());
213
-	            	}
214
-	            	if(!message.getAuthor().equals(user.getName()) && !message.isRedirected())
215
-	            	{
216
-	            		for(Socket s:outdoor_dest_sockets)
217
-				    	{
218
-				    		try
219
-				    		{
220
-				    			ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
221
-				    		    out.writeObject(new Message(message.getAuthor(), message.getText(), true));
222
-				    		}
223
-				    		catch(IOException e){}
224
-				    	}
225
-	            	}
226
-		      }
227
-    	}
228
-		catch(EOFException e) {}
229
-		catch(Exception e)
230
-    	{
231
-    		e.printStackTrace();
232
-    	}
233
-	}
234
-}
235
-
236
-class ConnectionListenerThread extends Thread {
237
-	User user;
238
-	JTextArea displayArea;
239
-	JTextArea knownUsersPanel;
240
-	List<User> known_users;
241
-	List<User> known_outdoor_users;
242
-	List<Socket> dest_sockets;
243
-	List<Socket> outdoor_dest_sockets;
244
-	int portNumber;
245
-	
246
-	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,
247
-			JTextArea in_displayArea, JTextArea in_knownUsersPanel, int in_portNumber)
248
-	{
249
-		user = in_user;
250
-		known_users = in_known_users;
251
-		known_outdoor_users = in_known_outdoor_users;
252
-		dest_sockets = in_dest_sockets;
253
-		outdoor_dest_sockets = in_outdoor_dest_sockets;
254
-		displayArea = in_displayArea;
255
-		knownUsersPanel = in_knownUsersPanel;
256
-		portNumber = in_portNumber;
257
-	}
258
-	public void run()
259
-	{
260
-		boolean exit = false;
261
-		boolean accepted = true;
262
-		boolean outdoor;
263
-		byte[] buffer = new byte[100];
264
-		DatagramPacket request = new DatagramPacket(buffer, buffer.length);
265
-		DatagramPacket responsePacket;
266
-		
267
-		DatagramSocket requestSocket;
268
-		DatagramSocket responseSocket;
269
-		
270
-		String username;
271
-		String response = "";
272
-		
273
-		byte[] responseBytes;
274
-		try
275
-		{
276
-			requestSocket = new DatagramSocket(1234);	
277
-			responseSocket = new DatagramSocket();
278
-
279
-			while(!exit)
280
-			{
281
-				try
282
-				{
283
-					System.out.println("Waiting for connection request");
284
-					requestSocket.receive(request);
285
-					username = new String(request.getData(), 0, request.getLength());
286
-					
287
-					if(username.charAt(0) == '+')
288
-					{
289
-						username = username.substring(1);
290
-						outdoor = true;
291
-					}
292
-					else
293
-					{
294
-						outdoor = false;
295
-						InetAddress clientAddress= request.getAddress();
296
-						System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
297
-						
298
-						response = "";
299
-						for(User u:known_users)
300
-						{
301
-							response += u.getName() + " ";
302
-						}
303
-						response = response.trim();
304
-						response += ";";
305
-						for(User u:known_outdoor_users)
306
-						{
307
-							response += u.getName() + " ";
308
-						}
309
-						response = response.trim();
310
-						response += ";";
311
-						for(Socket s:dest_sockets)
312
-						{
313
-							response += s.getInetAddress().getHostAddress() + " ";
314
-						}
315
-						response = response.trim();
316
-						
317
-						response += ";" + (portNumber+1);
318
-						
319
-						System.out.println("Response :" + response);
320
-						responseBytes = response.getBytes();
321
-						responsePacket = new DatagramPacket(responseBytes, responseBytes.length, clientAddress, 1337);
322
-						responseSocket.send(responsePacket);
323
-					}
324
-					accepted = true;
325
-					for(User a:known_users)
326
-					{
327
-						accepted = accepted && (!username.equals(a.getName()));
328
-					}
329
-					for(User a:known_outdoor_users)
330
-					{
331
-						accepted = accepted && (!username.equals(a.getName()));
332
-					}
333
-					if(accepted)
334
-					{
335
-						if(outdoor)
336
-						{
337
-							known_outdoor_users.add(new User(username));
338
-							Collections.sort(known_outdoor_users);
339
-						}
340
-						else
341
-						{
342
-							portNumber++;
343
-							Socket s = (new ServerSocket(portNumber)).accept();
344
-							(new ReceiveThread(user, s, displayArea, known_users, known_outdoor_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
345
-				        	dest_sockets.add(s);
346
-			        	  	
347
-							known_users.add(new User(username));
348
-							
349
-							Collections.sort(known_users);
350
-						}
351
-						
352
-			            displayArea.append(username + " has joined the chat.\n");
353
-			            displayArea.setCaretPosition(displayArea.getDocument().getLength());
354
-			            
355
-			            knownUsersPanel.setText("");
356
-			            knownUsersPanel.append("Indoor users:\n");
357
-			            for(User a:known_users)
358
-			            {
359
-			            	knownUsersPanel.append(" " + a.getName() + "  \n");
360
-			            }
361
-			            knownUsersPanel.append("\nOutdoor users:\n");
362
-			            for(User a:known_outdoor_users)
363
-			            {
364
-			            	knownUsersPanel.append(" " + a.getName() + "  \n");
365
-			            }
366
-					}
367
-				}
368
-				catch(SocketTimeoutException e) {}
369
-	    	}
370
-		}
371
-		catch(Exception e)
372
-		{
373
-			e.printStackTrace();
374
-			System.out.println(e.getMessage());
375
-		}
376
-	}
377
-}
378
-
379
-class ListenerStartThread extends Thread {
380
-
381
-	ServerSocket s;
382
-	List<Socket> dest_sockets;
383
-	ListenerStartThread(ServerSocket in_s, List<Socket> in_dest_sockets)
384
-	{
385
-		s = in_s;
386
-		dest_sockets = in_dest_sockets;
387
-	}
388
-	public void run()
389
-	{
390
-		try
391
-		{
392
-			dest_sockets.add(s.accept());
393
-		}
394
-		catch(IOException e) {}
395
-	}
396
-}
397
-
398 17
 public class NetworkClient {
399 18
 	 private User user;
400 19
 	 private List<User> known_users;
@@ -506,6 +125,10 @@ public class NetworkClient {
506 125
                 	{
507 126
                 		known_users.add(new User (u));
508 127
                 	}
128
+            		for(String u:outdoorUsernameList)
129
+                	{
130
+                		known_outdoor_users.add(new User (u));
131
+                	}
509 132
             		for(String a:addressList)
510 133
                 	{
511 134
             			Socket s = new Socket(a, portNumber);
@@ -571,32 +194,14 @@ public class NetworkClient {
571 194
           }
572 195
           
573 196
           user.setName(username);
574
-          if(!outdoor)
575
-        	  known_users.add(new User(username));
576
-          else
577
-        	 known_outdoor_users.add(new User(username));
578
-          Collections.sort(known_users);
579 197
           
580
-          chatText.append(username + " has joined the chat.\n");
581
-          chatText.setCaretPosition(chatText.getDocument().getLength());
582
-          
583
-          knownUsersPanel.setText("");
584
-          knownUsersPanel.append("Indoor users:\n");
585
-          for(User a:known_users)
586
-          {
587
-          	knownUsersPanel.append(" " + a.getName() + "  \n");
588
-          }
589
-          knownUsersPanel.append("\nOutdoor users:\n");
590
-          for(User a:known_outdoor_users)
591
-          {
592
-          	knownUsersPanel.append(" " + a.getName() + "  \n");
593
-          }
198
+          send(new ConnectNotification(user, false, outdoor));
594 199
       }
595 200
       
596 201
       return connected;
597 202
 	}
598 203
 	
599
-	void send (String message)
204
+	void send (Notification notif)
600 205
 	{
601 206
 		for(Socket s:dest_sockets)
602 207
 		{
@@ -604,7 +209,7 @@ public class NetworkClient {
604 209
 			{
605 210
 				//System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
606 211
 				ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
607
-			    out.writeObject(new Message(user.getName(), message, false));
212
+			    out.writeObject(notif);
608 213
 			}
609 214
 			catch(IOException e){}
610 215
 		}
@@ -614,7 +219,7 @@ public class NetworkClient {
614 219
     		{
615 220
     			//System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
616 221
     			ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
617
-			    out.writeObject(new Message(user.getName(), message, false));
222
+			    out.writeObject(notif);
618 223
     		}
619 224
     		catch(IOException e){}
620 225
     	}
@@ -622,7 +227,7 @@ public class NetworkClient {
622 227
 	
623 228
 	void disconnect()
624 229
 	{
625
-		send(null);
230
+		send(new DisconnectNotification(user, false));
626 231
 		try
627 232
 		{
628 233
 			Thread.sleep(1000);
@@ -638,4 +243,9 @@ public class NetworkClient {
638 243
 			catch(IOException e){}
639 244
 		}
640 245
 	}
246
+	
247
+	User getUser()
248
+	{
249
+		return user;
250
+	}
641 251
 }

+ 26
- 0
src/chat/Notification.java View File

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

+ 115
- 0
src/chat/OutdoorListenerThread.java View File

@@ -0,0 +1,115 @@
1
+package chat;
2
+
3
+import java.io.BufferedReader;
4
+import java.io.InputStreamReader;
5
+import java.io.PrintWriter;
6
+import java.net.DatagramPacket;
7
+import java.net.DatagramSocket;
8
+import java.net.InetAddress;
9
+import java.net.ServerSocket;
10
+import java.net.Socket;
11
+import java.net.SocketTimeoutException;
12
+import java.util.List;
13
+
14
+import javax.swing.JTextArea;
15
+
16
+class OutdoorListenerThread extends Thread {
17
+	User user;
18
+	JTextArea displayArea;
19
+	JTextArea knownUsersPanel;
20
+	List<User> known_users;
21
+	List<User> known_outdoor_users;
22
+	List<Socket> dest_sockets;
23
+	List<Socket> outdoor_dest_sockets;
24
+	int portNumber;
25
+	
26
+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
+			JTextArea in_displayArea, JTextArea in_knownUsersPanel)
28
+	{
29
+		user = in_user;
30
+		known_users = in_known_users;
31
+		known_outdoor_users = in_known_outdoor_users;
32
+		dest_sockets = in_dest_sockets;
33
+		outdoor_dest_sockets = in_outdoor_dest_sockets;
34
+		displayArea = in_displayArea;
35
+		knownUsersPanel = in_knownUsersPanel;
36
+		portNumber = 2344;
37
+	}
38
+	public void run()
39
+	{
40
+		boolean exit = false;
41
+		boolean accepted = true;
42
+
43
+		String notification;
44
+		String username;
45
+		String response = "";
46
+		
47
+		byte[] buffer = new byte[100];
48
+		DatagramPacket notifPacket = new DatagramPacket(buffer, buffer.length);
49
+		DatagramSocket notifSocket;
50
+		byte[] notifBytes;
51
+		
52
+		try
53
+		{
54
+			while(!exit)
55
+			{
56
+				try
57
+				{
58
+					ServerSocket servSocket = new ServerSocket(1233);
59
+					System.out.println("Waiting for connection request");
60
+					Socket link = servSocket.accept();
61
+					BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
62
+					PrintWriter out = new PrintWriter(link.getOutputStream(), true);
63
+					username = in.readLine();
64
+					System.out.println("Received a request from " + username + "@" + link.getRemoteSocketAddress().toString());
65
+					
66
+					response = "";
67
+					for(User u:known_users)
68
+					{
69
+						response += u.getName() + " ";
70
+					}
71
+					response = response.trim();
72
+					response += ";";
73
+					for(User u:known_outdoor_users)
74
+					{
75
+						response += u.getName() + " ";
76
+					}
77
+					response = response.trim();
78
+					response += ";" + (portNumber+1);
79
+					
80
+					out.println(response);
81
+					
82
+					accepted = true;
83
+					for(User a:known_users)
84
+					{
85
+						accepted = accepted && (!username.equals(a.getName()));
86
+					}
87
+					for(User a:known_outdoor_users)
88
+					{
89
+						accepted = accepted && (!username.equals(a.getName()));
90
+					}
91
+					if(accepted)
92
+					{
93
+						notification = "+" + username;
94
+						notifBytes = notification.getBytes();
95
+						notifSocket = new DatagramSocket();
96
+						notifPacket = new DatagramPacket(notifBytes, notifBytes.length, InetAddress.getByName("192.168.1.255"), 1234);
97
+						notifSocket.send(notifPacket);
98
+						
99
+						portNumber++;
100
+						Socket s = (new ServerSocket(portNumber)).accept();
101
+						(new ReceiveThread(user, s, displayArea, known_users, known_outdoor_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, true)).start();
102
+			        	outdoor_dest_sockets.add(s);
103
+					}
104
+					servSocket.close();
105
+				}
106
+				catch(SocketTimeoutException e) {}
107
+	    	}
108
+		}
109
+		catch(Exception e)
110
+		{
111
+			e.printStackTrace();
112
+			System.out.println(e.getMessage());
113
+		}
114
+	}
115
+}

+ 152
- 0
src/chat/ReceiveThread.java View File

@@ -0,0 +1,152 @@
1
+package chat;
2
+
3
+import java.io.EOFException;
4
+import java.io.IOException;
5
+import java.io.ObjectInputStream;
6
+import java.io.ObjectOutputStream;
7
+import java.net.Socket;
8
+import java.util.Collections;
9
+import java.util.List;
10
+
11
+import javax.swing.JTextArea;
12
+
13
+class ReceiveThread extends Thread {
14
+	User user;
15
+	Socket socket;
16
+	JTextArea displayArea;
17
+	JTextArea knownUsersPanel;
18
+	Boolean isOutdoor;
19
+	List<User> known_users;
20
+	List<User> known_outdoor_users;
21
+	List<Socket> dest_sockets;
22
+	List<Socket> outdoor_dest_sockets;
23
+
24
+	ReceiveThread(User in_user, Socket in_socket, JTextArea in_displayArea, List<User> in_known_users,  List<User> in_known_outdoor_users,
25
+			JTextArea in_knownUsersPanel, List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets, Boolean in_isOutdoor)
26
+	{
27
+		user = in_user;
28
+		socket = in_socket;
29
+		displayArea = in_displayArea;
30
+		knownUsersPanel = in_knownUsersPanel;
31
+		known_users = in_known_users;
32
+		known_outdoor_users = in_known_outdoor_users;
33
+		dest_sockets = in_dest_sockets;
34
+		outdoor_dest_sockets = in_outdoor_dest_sockets;
35
+		isOutdoor = in_isOutdoor;
36
+	}
37
+	public void run()
38
+	{
39
+		boolean exit = false;
40
+		try
41
+    	{
42
+			while(!exit)
43
+			{
44
+        			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
45
+			        Notification notif = (Notification) in.readObject();
46
+			        
47
+	            	if(isOutdoor)
48
+		            {
49
+		            	for(Socket s:dest_sockets)
50
+			    		{
51
+			    			try
52
+			    			{
53
+			    				ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
54
+			    				notif.setRedirected(true);
55
+			    			    out.writeObject(notif);
56
+			    			}
57
+			    			catch(IOException e){}
58
+			    		}
59
+		            }
60
+	            	else
61
+	            	{
62
+			        	if(notif instanceof Message)
63
+			        	{
64
+			        		Message m = (Message) notif;
65
+				            displayArea.append(m.getAuthor() + " : " + m.getText() + "\n");
66
+			        	}
67
+			        	else if (notif instanceof DisconnectNotification)
68
+			        	{
69
+			        		DisconnectNotification dn = (DisconnectNotification) notif;
70
+			        		
71
+				            displayArea.append(dn.getAuthor().getName() + " has left the chat.\n");
72
+				            for(int i = 0;i < known_users.size();i ++)
73
+				            {
74
+				            	if (known_users.get(i).getName().equals(dn.getAuthor()))
75
+				            	{
76
+				            		known_users.remove(i);
77
+				            		break;
78
+				            		//System.out.println("Removing " + message.getAuthor());
79
+				            	}
80
+				            }
81
+				            for(int i = 0;i < known_outdoor_users.size();i ++)
82
+				            {
83
+				            	if (known_outdoor_users.get(i).getName().equals(dn.getAuthor()))
84
+				            	{
85
+				            		known_outdoor_users.remove(i);
86
+				            		break;
87
+				            		//System.out.println("Removing " + message.getAuthor());
88
+				            	}
89
+				            }
90
+				            
91
+				            if(!dn.isRedirected() && !dn.getAuthor().equals(user.getName()))
92
+				            {
93
+				            	socket.close();
94
+				            	if(!isOutdoor)
95
+				            	dest_sockets.remove(socket);
96
+				            	else
97
+				            	outdoor_dest_sockets.remove(socket);
98
+				            }
99
+			        	}
100
+			        	
101
+			        	else if (notif instanceof ConnectNotification)
102
+			        	{
103
+			        		ConnectNotification cn = (ConnectNotification) notif;
104
+			        		
105
+				            displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n");
106
+				            if(cn.isOutdoor())
107
+				            {
108
+				            	known_outdoor_users.add(cn.getAuthor());
109
+					            Collections.sort(known_outdoor_users);
110
+				            }
111
+				            else
112
+				            {
113
+				            	known_users.add(cn.getAuthor());
114
+					            Collections.sort(known_users);
115
+				            }
116
+			        	}
117
+			        		
118
+			        	knownUsersPanel.setText("");
119
+			            knownUsersPanel.append("Indoor users:\n");
120
+			            for(User a:known_users)
121
+			            {
122
+			            	knownUsersPanel.append(" " + a.getName() + "  \n");
123
+			            }
124
+			            knownUsersPanel.append("\nOutdoor users:\n");
125
+			            for(User a:known_outdoor_users)
126
+			            {
127
+			            	knownUsersPanel.append(" " + a.getName() + "  \n");
128
+			            }
129
+			            displayArea.setCaretPosition(displayArea.getDocument().getLength());
130
+	            	}
131
+	            	if(!notif.getAuthor().equals(user.getName()) && !notif.isRedirected())
132
+	            	{
133
+	            		for(Socket s:outdoor_dest_sockets)
134
+				    	{
135
+				    		try
136
+				    		{
137
+				    			ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
138
+				    			notif.setRedirected(true);
139
+				    		    out.writeObject(notif);
140
+				    		}
141
+				    		catch(IOException e){}
142
+				    	}
143
+	            	}
144
+		      }
145
+    	}
146
+		catch(EOFException e) {}
147
+		catch(Exception e)
148
+    	{
149
+    		e.printStackTrace();
150
+    	}
151
+	}
152
+}

Loading…
Cancel
Save