Fix user not being disconnected on local network
This commit is contained in:
parent
298e01d09c
commit
599a4a1121
5 changed files with 18 additions and 36 deletions
|
@ -2,11 +2,9 @@ package fr.insa.clavardator.client.users;
|
|||
|
||||
import fr.insa.clavardator.lib.network.TcpConnection;
|
||||
import fr.insa.clavardator.lib.util.ErrorCallback;
|
||||
import fr.insa.clavardator.lib.util.Log;
|
||||
import fr.insa.clavardator.lib.util.SimpleCallback;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class DirectPeerConnection extends PeerConnection {
|
||||
|
@ -18,35 +16,21 @@ public class DirectPeerConnection extends PeerConnection {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void send(Serializable message, SimpleCallback callback, @Nullable ErrorCallback errorCallback) {
|
||||
protected void send(Serializable message, @Nullable SimpleCallback callback, @Nullable ErrorCallback errorCallback) {
|
||||
connection.send(message, callback, errorCallback);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void receive(TcpConnection.MessageReceivedCallback callback, ErrorCallback errorCallback) {
|
||||
connection.receive(callback,
|
||||
e -> {
|
||||
disconnect();
|
||||
if (!(e instanceof EOFException)) {
|
||||
Log.e(this.getClass().getSimpleName(), "Error receiving message from " + user.getId(), e);
|
||||
errorCallback.onError(e);
|
||||
}
|
||||
});
|
||||
connection.receive(callback, errorCallback);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Close the connection to this user
|
||||
*/
|
||||
private void closeConnection() {
|
||||
@Override
|
||||
public void disconnect() {
|
||||
if (connection != null && connection.isOpen()) {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
closeConnection();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ public abstract class PeerConnection {
|
|||
this.user = user;
|
||||
}
|
||||
|
||||
protected abstract void send(Serializable message, SimpleCallback calback, @Nullable ErrorCallback errorCallback);
|
||||
protected abstract void send(Serializable message, @Nullable SimpleCallback calback, @Nullable ErrorCallback errorCallback);
|
||||
|
||||
/**
|
||||
* Subscribe to this user messages.
|
||||
|
|
|
@ -15,6 +15,7 @@ import fr.insa.clavardator.lib.util.Log;
|
|||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
@ -38,7 +39,13 @@ public class PeerUser extends User implements Comparable<PeerUser> {
|
|||
setId(id);
|
||||
setUsername(username);
|
||||
setState(UserState.CONNECTED);
|
||||
connection.receive(msg -> onMessageReceived(msg, errorCallback), errorCallback);
|
||||
connection.receive(msg -> onMessageReceived(msg, errorCallback), e -> {
|
||||
disconnect();
|
||||
if (!(e instanceof EOFException)) {
|
||||
Log.e(this.getClass().getSimpleName(), "Error receiving message from " + getId(), e);
|
||||
errorCallback.onError(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void init(TcpConnection tcpConnection, String id, String username, ErrorCallback errorCallback) {
|
||||
|
@ -116,7 +123,7 @@ public class PeerUser extends User implements Comparable<PeerUser> {
|
|||
Log.v(this.getClass().getSimpleName(), "Received username request using current username");
|
||||
UsernameTakenException exception = new UsernameTakenException("Username taken",
|
||||
CurrentUser.getInstance().getId(), getId());
|
||||
connection.send(exception, connection::disconnect, null);
|
||||
connection.send(exception, null, null);
|
||||
} else {
|
||||
Log.e(this.getClass().getSimpleName(), "Could not send UsernameTaken: connection is not initialized");
|
||||
}
|
||||
|
|
|
@ -3,11 +3,9 @@ package fr.insa.clavardator.client.users;
|
|||
import fr.insa.clavardator.client.server.Proxy;
|
||||
import fr.insa.clavardator.lib.network.TcpConnection;
|
||||
import fr.insa.clavardator.lib.util.ErrorCallback;
|
||||
import fr.insa.clavardator.lib.util.Log;
|
||||
import fr.insa.clavardator.lib.util.SimpleCallback;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class ProxyPeerConnection extends PeerConnection {
|
||||
|
@ -19,21 +17,14 @@ public class ProxyPeerConnection extends PeerConnection {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void send(Serializable message, SimpleCallback callback, @Nullable ErrorCallback errorCallback) {
|
||||
protected void send(Serializable message, @Nullable SimpleCallback callback, @Nullable ErrorCallback errorCallback) {
|
||||
proxy.send(message, callback, errorCallback);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void receive(TcpConnection.MessageReceivedCallback callback, ErrorCallback errorCallback) {
|
||||
proxy.receive(user.getId(), callback,
|
||||
e -> {
|
||||
disconnect();
|
||||
if (!(e instanceof EOFException)) {
|
||||
Log.e(this.getClass().getSimpleName(), "Error receiving message from " + user.getId(), e);
|
||||
errorCallback.onError(e);
|
||||
}
|
||||
});
|
||||
proxy.receive(user.getId(), callback, errorCallback);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -88,9 +88,9 @@ public class TcpConnection {
|
|||
outputStream.close();
|
||||
if (inputStream != null)
|
||||
inputStream.close();
|
||||
socket.close();
|
||||
if (socket != null)
|
||||
socket.close();
|
||||
} catch (IOException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue