Fix user disconnection via proxy and various other bugs

This commit is contained in:
Yohan Simard 2021-01-27 01:30:06 +01:00
parent f41c7baa8b
commit 172f9d48a3
6 changed files with 58 additions and 35 deletions

View file

@ -1,8 +1,8 @@
package fr.insa.clavardator.client.db;
import fr.insa.clavardator.client.users.CurrentUser;
import fr.insa.clavardator.lib.message.FileMessage;
import fr.insa.clavardator.lib.message.Message;
import fr.insa.clavardator.client.users.CurrentUser;
import fr.insa.clavardator.lib.users.User;
import fr.insa.clavardator.lib.users.UserInformation;
import fr.insa.clavardator.lib.util.ErrorCallback;
@ -275,8 +275,6 @@ public class DatabaseController {
chatHistory.add(new FileMessage(sender, recipient, date, text, filePath));
} catch (IOException e) {
Log.e(getClass().getSimpleName(), "Error while opening the file", e);
errorCallback.onError(e);
return;
}
}
}
@ -325,7 +323,7 @@ public class DatabaseController {
// Handle messages containing a file
String filePath = null;
if (message instanceof FileMessage) {
filePath = "'" + ((FileMessage) message).getPath() + "'";
filePath = ((FileMessage) message).getPath();
}
@Language("SQL") String sql = "INSERT INTO message " +
"(timestamp, sender, recipient, text, file_path) VALUES (?, ?, ?, ?, ?)";

View file

@ -147,7 +147,7 @@ public class PeerUser extends User implements Comparable<PeerUser> {
}
history.addMessage((Message) msg, errorCallback);
} else if (msg instanceof UsernameTakenException) {
disconnect();
CurrentUser.getInstance().setState(CurrentUser.State.INVALID);
errorCallback.onError(new Exception("Received username already taken message"));
}
}

View file

