2 Commits

Author SHA1 Message Date
  Louis Farina 9d55a5700e outdoor users(16) 11 months ago
  Louis Farina b03bb2a335 outdoor users(15) 11 months ago

BIN
Projet.jar View File


+ 29
- 44
src/chat/ConnectionListenerThread.java View File

@@ -62,43 +62,36 @@ public class ConnectionListenerThread extends Thread {
62 62
 					requestSocket.receive(request);
63 63
 					username = new String(request.getData(), 0, request.getLength());
64 64
 					
65
-					if(username.charAt(0) == '+')
65
+
66
+					InetAddress clientAddress= request.getAddress();
67
+					System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
68
+						
69
+					response = "";
70
+					for(User u:known_users)
66 71
 					{
67
-						username = username.substring(1);
68
-						outdoor = true;
72
+						response += u.getName() + " ";
69 73
 					}
70
-					else
74
+					response = response.trim();
75
+					response += ";";
76
+					for(User u:known_outdoor_users)
71 77
 					{
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();
78
+						response += u.getName() + " ";
79
+					}
80
+					response = response.trim();
81
+					response += ";";
82
+					for(Socket s:dest_sockets)
83
+					{
84
+						response += s.getInetAddress().getHostAddress() + " ";
85
+					}
86
+					response = response.trim();
94 87
 						
95
-						response += ";" + (portNumber+1);
88
+					response += ";" + (portNumber+1);
96 89
 						
97
-						System.out.println("Response :" + response);
98
-						responseBytes = response.getBytes();
99
-						responsePacket = new DatagramPacket(responseBytes, responseBytes.length, clientAddress, 1337);
100
-						responseSocket.send(responsePacket);
101
-					}
90
+					System.out.println("Response :" + response);
91
+					responseBytes = response.getBytes();
92
+					responsePacket = new DatagramPacket(responseBytes, responseBytes.length, clientAddress, 1337);
93
+					responseSocket.send(responsePacket);
94
+					
102 95
 					accepted = true;
103 96
 					for(User a:known_users)
104 97
 					{
@@ -110,18 +103,10 @@ public class ConnectionListenerThread extends Thread {
110 103
 					}
111 104
 					if(accepted)
112 105
 					{
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
-						}
106
+						portNumber++;
107
+						Socket s = (new ServerSocket(portNumber)).accept();
108
+						(new ReceiveThread(user, s, displayArea, known_users, known_outdoor_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
109
+				        dest_sockets.add(s);
125 110
 					}
126 111
 				}
127 112
 				catch(SocketTimeoutException e) {}

+ 4
- 2
src/chat/NetworkClient.java View File

@@ -203,21 +203,23 @@ public class NetworkClient {
203 203
 	
204 204
 	void send (Notification notif)
205 205
 	{
206
+		System.out.println("Indoor users:");
206 207
 		for(Socket s:dest_sockets)
207 208
 		{
208 209
 			try
209 210
 			{
210
-				//System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
211
+				System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
211 212
 				ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
212 213
 			    out.writeObject(notif);
213 214
 			}
214 215
 			catch(IOException e){}
215 216
 		}
217
+		System.out.println("Outdoor users:");
216 218
 		for(Socket s:outdoor_dest_sockets)
217 219
     	{
218 220
     		try
219 221
     		{
220
-    			//System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
222
+    			System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
221 223
     			ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
222 224
 			    out.writeObject(notif);
223 225
     		}

+ 5
- 17
src/chat/OutdoorListenerThread.java View File

@@ -21,7 +21,7 @@ class OutdoorListenerThread extends Thread {
21 21
 	List<User> known_outdoor_users;
22 22
 	List<Socket> dest_sockets;
23 23
 	List<Socket> outdoor_dest_sockets;
24
-	int portNumber;
24
+	int outdoorPortNumber;
25 25
 	
26 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 27
 			JTextArea in_displayArea, JTextArea in_knownUsersPanel)
@@ -33,22 +33,16 @@ OutdoorListenerThread(User in_user, List<User> in_known_users,  List<User> in_kn
33 33
 		outdoor_dest_sockets = in_outdoor_dest_sockets;
34 34
 		displayArea = in_displayArea;
35 35
 		knownUsersPanel = in_knownUsersPanel;
36
-		portNumber = 2344;
36
+		outdoorPortNumber = 2344;
37 37
 	}
38 38
 	public void run()
39 39
 	{
40 40
 		boolean exit = false;
41 41
 		boolean accepted = true;
42 42
 
43
-		String notification;
44 43
 		String username;
45 44
 		String response = "";
46 45
 		
47
-		byte[] buffer = new byte[100];
48
-		DatagramPacket notifPacket = new DatagramPacket(buffer, buffer.length);
49
-		DatagramSocket notifSocket;
50
-		byte[] notifBytes;
51
-		
52 46
 		try
53 47
 		{
54 48
 			while(!exit)
@@ -75,7 +69,7 @@ OutdoorListenerThread(User in_user, List<User> in_known_users,  List<User> in_kn
75 69
 						response += u.getName() + " ";
76 70
 					}
77 71
 					response = response.trim();
78
-					response += ";" + (portNumber+1);
72
+					response += ";" + (outdoorPortNumber+1);
79 73
 					
80 74
 					out.println(response);
81 75
 					
@@ -90,14 +84,8 @@ OutdoorListenerThread(User in_user, List<User> in_known_users,  List<User> in_kn
90 84
 					}
91 85
 					if(accepted)
92 86
 					{
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();
87
+						outdoorPortNumber++;
88
+						Socket s = (new ServerSocket(outdoorPortNumber)).accept();
101 89
 						(new ReceiveThread(user, s, displayArea, known_users, known_outdoor_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, true)).start();
102 90
 			        	outdoor_dest_sockets.add(s);
103 91
 					}

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

@@ -1,6 +1,7 @@
1 1
 package chat;
2 2
 
3 3
 import java.io.EOFException;
4
+import java.net.SocketException;
4 5
 import java.io.IOException;
5 6
 import java.io.ObjectInputStream;
6 7
 import java.io.ObjectOutputStream;
@@ -46,7 +47,6 @@ class ReceiveThread extends Thread {
46 47
 			        
47 48
 			        if(!(notif.getAuthor().equals(user)) && !notif.isRedirected())
48 49
 	            	{
49
-	            		System.out.println("Redirecting message");
50 50
 	            		for(Socket s:outdoor_dest_sockets)
51 51
 				    	{
52 52
 				    		try
@@ -54,6 +54,7 @@ class ReceiveThread extends Thread {
54 54
 				    			ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
55 55
 				    			notif.setRedirected(true);
56 56
 				    		    out.writeObject(notif);
57
+				    		    notif.setRedirected(false);
57 58
 				    		}
58 59
 				    		catch(IOException e){}
59 60
 				    	}
@@ -90,6 +91,8 @@ class ReceiveThread extends Thread {
90 91
 				            
91 92
 				            if(!dn.isRedirected() && !dn.getAuthor().equals(user))
92 93
 				            {
94
+				            	exit = true;
95
+				            	
93 96
 				            	socket.close();
94 97
 				            	if(!isOutdoor)
95 98
 				            	dest_sockets.remove(socket);
@@ -136,6 +139,13 @@ class ReceiveThread extends Thread {
136 139
 		      }
137 140
     	}
138 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
+		}
139 149
 		catch(Exception e)
140 150
     	{
141 151
     		e.printStackTrace();

Loading…
Cancel
Save