Fix offline mode and local DB initialization

This commit is contained in:
Kai S. K. Engelbart 2020-06-07 16:26:54 +02:00
parent cd4f01543e
commit 1689896f42
4 changed files with 18 additions and 18 deletions

View File

@ -40,18 +40,18 @@ public class PersistentLocalDB extends LocalDB {
* Constructs an empty local database. To serialize any chats to the file * Constructs an empty local database. To serialize any chats to the file
* system, call {@link PersistentLocalDB#initializeUserStorage()}. * 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 * @throws IOException if the PersistentLocalDB could not be initialized
* @since Envoy Client v0.1-alpha * @since Envoy Client v0.1-alpha
*/ */
public PersistentLocalDB(File localDbDir) throws IOException { public PersistentLocalDB(File localDBDir) throws IOException {
localDBDir = localDbDir; this.localDBDir = localDBDir;
// Initialize local database directory // Initialize local database directory
if (localDbDir.exists() && !localDbDir.isDirectory()) if (localDBDir.exists() && !localDBDir.isDirectory())
throw new IOException(String.format("LocalDbDir '%s' is not a directory!", localDbDir.getAbsolutePath())); throw new IOException(String.format("LocalDbDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
usersFile = new File(localDbDir, "users.db"); usersFile = new File(localDBDir, "users.db");
idGeneratorFile = new File(localDbDir, "id_generator.db"); idGeneratorFile = new File(localDBDir, "id_generator.db");
} }
/** /**

View File

@ -223,6 +223,7 @@ public class Client implements Closeable {
checkOnline(); checkOnline();
Map<String, Contact> users = new HashMap<>(); Map<String, Contact> users = new HashMap<>();
contacts.forEach(u -> users.put(u.getName(), u)); contacts.forEach(u -> users.put(u.getName(), u));
users.put(sender.getName(), sender);
return users; return users;
} }

View File

@ -72,12 +72,15 @@ public final class LoginDialog extends Dialog<Void> {
this.localDB = localDB; this.localDB = localDB;
this.receivedMessageCache = receivedMessageCache; this.receivedMessageCache = receivedMessageCache;
// Prepare handshake
localDB.loadIDGenerator();
final var loader = new FXMLLoader(getClass().getResource("/fxml/LoginDialog.fxml")); final var loader = new FXMLLoader(getClass().getResource("/fxml/LoginDialog.fxml"));
loader.setController(this); loader.setController(this);
final var dialogPane = loader.<DialogPane>load(); final var dialogPane = loader.<DialogPane>load();
((Stage) getDialogPane().getScene().getWindow()).getIcons().add(IconUtil.load("/icons/envoy_logo.png")); ((Stage) getDialogPane().getScene().getWindow()).getIcons().add(IconUtil.load("/icons/envoy_logo.png"));
// Configure dialog buttons // Configure dialog buttons
dialogPane.getButtonTypes().addAll(ButtonType.CLOSE, ButtonType.OK); dialogPane.getButtonTypes().addAll(ButtonType.CLOSE, ButtonType.OK);
@ -138,22 +141,18 @@ public final class LoginDialog extends Dialog<Void> {
Platform.runLater(this::hide); Platform.runLater(this::hide);
} }
} catch (IOException | InterruptedException | TimeoutException e) { } catch (IOException | InterruptedException | TimeoutException e) {
logger.warning("Could not connect to server. Trying offline mode..."); logger.warning("Could not connect to server: " + e);
e.printStackTrace(); logger.finer("Attempting offline mode...");
try { try {
// Try entering offline mode // Try entering offline mode
localDB.loadUsers(); localDB.loadUsers();
User clientUser = (User) localDB.getUsers().get(credentials.getIdentifier()); User clientUser = (User) localDB.getUsers().get(credentials.getIdentifier());
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown"); if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
client.setSender(clientUser); 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();
new Alert(AlertType.WARNING, "A connection to the server could not be established. Starting in offline mode.\n" hide();
+ e)
.showAndWait();
hide();
});
} catch (Exception e1) { } 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); System.exit(1);
} }
} }

View File

@ -115,7 +115,7 @@ public final class Startup extends Application {
localDB.getUsers() localDB.getUsers()
.values() .values()
.stream() .stream()
.filter(u -> u instanceof User && u != localDB.getUser()) .filter(User.class::isInstance)
.map(User.class::cast) .map(User.class::cast)
.forEach(u -> u.setStatus(UserStatus.OFFLINE)); .forEach(u -> u.setStatus(UserStatus.OFFLINE));