From 1689896f42a79563e1c3765a8eeb961cb85654b5 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 7 Jun 2020 16:26:54 +0200 Subject: [PATCH] Fix offline mode and local DB initialization --- .../envoy/client/data/PersistentLocalDB.java | 14 +++++++------- src/main/java/envoy/client/net/Client.java | 1 + .../java/envoy/client/ui/LoginDialog.java | 19 +++++++++---------- src/main/java/envoy/client/ui/Startup.java | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/java/envoy/client/data/PersistentLocalDB.java b/src/main/java/envoy/client/data/PersistentLocalDB.java index a4fe390..66bf167 100644 --- a/src/main/java/envoy/client/data/PersistentLocalDB.java +++ b/src/main/java/envoy/client/data/PersistentLocalDB.java @@ -40,18 +40,18 @@ public class PersistentLocalDB extends LocalDB { * Constructs an empty local database. To serialize any chats to the file * system, call {@link PersistentLocalDB#initializeUserStorage()}. * - * @param localDbDir the directory in which to store users and chats + * @param localDBDir the directory in which to store users and chats * @throws IOException if the PersistentLocalDB could not be initialized * @since Envoy Client v0.1-alpha */ - public PersistentLocalDB(File localDbDir) throws IOException { - localDBDir = localDbDir; + public PersistentLocalDB(File localDBDir) throws IOException { + this.localDBDir = localDBDir; // Initialize local database directory - if (localDbDir.exists() && !localDbDir.isDirectory()) - throw new IOException(String.format("LocalDbDir '%s' is not a directory!", localDbDir.getAbsolutePath())); - usersFile = new File(localDbDir, "users.db"); - idGeneratorFile = new File(localDbDir, "id_generator.db"); + if (localDBDir.exists() && !localDBDir.isDirectory()) + throw new IOException(String.format("LocalDbDir '%s' is not a directory!", localDBDir.getAbsolutePath())); + usersFile = new File(localDBDir, "users.db"); + idGeneratorFile = new File(localDBDir, "id_generator.db"); } /** diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 00a6ddf..ce9b3a6 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -223,6 +223,7 @@ public class Client implements Closeable { checkOnline(); Map users = new HashMap<>(); contacts.forEach(u -> users.put(u.getName(), u)); + users.put(sender.getName(), sender); return users; } diff --git a/src/main/java/envoy/client/ui/LoginDialog.java b/src/main/java/envoy/client/ui/LoginDialog.java index cba70c9..4c52b0c 100644 --- a/src/main/java/envoy/client/ui/LoginDialog.java +++ b/src/main/java/envoy/client/ui/LoginDialog.java @@ -72,12 +72,15 @@ public final class LoginDialog extends Dialog { this.localDB = localDB; this.receivedMessageCache = receivedMessageCache; + // Prepare handshake + localDB.loadIDGenerator(); + final var loader = new FXMLLoader(getClass().getResource("/fxml/LoginDialog.fxml")); loader.setController(this); final var dialogPane = loader.load(); ((Stage) getDialogPane().getScene().getWindow()).getIcons().add(IconUtil.load("/icons/envoy_logo.png")); - + // Configure dialog buttons dialogPane.getButtonTypes().addAll(ButtonType.CLOSE, ButtonType.OK); @@ -138,22 +141,18 @@ public final class LoginDialog extends Dialog { Platform.runLater(this::hide); } } catch (IOException | InterruptedException | TimeoutException e) { - logger.warning("Could not connect to server. Trying offline mode..."); - e.printStackTrace(); + logger.warning("Could not connect to server: " + e); + logger.finer("Attempting offline mode..."); try { // Try entering offline mode localDB.loadUsers(); User clientUser = (User) localDB.getUsers().get(credentials.getIdentifier()); if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown"); client.setSender(clientUser); - Platform.runLater(() -> { - new Alert(AlertType.WARNING, "A connection to the server could not be established. Starting in offline mode.\n" - + e) - .showAndWait(); - hide(); - }); + new Alert(AlertType.WARNING, "A connection to the server could not be established. Starting in offline mode.\n" + e).showAndWait(); + hide(); } catch (Exception e1) { - Platform.runLater(() -> new Alert(AlertType.ERROR, "Client error: " + e.toString()).showAndWait()); + new Alert(AlertType.ERROR, "Client error: " + e).showAndWait(); System.exit(1); } } diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index fa9fac0..1b5397d 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -115,7 +115,7 @@ public final class Startup extends Application { localDB.getUsers() .values() .stream() - .filter(u -> u instanceof User && u != localDB.getUser()) + .filter(User.class::isInstance) .map(User.class::cast) .forEach(u -> u.setStatus(UserStatus.OFFLINE));