From 21ad87d97db4d99d86d66abdcec3065972571991 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sun, 28 Jun 2020 13:20:10 +0200 Subject: [PATCH] Implemented pendingMessage processing using an extra cache --- src/main/java/envoy/client/net/Client.java | 24 +++++++++++++++---- src/main/java/envoy/client/net/Receiver.java | 3 ++- src/main/java/envoy/client/ui/Startup.java | 5 +++- .../client/ui/controller/LoginScene.java | 15 +++++++----- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index b4b1ed0..cf41d93 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -59,6 +59,12 @@ public class Client implements Closeable { * 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 @@ -68,7 +74,7 @@ public class Client implements Closeable { * @throws InterruptedException if the current thread is interrupted while * waiting for the handshake response */ - public void performHandshake(LoginCredentials credentials, Cache receivedMessageCache, + public void performHandshake(LoginCredentials credentials, Cache receivedMessageCache, Cache receivedGroupMessageCache, Cache receivedMessageStatusChangeCache) throws TimeoutException, IOException, InterruptedException { if (online) throw new IllegalStateException("Handshake has already been performed successfully"); @@ -83,6 +89,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(HandshakeRejection.class, evt -> { rejected = true; eventBus.dispatch(evt); }); @@ -130,6 +137,12 @@ public class Client implements Closeable { * 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 @@ -138,21 +151,24 @@ public class Client implements Closeable { * requested from the server * @since Envoy Client v0.2-alpha */ - public void initReceiver(LocalDB localDB, Cache receivedMessageCache, Cache receivedMessageStatusChangeCache) + public void initReceiver(LocalDB localDB, Cache receivedMessageCache, Cache receivedGroupMessageCache, + Cache receivedMessageStatusChangeCache) throws IOException { checkOnline(); // Process incoming messages final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor(); + final ReceivedGroupMessageProcessor receivedGroupMessageProcessor = new ReceivedGroupMessageProcessor(); final MessageStatusChangeProcessor messageStatusChangeEventProcessor = new MessageStatusChangeProcessor(); // TODO: Define a cache - receiver.registerProcessor(GroupMessage.class, new ReceivedGroupMessageProcessor()); + receiver.registerProcessor(GroupMessage.class, receivedGroupMessageProcessor); receiver.registerProcessor(Message.class, receivedMessageProcessor); - // Relay cached unread messages + // Relay cached unread messages and unread groupMessages receivedMessageCache.setProcessor(receivedMessageProcessor); + receivedGroupMessageCache.setProcessor(receivedGroupMessageProcessor); // Process message status changes receiver.registerProcessor(MessageStatusChange.class, messageStatusChangeEventProcessor); diff --git a/src/main/java/envoy/client/net/Receiver.java b/src/main/java/envoy/client/net/Receiver.java index c71e5ed..dbcca32 100644 --- a/src/main/java/envoy/client/net/Receiver.java +++ b/src/main/java/envoy/client/net/Receiver.java @@ -71,7 +71,8 @@ public class Receiver extends Thread { final Consumer processor = processors.get(obj.getClass()); if (processor == null) logger.log(Level.WARNING, String.format( - "The received object has the class %s for which no processor is defined.", obj.getClass())); + "The received object has the %s for which no processor is defined.", + obj.getClass())); else processor.accept(obj); } } diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index f57b22e..b8882e6 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -15,6 +15,7 @@ import envoy.client.data.*; import envoy.client.net.Client; import envoy.client.ui.SceneContext.SceneInfo; import envoy.client.ui.controller.LoginScene; +import envoy.data.GroupMessage; import envoy.data.Message; import envoy.event.MessageStatusChange; import envoy.exception.EnvoyException; @@ -43,6 +44,7 @@ public final class Startup extends Application { private LocalDB localDB; private Client client; private Cache messageCache; + private Cache groupMessageCache; private Cache messageStatusCache; private static final ClientConfig config = ClientConfig.getInstance(); @@ -99,6 +101,7 @@ public final class Startup extends Application { // Initialize client and unread message cache client = new Client(); messageCache = new Cache<>(); + groupMessageCache = new Cache<>(); messageStatusCache = new Cache<>(); stage.setTitle("Envoy"); @@ -106,7 +109,7 @@ public final class Startup extends Application { final var sceneContext = new SceneContext(stage); sceneContext.load(SceneInfo.LOGIN_SCENE); - sceneContext.getController().initializeData(client, localDB, messageCache, messageStatusCache, sceneContext); + sceneContext.getController().initializeData(client, localDB, messageCache, groupMessageCache, messageStatusCache, sceneContext); } /** diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index 380842a..95350c7 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -15,9 +15,7 @@ import envoy.client.data.*; import envoy.client.net.Client; import envoy.client.ui.SceneContext; import envoy.client.ui.Startup; -import envoy.data.LoginCredentials; -import envoy.data.Message; -import envoy.data.User; +import envoy.data.*; import envoy.data.User.UserStatus; import envoy.event.EventBus; import envoy.event.HandshakeRejection; @@ -58,6 +56,7 @@ public final class LoginScene { private Client client; private LocalDB localDB; private Cache receivedMessageCache; + private Cache receivedGroupMessageCache; private Cache receivedMessageStatusChangeCache; private SceneContext sceneContext; @@ -83,6 +82,8 @@ public final class LoginScene { * @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 @@ -91,11 +92,12 @@ public final class LoginScene { * scene * @since Envoy Client v0.1-beta */ - public void initializeData(Client client, LocalDB localDB, Cache receivedMessageCache, + public void initializeData(Client client, LocalDB localDB, Cache receivedMessageCache, Cache receivedGroupMessageCache, Cache receivedMessageStatusChangeCache, SceneContext sceneContext) { this.client = client; this.localDB = localDB; this.receivedMessageCache = receivedMessageCache; + this.receivedGroupMessageCache = receivedGroupMessageCache; this.receivedMessageStatusChangeCache = receivedMessageStatusChangeCache; this.sceneContext = sceneContext; @@ -145,9 +147,9 @@ public final class LoginScene { private void performHandshake(LoginCredentials credentials) { try { - client.performHandshake(credentials, receivedMessageCache, receivedMessageStatusChangeCache); + client.performHandshake(credentials, receivedMessageCache, receivedGroupMessageCache, receivedMessageStatusChangeCache); if (client.isOnline()) { - client.initReceiver(localDB, receivedMessageCache, receivedMessageStatusChangeCache); + client.initReceiver(localDB, receivedMessageCache, receivedGroupMessageCache, receivedMessageStatusChangeCache); loadChatScene(); } } catch (IOException | InterruptedException | TimeoutException e) { @@ -212,6 +214,7 @@ public final class LoginScene { // 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(); } }