Performing handshake and online init in LoginDialog

This commit is contained in:
2020-02-12 17:31:20 +01:00
parent 17eeed0bfb
commit 8714c8fe0e
3 changed files with 205 additions and 180 deletions

View File

@ -15,9 +15,7 @@ import envoy.client.data.*;
import envoy.client.net.Client;
import envoy.client.net.WriteProxy;
import envoy.client.util.EnvoyLog;
import envoy.data.LoginCredentials;
import envoy.data.Message;
import envoy.data.User;
import envoy.exception.EnvoyException;
/**
@ -74,14 +72,6 @@ public class Startup {
EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier());
EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier());
// Acquire login credentials
LoginCredentials credentials = config.hasLoginCredentials() ? config.getLoginCredentials() : new LoginDialog().getCredentials();
if (credentials == null) {
logger.info("The login process has been cancelled. Exiting...");
System.exit(0);
}
// Initialize the local database
LocalDb localDb;
if (config.isIgnoreLocalDB()) {
@ -95,45 +85,23 @@ public class Startup {
} catch (IOException e3) {
logger.log(Level.SEVERE, "Could not initialize local database", e3);
JOptionPane
.showMessageDialog(null, "Could not initialize local database!\n" + e3.toString(), "Local database error", JOptionPane.ERROR_MESSAGE);
.showMessageDialog(null, "Could not initialize local database!\n" + e3, "Local database error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
return;
}
SwingUtilities.invokeLater(() -> chatWindow.setVisible(true));
// Acquire the client user (with ID) either from the server or from the local
// database, which triggers offline mode
// Initialize client and unread message cache
Client client = new Client();
Cache<Message> cache = new Cache<>();
try {
// Try entering online mode first
localDb.loadIdGenerator();
client.performHandshake(credentials, cache);
client.initReceiver(localDb, cache);
} catch (Exception e1) {
logger.warning("Could not connect to server. Trying offline mode...");
e1.printStackTrace();
try {
// Try entering offline mode
localDb.loadUsers();
User clientUser = localDb.getUsers().get(credentials.getIdentifier());
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
client.setSender(clientUser);
JOptionPane.showMessageDialog(null,
"A connection to the server could not be established. Starting in offline mode.\n" + e1,
"Connection error",
JOptionPane.WARNING_MESSAGE);
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, e2.toString(), "Client error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
return;
}
}
// Try to connect to the server
new LoginDialog(client, localDb, cache);
SwingUtilities.invokeLater(() -> chatWindow.setVisible(true));
// Set client user in local database
localDb.setUser(client.getSender());
// Initialize chats in local database
try {
localDb.initializeUserStorage();