From c2bbb1a2241a711f2c91ce50974e5c6da6f7b836 Mon Sep 17 00:00:00 2001 From: kske Date: Mon, 30 Nov 2020 22:19:45 +0100 Subject: [PATCH] Send pending messages after successful handshake Instead of caching pending messages during the handshake and relaying them afterwards, they are now sent after the handshake has been completed. This is possible because the relevant processors (messages and status changes) are now event handlers which are registered at the event bus, which means that they can immediately react to pending messages even if Client#initReceiver has not been fully executed yet. Because Client#initReceiver exists for that very reason, it is now longer necessary. ID generator initialization, which was its other part, is now directly handled in Startup#performHandshake, which is a far more sensible placement. Fixes #106 --- .../main/java/envoy/client/net/Client.java | 40 ++++--------------- .../main/java/envoy/client/ui/Startup.java | 15 ++++--- .../processors/LoginCredentialProcessor.java | 31 ++++++++------ 3 files changed, 33 insertions(+), 53 deletions(-) diff --git a/client/src/main/java/envoy/client/net/Client.java b/client/src/main/java/envoy/client/net/Client.java index fe1c9e2..d036341 100644 --- a/client/src/main/java/envoy/client/net/Client.java +++ b/client/src/main/java/envoy/client/net/Client.java @@ -12,7 +12,7 @@ import envoy.data.*; import envoy.event.*; import envoy.util.*; -import envoy.client.data.*; +import envoy.client.data.ClientConfig; import envoy.client.event.EnvoyCloseEvent; /** @@ -55,13 +55,12 @@ public final class Client implements EventListener, Closeable { * the handshake does exceed this time limit, an exception is thrown. * * @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, CacheMap cacheMap) + public void performHandshake(LoginCredentials credentials) throws TimeoutException, IOException, InterruptedException { if (online) throw new IllegalStateException("Handshake has already been performed successfully"); @@ -79,7 +78,6 @@ public final class Client implements EventListener, Closeable { // Register user creation processor, contact list processor, message cache and // authentication token receiver.registerProcessor(User.class, sender -> this.sender = sender); - receiver.registerProcessors(cacheMap.getMap()); // Start receiver receiver.start(); @@ -101,42 +99,18 @@ public final class Client implements EventListener, Closeable { if (System.currentTimeMillis() - start > 5000) { rejected = true; + socket.close(); + receiver.removeAllProcessors(); throw new TimeoutException("Did not log in after 5 seconds"); } Thread.sleep(500); } - online = true; - logger.log(Level.INFO, "Handshake completed."); - } - - /** - * 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 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, CacheMap cacheMap) throws IOException { - checkOnline(); - - // Remove all processors as they are only used during the handshake + // Remove handshake specific processors receiver.removeAllProcessors(); - // Relay cached messages and message status changes - cacheMap.get(Message.class).setProcessor(eventBus::dispatch); - cacheMap.get(GroupMessage.class).setProcessor(eventBus::dispatch); - cacheMap.get(MessageStatusChange.class).setProcessor(eventBus::dispatch); - cacheMap.get(GroupMessageStatusChange.class).setProcessor(eventBus::dispatch); - - // Request a generator if none is present or the existing one is consumed - if (!localDB.hasIDGenerator() || !localDB.getIDGenerator().hasNext()) - requestIDGenerator(); - - // Relay caches - cacheMap.getMap().values().forEach(Cache::relay); + online = true; + logger.log(Level.INFO, "Handshake completed."); } /** diff --git a/client/src/main/java/envoy/client/ui/Startup.java b/client/src/main/java/envoy/client/ui/Startup.java index ddbe0ef..08a0a95 100644 --- a/client/src/main/java/envoy/client/ui/Startup.java +++ b/client/src/main/java/envoy/client/ui/Startup.java @@ -12,7 +12,7 @@ import javafx.stage.Stage; import envoy.data.*; import envoy.data.User.UserStatus; -import envoy.event.*; +import envoy.event.UserStatusChange; import envoy.exception.EnvoyException; import envoy.util.EnvoyLog; @@ -115,21 +115,20 @@ public final class Startup extends Application { * @since Envoy Client v0.2-beta */ public static boolean performHandshake(LoginCredentials credentials) { - final var cacheMap = new CacheMap(); - cacheMap.put(Message.class, new Cache()); - cacheMap.put(GroupMessage.class, new Cache()); - cacheMap.put(MessageStatusChange.class, new Cache()); - cacheMap.put(GroupMessageStatusChange.class, new Cache()); final var originalStatus = localDB.getUser() == null ? UserStatus.ONLINE : localDB.getUser().getStatus(); try { - client.performHandshake(credentials, cacheMap); + client.performHandshake(credentials); if (client.isOnline()) { // Restore the original status as the server automatically returns status ONLINE client.getSender().setStatus(originalStatus); loadChatScene(); - client.initReceiver(localDB, cacheMap); + + // Request an ID generator if none is present or the existing one is consumed + if (!localDB.hasIDGenerator() || !localDB.getIDGenerator().hasNext()) + client.requestIDGenerator(); + return true; } else return false; diff --git a/server/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/server/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 3000db9..0ced984 100755 --- a/server/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/server/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -40,6 +40,7 @@ public final class LoginCredentialProcessor implements ObjectProcessor pendingGroupMessages = PersistenceManager.getInstance() @@ -197,10 +209,11 @@ public final class LoginCredentialProcessor implements ObjectProcessor