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
* 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");
}
/**

View File

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

View File

@ -72,12 +72,15 @@ public final class LoginDialog extends Dialog<Void> {
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.<DialogPane>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<Void> {
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);
}
}

View File

@ -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));