From 31da9c4af6a102a8b904e50f9c88d4579a2802e7 Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 9 Jul 2020 09:12:41 +0200 Subject: [PATCH] Current working status --- src/main/java/envoy/client/net/Client.java | 80 ++++--------------- src/main/java/envoy/client/ui/Startup.java | 24 +++--- .../client/ui/controller/LoginScene.java | 77 +++++------------- 3 files changed, 48 insertions(+), 133 deletions(-) diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 7348abe..50cf9dc 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -3,7 +3,9 @@ package envoy.client.net; import java.io.Closeable; import java.io.IOException; import java.net.Socket; +import java.util.Map; import java.util.concurrent.TimeoutException; +import java.util.function.Consumer; import java.util.logging.Level; import java.util.logging.Logger; @@ -38,6 +40,8 @@ public class Client implements Closeable { private Receiver receiver; private boolean online; + private Map, Cache> cacheMap; + // Asynchronously initialized during handshake private volatile User sender; private volatile boolean rejected; @@ -53,33 +57,14 @@ public class Client implements Closeable { * will block for up to 5 seconds. If the handshake does exceed this time limit, * an exception is thrown. * - * @param credentials the login credentials of the - * user - * @param receivedMessageCache a message cache containing all - * unread messages from the server - * that can be relayed after - * initialization - * @param receivedGroupMessageCache a groupMessage cache containing - * all unread groupMessages from - * the server that can be relayed - * after initialization - * @param receivedMessageStatusChangeCache an event cache containing all - * received - * messageStatusChangeEvents from - * the server that can be relayed - * after initialization - * @param receivedGroupMessageStatusChangeCache an event cache containing all - * received - * groupMessageStatusChangeEvents - * from the server that can be - * relayed after initialization + * @param credentials the login credentials of the user + * @param cacheMap the map of all caches needed * @throws TimeoutException if the server could not be reached * @throws IOException if the login credentials could not be written * @throws InterruptedException if the current thread is interrupted while * waiting for the handshake response */ - public void performHandshake(LoginCredentials credentials, Cache receivedMessageCache, Cache receivedGroupMessageCache, - Cache receivedMessageStatusChangeCache, Cache receivedGroupMessageStatusChangeCache) + public void performHandshake(LoginCredentials credentials, Map, Cache> cacheMap) throws TimeoutException, IOException, InterruptedException { if (online) throw new IllegalStateException("Handshake has already been performed successfully"); @@ -93,10 +78,7 @@ public class Client implements Closeable { // Register user creation processor, contact list processor and message cache receiver.registerProcessor(User.class, sender -> this.sender = sender); - receiver.registerProcessor(Message.class, receivedMessageCache); - receiver.registerProcessor(GroupMessage.class, receivedGroupMessageCache); - receiver.registerProcessor(MessageStatusChange.class, receivedMessageStatusChangeCache); - receiver.registerProcessor(GroupMessageStatusChange.class, receivedGroupMessageStatusChangeCache); + cacheMap.forEach((inputclass, cache) -> receiver.registerProcessor(inputclass, cache)); receiver.registerProcessor(HandshakeRejection.class, evt -> { rejected = true; eventBus.dispatch(evt); }); rejected = false; @@ -135,43 +117,14 @@ public class Client implements Closeable { * Initializes the {@link Receiver} used to process data sent from the server to * this client. * - * @param localDB the local database used to - * persist - * the current - * {@link IDGenerator} - * @param receivedMessageCache a message cache containing all - * unread - * messages - * from the server that can be - * relayed - * after - * initialization - * @param receivedGroupMessageCache a groupMessage cache containing - * all - * unread - * groupMessages - * from the server that can be - * relayed - * after - * initialization - * @param receivedMessageStatusChangeCache an event cache containing all - * received - * messageStatusChangeEvents - * from the server that can be - * relayed - * after initialization - * @param receivedGroupMessageStatusChangeCache an event cache containing all - * received - * groupMessageStatusChangeEvents - * from the server that can be - * relayed after initialization + * @param localDB the local database used to persist the current + * {@link IDGenerator} + * @param cacheMap the map of all caches needed * @throws IOException if no {@link IDGenerator} is present and none could be * requested from the server * @since Envoy Client v0.2-alpha */ - public void initReceiver(LocalDB localDB, Cache receivedMessageCache, Cache receivedGroupMessageCache, - Cache receivedMessageStatusChangeCache, Cache receivedGroupMessageStatusChangeCache) - throws IOException { + public void initReceiver(LocalDB localDB, Map, Cache> cacheMap) throws IOException { checkOnline(); // Process incoming messages @@ -188,12 +141,11 @@ public class Client implements Closeable { receiver.registerProcessor(GroupMessageStatusChange.class, groupMessageStatusChangeProcessor); // Relay cached unread messages and unread groupMessages - receivedMessageCache.setProcessor(receivedMessageProcessor); - receivedGroupMessageCache.setProcessor(receivedGroupMessageProcessor); - + cacheMap.get(Message.class).setProcessor((Consumer) receivedMessageProcessor); + cacheMap.get(GroupMessage.class).setProcessor((Consumer) receivedGroupMessageProcessor); // Process message status changes - receivedMessageStatusChangeCache.setProcessor(messageStatusChangeProcessor); - receivedGroupMessageStatusChangeCache.setProcessor(groupMessageStatusChangeProcessor); + cacheMap.get(MessageStatusChange.class).setProcessor((Consumer) messageStatusChangeProcessor); + cacheMap.get(GroupMessageStatusChange.class).setProcessor((Consumer) groupMessageStatusChangeProcessor); // Process user status changes receiver.registerProcessor(UserStatusChange.class, eventBus::dispatch); diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 54a72d8..605501c 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -2,6 +2,7 @@ package envoy.client.ui; import java.io.File; import java.io.IOException; +import java.util.HashMap; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; @@ -42,12 +43,8 @@ public final class Startup extends Application { */ public static final String VERSION = "0.1-beta"; - private LocalDB localDB; - private Client client; - private Cache messageCache; - private Cache groupMessageCache; - private Cache messageStatusCache; - private Cache groupMessageStatusCache; + private LocalDB localDB; + private Client client; private static final ClientConfig config = ClientConfig.getInstance(); private static final Logger logger = EnvoyLog.getLogger(Startup.class); @@ -101,19 +98,20 @@ public final class Startup extends Application { } // Initialize client and unread message cache - client = new Client(); - messageCache = new Cache<>(); - groupMessageCache = new Cache<>(); - messageStatusCache = new Cache<>(); - groupMessageStatusCache = new Cache<>(); + client = new Client(); + + final var cacheMap = new HashMap, Cache>(); + cacheMap.put(Message.class, new Cache()); + cacheMap.put(GroupMessage.class, new Cache()); + cacheMap.put(MessageStatusChange.class, new Cache()); + cacheMap.put(GroupMessageStatusChange.class, new Cache()); stage.setTitle("Envoy"); stage.getIcons().add(IconUtil.loadIcon("envoy_logo")); final var sceneContext = new SceneContext(stage); sceneContext.load(SceneInfo.LOGIN_SCENE); - sceneContext.getController() - .initializeData(client, localDB, messageCache, groupMessageCache, messageStatusCache, groupMessageStatusCache, sceneContext); + sceneContext.getController().initializeData(client, localDB, cacheMap, sceneContext); } /** diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index 897cd08..98f9940 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -2,6 +2,7 @@ package envoy.client.ui.controller; import java.io.FileNotFoundException; import java.io.IOException; +import java.util.Map; import java.util.concurrent.TimeoutException; import java.util.logging.Level; import java.util.logging.Logger; @@ -16,9 +17,11 @@ import envoy.client.net.Client; import envoy.client.ui.ClearableTextField; import envoy.client.ui.SceneContext; import envoy.client.ui.Startup; -import envoy.data.*; +import envoy.data.LoginCredentials; +import envoy.data.User; import envoy.data.User.UserStatus; -import envoy.event.*; +import envoy.event.EventBus; +import envoy.event.HandshakeRejection; import envoy.exception.EnvoyException; import envoy.util.Bounds; import envoy.util.EnvoyLog; @@ -52,13 +55,10 @@ public final class LoginScene { @FXML private Label connectionLabel; - private Client client; - private LocalDB localDB; - private Cache receivedMessageCache; - private Cache receivedGroupMessageCache; - private Cache receivedMessageStatusChangeCache; - private Cache receivedGroupMessageStatusChangeCache; - private SceneContext sceneContext; + private Client client; + private LocalDB localDB; + private Map, Cache> cacheMap; + private SceneContext sceneContext; private static final Logger logger = EnvoyLog.getLogger(LoginScene.class); private static final EventBus eventBus = EventBus.getInstance(); @@ -75,41 +75,17 @@ public final class LoginScene { /** * Loads the login dialog using the FXML file {@code LoginDialog.fxml}. * - * @param client the client used to perform the - * handshake - * @param localDB the local database used for - * offline - * login - * @param receivedMessageCache the cache storing messages - * received - * during - * the handshake - * @param receivedGroupMessageCache the cache storing groupMessages - * received during the handshake - * @param receivedMessageStatusChangeCache the cache storing - * messageStatusChangeEvents - * received - * during handshake - * @param receivedGroupMessageStatusChangeCache the cache storing - * groupMessageStatusChangeEvents - * received - * during handshake - * @param sceneContext the scene context used to - * initialize - * the chat - * scene + * @param client the client used to perform the handshake + * @param localDB the local database used for offline login + * @param cacheMap the map of all caches needed + * @param sceneContext the scene context used to initialize the chat scene * @since Envoy Client v0.1-beta */ - public void initializeData(Client client, LocalDB localDB, Cache receivedMessageCache, Cache receivedGroupMessageCache, - Cache receivedMessageStatusChangeCache, Cache receivedGroupMessageStatusChangeCache, - SceneContext sceneContext) { - this.client = client; - this.localDB = localDB; - this.receivedMessageCache = receivedMessageCache; - this.receivedGroupMessageCache = receivedGroupMessageCache; - this.receivedMessageStatusChangeCache = receivedMessageStatusChangeCache; - this.receivedGroupMessageStatusChangeCache = receivedGroupMessageStatusChangeCache; - this.sceneContext = sceneContext; + public void initializeData(Client client, LocalDB localDB, Map, Cache> cacheMap, SceneContext sceneContext) { + this.client = client; + this.localDB = localDB; + this.cacheMap = cacheMap; + this.sceneContext = sceneContext; // Prepare handshake localDB.loadIDGenerator(); @@ -157,17 +133,9 @@ public final class LoginScene { private void performHandshake(LoginCredentials credentials) { try { - client.performHandshake(credentials, - receivedMessageCache, - receivedGroupMessageCache, - receivedMessageStatusChangeCache, - receivedGroupMessageStatusChangeCache); + client.performHandshake(credentials, cacheMap); if (client.isOnline()) { - client.initReceiver(localDB, - receivedMessageCache, - receivedGroupMessageCache, - receivedMessageStatusChangeCache, - receivedGroupMessageStatusChangeCache); + client.initReceiver(localDB, cacheMap); loadChatScene(); } } catch (IOException | InterruptedException | TimeoutException e) { @@ -230,9 +198,6 @@ public final class LoginScene { sceneContext.getController().initializeData(sceneContext, localDB, client, writeProxy); // Relay unread messages from cache - if (receivedMessageCache != null && client.isOnline()) receivedMessageCache.relay(); - if (receivedGroupMessageCache != null && client.isOnline()) receivedGroupMessageCache.relay(); - if (receivedMessageStatusChangeCache != null && client.isOnline()) receivedMessageStatusChangeCache.relay(); - if (receivedGroupMessageStatusChangeCache != null && client.isOnline()) receivedGroupMessageStatusChangeCache.relay(); + if (client.isOnline()) cacheMap.values().forEach(cache -> { if (cache != null) cache.relay(); }); } }