@ -7,6 +7,7 @@ import fr.insa.clavardator.client.server.Proxy;
import fr.insa.clavardator.lib.network.TcpListener;
import fr.insa.clavardator.lib.users.User;
import fr.insa.clavardator.lib.users.UserInformation;
import fr.insa.clavardator.lib.users.UserState;
import fr.insa.clavardator.lib.util.ErrorCallback;
import fr.insa.clavardator.lib.util.Log;
import javafx.application.Platform;
@ -52,17 +53,35 @@ public class UserList {
final PeerUser savedUser = userHashmap.get(userInfo.id);
if (savedUser != null) {
if (savedUser.isActive()) {
Log.v(getClass().getSimpleName(), "Received user from presence server already known and connected");
if (userInfo.getState() == UserState.DISCONNECTED) {
Log.v(getClass().getSimpleName(), "Received disconnected user from presence server already known and connected, disconnecting...");
savedUser.disconnect();
} else {
Log.v(getClass().getSimpleName(), "Received user from presence server already known and connected");
}
} else {
Log.v(getClass().getSimpleName(), "Received user from presence server already known but not connected, connecting...");
savedUser.init(proxy, userInfo.id, userInfo.getUsername(), null);
if (userInfo.getState() == UserState.CONNECTED) {
Log.v(getClass().getSimpleName(), "Received user from presence server already known but not connected, connecting...");
savedUser.init(proxy, userInfo.id, userInfo.getUsername(),
e -> Log.e(getClass().getSimpleName(), "Error with user " + userInfo.getUsername(), e));
} else {
Log.v(getClass().getSimpleName(), "Received disconnected user from presence server already known and not connected.");
}
}
} else {
Log.v(getClass().getSimpleName(), "Received new user from presence server");
final PeerUser user = new PeerUser();
user.init(proxy, userInfo.id, userInfo.getUsername(), null);
userHashmap.put(user.getId(), user);
Platform.runLater(() -> userObservableList.add(user));
if (userInfo.getState() == UserState.CONNECTED) {
Log.v(getClass().getSimpleName(), "Received new connected user from presence server");
final PeerUser user = new PeerUser();
user.init(proxy, userInfo.id, userInfo.getUsername(),
e -> Log.e(getClass().getSimpleName(), "Error with user " + userInfo.getUsername(), e));
userHashmap.put(user.getId(), user);
Platform.runLater(() -> userObservableList.add(user));
} else {
Log.v(getClass().getSimpleName(), "Received new disconnected user from presence server");
final PeerUser user = new PeerUser(userInfo.id, userInfo.getUsername());
userHashmap.put(user.getId(), user);
Platform.runLater(() -> userObservableList.add(user));
}
}
}));
}
@ -108,10 +127,13 @@ public class UserList {
final UserInformation userInfo = handshake.getUserInformation();
final PeerUser savedUser = userHashmap.get(userInfo.id);
if (savedUser != null) {
savedUser.init(handshake.getConnection(), userInfo.id, userInfo.getUsername(), null);
savedUser.init(handshake.getConnection(), userInfo.id, userInfo.getUsername(),
e -> Log.e(getClass().getSimpleName(), "Error with user " + userInfo.getUsername(), e));
} else {
final PeerUser user = new PeerUser();
user.init(handshake.getConnection(), userInfo.id, userInfo.getUsername(), null);
user.init(handshake.getConnection(), userInfo.id, userInfo.getUsername(),
e -> Log.e(getClass().getSimpleName(), "Error with user " + userInfo.getUsername(), e));
userHashmap.put(user.getId(), user);
Platform.runLater(() -> userObservableList.add(user));
}

View file

@ -32,13 +32,13 @@ public class FileMessage extends Message {
File file = new File(filePath);
if (!file.exists())
throw new IOException("The file " + filePath + " does not exist");
throw new IOException("The file " + file.getAbsolutePath() + " does not exist");
if (!file.canRead())
throw new IOException("The file " + filePath + " is not readable");
throw new IOException("The file " + file.getAbsolutePath() + " is not readable");
if (!file.isFile())
throw new IOException("The path " + filePath + " does not lead to a file");
throw new IOException("The path " + file.getAbsolutePath() + " does not lead to a file");
if (file.length() > MAX_FILE_SIZE)
throw new IOException("The file " + filePath + " is too large");
throw new IOException("The file " + file.getAbsolutePath() + " is too large");
fileName = file.getName();
path = filePath;
@ -63,12 +63,20 @@ public class FileMessage extends Message {
* @return the path to the file
*/
public String storeFile() throws IOException {
path = STORED_FILES_FOLDER + File.separatorChar + fileName;
// Create directory
// Create clavardator directory
File dir = new File(STORED_FILES_FOLDER);
dir.mkdirs();
// Copy the source file to rawFile
if (rawFile == null) {
FileInputStream istream = new FileInputStream(path);
rawFile = istream.readAllBytes();
istream.close();
}
path = STORED_FILES_FOLDER + File.separatorChar + fileName;
// Create new file
int extensionBeginning = fileName.lastIndexOf('.');
String name = fileName;
@ -84,13 +92,7 @@ public class FileMessage extends Message {
file = new File(path);
}
// write to the file
if (rawFile == null) {
FileInputStream istream = new FileInputStream(fileName);
rawFile = istream.readAllBytes();
istream.close();
}
// Write the file in clavardator dir
FileOutputStream ostream = new FileOutputStream(file);
ostream.write(rawFile);
ostream.close();

View file

@ -95,13 +95,14 @@ public class TcpConnection {
*/
public void close() {
shouldStop = true;
if (outputStream != null) {
try {
try {
if (outputStream != null)
outputStream.close();
if (inputStream != null)
inputStream.close();
socket.close();
} catch (IOException ignored) {
}
socket.close();
} catch (IOException ignored) {
}
}

View file

@ -27,9 +27,9 @@ public class Presence {
}
public void publish(UserInformation info) {
ArrayList<UserInformation> msg = new ArrayList<>(1);
msg.add(info);
for (TcpConnection subscriber : subscribers.values()) {
ArrayList<UserInformation> msg = new ArrayList<>(1);
msg.add(info);
subscriber.send(msg, null,
e -> Log.e(getClass().getSimpleName(), "Error while publishing user information", e));
}