From f6eeeee79b77da9ec25b835a5c1122be96d24dc6 Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 25 Sep 2020 15:28:14 +0200 Subject: [PATCH] Remove message and event processors from client --- .../main/java/envoy/client/data/LocalDB.java | 21 +++++++++++- .../main/java/envoy/client/net/Client.java | 27 +++++++--------- .../GroupMessageStatusChangeProcessor.java | 25 --------------- .../net/MessageStatusChangeProcessor.java | 32 ------------------- .../net/ReceivedGroupMessageProcessor.java | 29 ----------------- .../client/net/ReceivedMessageProcessor.java | 24 -------------- .../envoy/client/ui/controller/ChatScene.java | 2 +- .../envoy/client/util/ReflectionUtil.java | 3 +- 8 files changed, 33 insertions(+), 130 deletions(-) delete mode 100644 client/src/main/java/envoy/client/net/GroupMessageStatusChangeProcessor.java delete mode 100644 client/src/main/java/envoy/client/net/MessageStatusChangeProcessor.java delete mode 100644 client/src/main/java/envoy/client/net/ReceivedGroupMessageProcessor.java delete mode 100644 client/src/main/java/envoy/client/net/ReceivedMessageProcessor.java diff --git a/client/src/main/java/envoy/client/data/LocalDB.java b/client/src/main/java/envoy/client/data/LocalDB.java index c6b527d..44cd639 100644 --- a/client/src/main/java/envoy/client/data/LocalDB.java +++ b/client/src/main/java/envoy/client/data/LocalDB.java @@ -5,10 +5,11 @@ import java.nio.channels.*; import java.nio.file.StandardOpenOption; import java.time.Instant; import java.util.*; -import java.util.logging.Level; +import java.util.logging.*; import envoy.client.event.EnvoyCloseEvent; import envoy.data.*; +import envoy.data.Message.MessageStatus; import envoy.event.*; import envoy.exception.EnvoyException; import envoy.util.*; @@ -45,6 +46,8 @@ public final class LocalDB implements EventListener { private final File dbDir, idGeneratorFile, lastLoginFile, usersFile; + private static final Logger logger = EnvoyLog.getLogger(LocalDB.class); + /** * Constructs an empty local database. * @@ -219,6 +222,22 @@ public final class LocalDB implements EventListener { } } + @Event(priority = 150) + private void onMessage(Message msg) { if (msg.getStatus() == MessageStatus.SENT) msg.nextStatus(); } + + @Event(priority = 150) + private void onGroupMessage(GroupMessage msg) { + // TODO: Cancel event once EventBus is updated + if (msg.getStatus() == MessageStatus.WAITING || msg.getStatus() == MessageStatus.READ) + logger.warning("The groupMessage has the unexpected status " + msg.getStatus()); + } + + @Event(priority = 150, includeSubtypes = true) + private void onMessageStatusChange(MessageStatusChange evt) { + // TODO: Cancel event once EventBus is updated + if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.warning("Received invalid " + evt); + } + /** * Stores a new authentication token. * diff --git a/client/src/main/java/envoy/client/net/Client.java b/client/src/main/java/envoy/client/net/Client.java index 24f6664..149052b 100644 --- a/client/src/main/java/envoy/client/net/Client.java +++ b/client/src/main/java/envoy/client/net/Client.java @@ -125,21 +125,16 @@ public final class Client implements EventListener, Closeable { receiver.removeAllProcessors(); // Process incoming messages - final var receivedMessageProcessor = new ReceivedMessageProcessor(); - final var receivedGroupMessageProcessor = new ReceivedGroupMessageProcessor(); - final var messageStatusChangeProcessor = new MessageStatusChangeProcessor(); - final var groupMessageStatusChangeProcessor = new GroupMessageStatusChangeProcessor(); - - receiver.registerProcessor(GroupMessage.class, receivedGroupMessageProcessor); - receiver.registerProcessor(Message.class, receivedMessageProcessor); - receiver.registerProcessor(MessageStatusChange.class, messageStatusChangeProcessor); - receiver.registerProcessor(GroupMessageStatusChange.class, groupMessageStatusChangeProcessor); + receiver.registerProcessor(GroupMessage.class, eventBus::dispatch); + receiver.registerProcessor(Message.class, eventBus::dispatch); + receiver.registerProcessor(MessageStatusChange.class, eventBus::dispatch); + receiver.registerProcessor(GroupMessageStatusChange.class, eventBus::dispatch); // Relay cached messages and message status changes - cacheMap.get(Message.class).setProcessor(receivedMessageProcessor); - cacheMap.get(GroupMessage.class).setProcessor(receivedGroupMessageProcessor); - cacheMap.get(MessageStatusChange.class).setProcessor(messageStatusChangeProcessor); - cacheMap.get(GroupMessageStatusChange.class).setProcessor(groupMessageStatusChangeProcessor); + 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); // Process user status changes receiver.registerProcessor(UserStatusChange.class, eventBus::dispatch); @@ -176,7 +171,7 @@ public final class Client implements EventListener, Closeable { receiver.registerProcessor(GroupCreationResult.class, eventBus::dispatch); // Request a generator if none is present or the existing one is consumed - if (!localDB.hasIDGenerator() || !localDB.getIDGenerator().hasNext()) requestIdGenerator(); + if (!localDB.hasIDGenerator() || !localDB.getIDGenerator().hasNext()) requestIDGenerator(); // Relay caches cacheMap.getMap().values().forEach(Cache::relay); @@ -190,7 +185,7 @@ public final class Client implements EventListener, Closeable { * @throws RuntimeException if the object serialization failed * @since Envoy Client v0.2-beta */ - public void send(Object obj) throws IllegalStateException, RuntimeException { + public void send(Serializable obj) throws IllegalStateException, RuntimeException { checkOnline(); logger.log(Level.FINE, "Sending " + obj); try { @@ -217,7 +212,7 @@ public final class Client implements EventListener, Closeable { * * @since Envoy Client v0.3-alpha */ - public void requestIdGenerator() { + public void requestIDGenerator() { logger.log(Level.INFO, "Requesting new id generator..."); send(new IDGeneratorRequest()); } diff --git a/client/src/main/java/envoy/client/net/GroupMessageStatusChangeProcessor.java b/client/src/main/java/envoy/client/net/GroupMessageStatusChangeProcessor.java deleted file mode 100644 index c4aea7c..0000000 --- a/client/src/main/java/envoy/client/net/GroupMessageStatusChangeProcessor.java +++ /dev/null @@ -1,25 +0,0 @@ -package envoy.client.net; - -import java.util.function.Consumer; -import java.util.logging.Logger; - -import envoy.data.Message.MessageStatus; -import envoy.event.GroupMessageStatusChange; -import envoy.util.EnvoyLog; - -import dev.kske.eventbus.EventBus; - -/** - * @author Maximilian Käfer - * @since Envoy Client v0.1-beta - */ -public final 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/client/src/main/java/envoy/client/net/MessageStatusChangeProcessor.java b/client/src/main/java/envoy/client/net/MessageStatusChangeProcessor.java deleted file mode 100644 index 36f3ed4..0000000 --- a/client/src/main/java/envoy/client/net/MessageStatusChangeProcessor.java +++ /dev/null @@ -1,32 +0,0 @@ -package envoy.client.net; - -import java.util.function.Consumer; -import java.util.logging.Logger; - -import envoy.data.Message.MessageStatus; -import envoy.event.MessageStatusChange; -import envoy.util.EnvoyLog; - -import dev.kske.eventbus.EventBus; - -/** - * @author Kai S. K. Engelbart - * @since Envoy Client v0.3-alpha - */ -public final class MessageStatusChangeProcessor implements Consumer { - - private static final Logger logger = EnvoyLog.getLogger(MessageStatusChangeProcessor.class); - - /** - * Dispatches a {@link MessageStatusChange} if the status is - * {@code RECEIVED} or {@code READ}. - * - * @param evt the status change event - * @since Envoy Client v0.3-alpha - */ - @Override - public void accept(MessageStatusChange evt) { - if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.warning("Received invalid message status change " + evt); - else EventBus.getInstance().dispatch(evt); - } -} diff --git a/client/src/main/java/envoy/client/net/ReceivedGroupMessageProcessor.java b/client/src/main/java/envoy/client/net/ReceivedGroupMessageProcessor.java deleted file mode 100644 index a5d4cc3..0000000 --- a/client/src/main/java/envoy/client/net/ReceivedGroupMessageProcessor.java +++ /dev/null @@ -1,29 +0,0 @@ -package envoy.client.net; - -import java.util.function.Consumer; -import java.util.logging.Logger; - -import envoy.data.GroupMessage; -import envoy.data.Message.MessageStatus; -import envoy.util.EnvoyLog; - -import dev.kske.eventbus.EventBus; - -/** - * @author Maximilian Käfer - * @since Envoy Client v0.1-beta - */ -public final class ReceivedGroupMessageProcessor implements Consumer { - - private static final Logger logger = EnvoyLog.getLogger(ReceivedGroupMessageProcessor.class); - - @Override - public void accept(GroupMessage groupMessage) { - if (groupMessage.getStatus() == MessageStatus.WAITING || groupMessage.getStatus() == MessageStatus.READ) - logger.warning("The groupMessage has the unexpected status " + groupMessage.getStatus()); - - // Dispatch event - EventBus.getInstance().dispatch(groupMessage); - } - -} diff --git a/client/src/main/java/envoy/client/net/ReceivedMessageProcessor.java b/client/src/main/java/envoy/client/net/ReceivedMessageProcessor.java deleted file mode 100644 index 3efcc1c..0000000 --- a/client/src/main/java/envoy/client/net/ReceivedMessageProcessor.java +++ /dev/null @@ -1,24 +0,0 @@ -package envoy.client.net; - -import java.util.function.Consumer; - -import envoy.data.Message; -import envoy.data.Message.MessageStatus; - -import dev.kske.eventbus.EventBus; - -/** - * @author Kai S. K. Engelbart - * @since Envoy Client v0.3-alpha - */ -public final class ReceivedMessageProcessor implements Consumer { - - @Override - public void accept(Message message) { - // Update status to RECEIVED - if (message.getStatus() == MessageStatus.SENT) message.nextStatus(); - - // Dispatch message - EventBus.getInstance().dispatch(message); - } -} diff --git a/client/src/main/java/envoy/client/ui/controller/ChatScene.java b/client/src/main/java/envoy/client/ui/controller/ChatScene.java index baf05e4..5299be6 100644 --- a/client/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/client/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -694,7 +694,7 @@ public final class ChatScene implements EventListener, Restorable { scrollToMessageListEnd(); // Request a new ID generator if all IDs were used - if (!localDB.getIDGenerator().hasNext() && client.isOnline()) client.requestIdGenerator(); + if (!localDB.getIDGenerator().hasNext() && client.isOnline()) client.requestIDGenerator(); } // Clear text field and disable post button diff --git a/client/src/main/java/envoy/client/util/ReflectionUtil.java b/client/src/main/java/envoy/client/util/ReflectionUtil.java index 27ea492..49273f8 100644 --- a/client/src/main/java/envoy/client/util/ReflectionUtil.java +++ b/client/src/main/java/envoy/client/util/ReflectionUtil.java @@ -15,8 +15,7 @@ public final class ReflectionUtil { /** * Gets all declared variable values of the given instance that have the - * specified - * class. + * specified class. *

* (i.e. can get all {@code JComponents} (Swing) or {@code Nodes} (JavaFX) in a * GUI class).