From b3fc13f8e2f1630d5836b05365e12d574ad3ca32 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 4 Jul 2020 15:43:46 +0200 Subject: [PATCH] Implemented GroupMessageStatusChangeProcessor and a Cache for those --- src/main/java/envoy/client/net/Client.java | 17 +++++++---- .../GroupMessageStatusChangeProcessor.java | 29 +++++++++++++++++++ src/main/java/envoy/client/ui/Startup.java | 6 +++- .../envoy/client/ui/controller/ChatScene.java | 12 +++++--- .../client/ui/controller/LoginScene.java | 22 ++++++++++---- 5 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 src/main/java/envoy/client/net/GroupMessageStatusChangeProcessor.java diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index cf41d93..fb4f114 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -75,7 +75,8 @@ public class Client implements Closeable { * waiting for the handshake response */ public void performHandshake(LoginCredentials credentials, Cache receivedMessageCache, Cache receivedGroupMessageCache, - Cache receivedMessageStatusChangeCache) throws TimeoutException, IOException, InterruptedException { + Cache receivedMessageStatusChangeCache, Cache receivedGroupMessageStatusChangeCache) + throws TimeoutException, IOException, InterruptedException { if (online) throw new IllegalStateException("Handshake has already been performed successfully"); // Establish TCP connection @@ -91,6 +92,7 @@ public class Client implements Closeable { receiver.registerProcessor(Message.class, receivedMessageCache); receiver.registerProcessor(GroupMessage.class, receivedGroupMessageCache); receiver.registerProcessor(MessageStatusChange.class, receivedMessageStatusChangeCache); + receiver.registerProcessor(GroupMessageStatusChange.class, receivedGroupMessageStatusChangeCache); receiver.registerProcessor(HandshakeRejection.class, evt -> { rejected = true; eventBus.dispatch(evt); }); rejected = false; @@ -152,27 +154,30 @@ public class Client implements Closeable { * @since Envoy Client v0.2-alpha */ public void initReceiver(LocalDB localDB, Cache receivedMessageCache, Cache receivedGroupMessageCache, - Cache receivedMessageStatusChangeCache) + Cache receivedMessageStatusChangeCache, Cache receivedGroupMessageStatusChangeCache) throws IOException { checkOnline(); // Process incoming messages final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor(); final ReceivedGroupMessageProcessor receivedGroupMessageProcessor = new ReceivedGroupMessageProcessor(); - final MessageStatusChangeProcessor messageStatusChangeEventProcessor = new MessageStatusChangeProcessor(); + final MessageStatusChangeProcessor messageStatusChangeProcessor = new MessageStatusChangeProcessor(); + final GroupMessageStatusChangeProcessor groupMessageStatusChangeProcessor = new GroupMessageStatusChangeProcessor(); - // TODO: Define a cache receiver.registerProcessor(GroupMessage.class, receivedGroupMessageProcessor); receiver.registerProcessor(Message.class, receivedMessageProcessor); + receiver.registerProcessor(MessageStatusChange.class, messageStatusChangeProcessor); + + receiver.registerProcessor(GroupMessageStatusChange.class, groupMessageStatusChangeProcessor); // Relay cached unread messages and unread groupMessages receivedMessageCache.setProcessor(receivedMessageProcessor); receivedGroupMessageCache.setProcessor(receivedGroupMessageProcessor); // Process message status changes - receiver.registerProcessor(MessageStatusChange.class, messageStatusChangeEventProcessor); - receivedMessageStatusChangeCache.setProcessor(messageStatusChangeEventProcessor); + receivedMessageStatusChangeCache.setProcessor(messageStatusChangeProcessor); + receivedGroupMessageStatusChangeCache.setProcessor(groupMessageStatusChangeProcessor); // Process user status changes receiver.registerProcessor(UserStatusChange.class, eventBus::dispatch); diff --git a/src/main/java/envoy/client/net/GroupMessageStatusChangeProcessor.java b/src/main/java/envoy/client/net/GroupMessageStatusChangeProcessor.java new file mode 100644 index 0000000..ce3dc73 --- /dev/null +++ b/src/main/java/envoy/client/net/GroupMessageStatusChangeProcessor.java @@ -0,0 +1,29 @@ +package envoy.client.net; + +import java.util.function.Consumer; +import java.util.logging.Logger; + +import envoy.data.Message.MessageStatus; +import envoy.event.EventBus; +import envoy.event.GroupMessageStatusChange; +import envoy.util.EnvoyLog; + +/** + * Project: envoy-client
+ * File: GroupMessageStatusChangePocessor.java
+ * Created: 03.07.2020
+ * + * @author Maximilian Käfer + * @since Envoy Client v0.1-beta + */ +public class GroupMessageStatusChangeProcessor implements Consumer { + + private static final Logger logger = EnvoyLog.getLogger(GroupMessageStatusChangeProcessor.class); + + @Override + public void accept(GroupMessageStatusChange evt) { + if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.warning("Received invalid group message status change " + evt); + else EventBus.getInstance().dispatch(evt); + } + +} diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index b8882e6..de3289f 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -17,6 +17,7 @@ import envoy.client.ui.SceneContext.SceneInfo; import envoy.client.ui.controller.LoginScene; import envoy.data.GroupMessage; import envoy.data.Message; +import envoy.event.GroupMessageStatusChange; import envoy.event.MessageStatusChange; import envoy.exception.EnvoyException; import envoy.util.EnvoyLog; @@ -46,6 +47,7 @@ public final class Startup extends Application { private Cache messageCache; private Cache groupMessageCache; private Cache messageStatusCache; + private Cache groupMessageStatusCache; private static final ClientConfig config = ClientConfig.getInstance(); private static final Logger logger = EnvoyLog.getLogger(Startup.class); @@ -103,13 +105,15 @@ public final class Startup extends Application { messageCache = new Cache<>(); groupMessageCache = new Cache<>(); messageStatusCache = new Cache<>(); + groupMessageStatusCache = new Cache<>(); stage.setTitle("Envoy"); stage.getIcons().add(IconUtil.load("/icons/envoy_logo.png")); final var sceneContext = new SceneContext(stage); sceneContext.load(SceneInfo.LOGIN_SCENE); - sceneContext.getController().initializeData(client, localDB, messageCache, groupMessageCache, messageStatusCache, sceneContext); + sceneContext.getController() + .initializeData(client, localDB, messageCache, groupMessageCache, messageStatusCache, groupMessageStatusCache, sceneContext); } /** diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index c3aa608..f5f454b 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -26,9 +26,7 @@ import envoy.client.net.Client; import envoy.client.net.WriteProxy; import envoy.client.ui.*; import envoy.data.*; -import envoy.event.EventBus; -import envoy.event.MessageStatusChange; -import envoy.event.UserStatusChange; +import envoy.event.*; import envoy.event.contact.ContactOperation; import envoy.util.EnvoyLog; @@ -127,10 +125,16 @@ public final class ChatScene { // Listen to message status changes eventBus.register(MessageStatusChange.class, e -> localDB.getMessage(e.getID()).ifPresent(message -> { message.setStatus(e.get()); - // Update UI if in current chat if (currentChat != null && message.getSenderID() == currentChat.getRecipient().getID()) Platform.runLater(messageList::refresh); })); + + eventBus.register(GroupMessageStatusChange.class, e -> localDB.getMessage(e.getID()).ifPresent(groupMessage -> { + ((GroupMessage) groupMessage).getMemberStatuses().replace(e.getMemberID(), e.get()); + + // Update UI id in current chat + if (currentChat != null && groupMessage.getRecipientID() == currentChat.getRecipient().getID()) Platform.runLater(messageList::refresh); + })); // Listen to user status changes eventBus.register(UserStatusChange.class, diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index 95350c7..f3dec05 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -17,9 +17,7 @@ import envoy.client.ui.SceneContext; import envoy.client.ui.Startup; import envoy.data.*; import envoy.data.User.UserStatus; -import envoy.event.EventBus; -import envoy.event.HandshakeRejection; -import envoy.event.MessageStatusChange; +import envoy.event.*; import envoy.exception.EnvoyException; import envoy.util.Bounds; import envoy.util.EnvoyLog; @@ -58,6 +56,7 @@ public final class LoginScene { private Cache receivedMessageCache; private Cache receivedGroupMessageCache; private Cache receivedMessageStatusChangeCache; + private Cache receivedGroupMessageStatusChangeCache; private SceneContext sceneContext; private static final Logger logger = EnvoyLog.getLogger(LoginScene.class); @@ -93,12 +92,14 @@ public final class LoginScene { * @since Envoy Client v0.1-beta */ public void initializeData(Client client, LocalDB localDB, Cache receivedMessageCache, Cache receivedGroupMessageCache, - Cache receivedMessageStatusChangeCache, SceneContext sceneContext) { + 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; // Prepare handshake @@ -147,9 +148,17 @@ public final class LoginScene { private void performHandshake(LoginCredentials credentials) { try { - client.performHandshake(credentials, receivedMessageCache, receivedGroupMessageCache, receivedMessageStatusChangeCache); + client.performHandshake(credentials, + receivedMessageCache, + receivedGroupMessageCache, + receivedMessageStatusChangeCache, + receivedGroupMessageStatusChangeCache); if (client.isOnline()) { - client.initReceiver(localDB, receivedMessageCache, receivedGroupMessageCache, receivedMessageStatusChangeCache); + client.initReceiver(localDB, + receivedMessageCache, + receivedGroupMessageCache, + receivedMessageStatusChangeCache, + receivedGroupMessageStatusChangeCache); loadChatScene(); } } catch (IOException | InterruptedException | TimeoutException e) { @@ -216,5 +225,6 @@ public final class LoginScene { 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(); } }