Browse Source

Fix user not being disconnected on local network

Yohan Simard 3 years ago
parent
commit
599a4a1121

+ 4
- 20
client/src/main/java/fr/insa/clavardator/client/users/DirectPeerConnection.java View File

@@ -2,11 +2,9 @@ package fr.insa.clavardator.client.users;
2 2
 
3 3
 import fr.insa.clavardator.lib.network.TcpConnection;
4 4
 import fr.insa.clavardator.lib.util.ErrorCallback;
5
-import fr.insa.clavardator.lib.util.Log;
6 5
 import fr.insa.clavardator.lib.util.SimpleCallback;
7 6
 import org.jetbrains.annotations.Nullable;
8 7
 
9
-import java.io.EOFException;
10 8
 import java.io.Serializable;
11 9
 
12 10
 public class DirectPeerConnection extends PeerConnection {
@@ -18,35 +16,21 @@ public class DirectPeerConnection extends PeerConnection {
18 16
 	}
19 17
 
20 18
 	@Override
21
-	protected void send(Serializable message, SimpleCallback callback, @Nullable ErrorCallback errorCallback) {
19
+	protected void send(Serializable message, @Nullable SimpleCallback callback, @Nullable ErrorCallback errorCallback) {
22 20
 		connection.send(message, callback, errorCallback);
23 21
 	}
24 22
 
25 23
 
26 24
 	@Override
27 25
 	protected void receive(TcpConnection.MessageReceivedCallback callback, ErrorCallback errorCallback) {
28
-		connection.receive(callback,
29
-				e -> {
30
-					disconnect();
31
-					if (!(e instanceof EOFException)) {
32
-						Log.e(this.getClass().getSimpleName(), "Error receiving message from " + user.getId(), e);
33
-						errorCallback.onError(e);
34
-					}
35
-				});
26
+		connection.receive(callback, errorCallback);
36 27
 	}
37 28
 
38 29
 
39
-	/**
40
-	 * Close the connection to this user
41
-	 */
42
-	private void closeConnection() {
30
+	@Override
31
+	public void disconnect() {
43 32
 		if (connection != null && connection.isOpen()) {
44 33
 			connection.close();
45 34
 		}
46 35
 	}
47
-
48
-	@Override
49
-	public void disconnect() {
50
-		closeConnection();
51
-	}
52 36
 }

+ 1
- 1
client/src/main/java/fr/insa/clavardator/client/users/PeerConnection.java View File

@@ -14,7 +14,7 @@ public abstract class PeerConnection {
14 14
 		this.user = user;
15 15
 	}
16 16
 
17
-	protected abstract void send(Serializable message, SimpleCallback calback, @Nullable ErrorCallback errorCallback);
17
+	protected abstract void send(Serializable message, @Nullable SimpleCallback calback, @Nullable ErrorCallback errorCallback);
18 18
 
19 19
 	/**
20 20
 	 * Subscribe to this user messages.

+ 9
- 2
client/src/main/java/fr/insa/clavardator/client/users/PeerUser.java View File

@@ -15,6 +15,7 @@ import fr.insa.clavardator.lib.util.Log;
15 15
 import org.jetbrains.annotations.NotNull;
16 16
 import org.jetbrains.annotations.Nullable;
17 17
 
18
+import java.io.EOFException;
18 19
 import java.io.File;
19 20
 import java.io.IOException;
20 21
 import java.util.Date;
@@ -38,7 +39,13 @@ public class PeerUser extends User implements Comparable<PeerUser> {
38 39
 		setId(id);
39 40
 		setUsername(username);
40 41
 		setState(UserState.CONNECTED);
41
-		connection.receive(msg -> onMessageReceived(msg, errorCallback), errorCallback);
42
+		connection.receive(msg -> onMessageReceived(msg, errorCallback), e -> {
43
+			disconnect();
44
+			if (!(e instanceof EOFException)) {
45
+				Log.e(this.getClass().getSimpleName(), "Error receiving message from " + getId(), e);
46
+				errorCallback.onError(e);
47
+			}
48
+		});
42 49
 	}
43 50
 
44 51
 	public void init(TcpConnection tcpConnection, String id, String username, ErrorCallback errorCallback) {
@@ -116,7 +123,7 @@ public class PeerUser extends User implements Comparable<PeerUser> {
116 123
 			Log.v(this.getClass().getSimpleName(), "Received username request using current username");
117 124
 			UsernameTakenException exception = new UsernameTakenException("Username taken",
118 125
 					CurrentUser.getInstance().getId(), getId());
119
-			connection.send(exception, connection::disconnect, null);
126
+			connection.send(exception, null, null);
120 127
 		} else {
121 128
 			Log.e(this.getClass().getSimpleName(), "Could not send UsernameTaken: connection is not initialized");
122 129
 		}

+ 2
- 11
client/src/main/java/fr/insa/clavardator/client/users/ProxyPeerConnection.java View File

@@ -3,11 +3,9 @@ package fr.insa.clavardator.client.users;
3 3
 import fr.insa.clavardator.client.server.Proxy;
4 4
 import fr.insa.clavardator.lib.network.TcpConnection;
5 5
 import fr.insa.clavardator.lib.util.ErrorCallback;
6
-import fr.insa.clavardator.lib.util.Log;
7 6
 import fr.insa.clavardator.lib.util.SimpleCallback;
8 7
 import org.jetbrains.annotations.Nullable;
9 8
 
10
-import java.io.EOFException;
11 9
 import java.io.Serializable;
12 10
 
13 11
 public class ProxyPeerConnection extends PeerConnection {
@@ -19,21 +17,14 @@ public class ProxyPeerConnection extends PeerConnection {
19 17
 	}
20 18
 
21 19
 	@Override
22
-	protected void send(Serializable message, SimpleCallback callback, @Nullable ErrorCallback errorCallback) {
20
+	protected void send(Serializable message, @Nullable SimpleCallback callback, @Nullable ErrorCallback errorCallback) {
23 21
 		proxy.send(message, callback, errorCallback);
24 22
 	}
25 23
 
26 24
 
27 25
 	@Override
28 26
 	protected void receive(TcpConnection.MessageReceivedCallback callback, ErrorCallback errorCallback) {
29
-		proxy.receive(user.getId(), callback,
30
-				e -> {
31
-					disconnect();
32
-					if (!(e instanceof EOFException)) {
33
-						Log.e(this.getClass().getSimpleName(), "Error receiving message from " + user.getId(), e);
34
-						errorCallback.onError(e);
35
-					}
36
-				});
27
+		proxy.receive(user.getId(), callback, errorCallback);
37 28
 	}
38 29
 
39 30
 	@Override

+ 2
- 2
lib/src/main/java/fr/insa/clavardator/lib/network/TcpConnection.java View File

@@ -88,9 +88,9 @@ public class TcpConnection {
88 88
 				outputStream.close();
89 89
 			if (inputStream != null)
90 90
 				inputStream.close();
91
-			socket.close();
91
+			if (socket != null)
92
+				socket.close();
92 93
 		} catch (IOException ignored) {
93
-
94 94
 		}
95 95
 	}
96 96
 

Loading…
Cancel
Save