From 6bbd6936e09bc405ae021a030900de5daf5fb523 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 6 Feb 2020 18:35:05 +0100 Subject: [PATCH] Replaced MessageCache by Cache This class will be used to implement the offline cache for both messages and events. --- src/main/java/envoy/client/data/Cache.java | 58 +++++++++++++++++++ src/main/java/envoy/client/net/Client.java | 5 +- .../java/envoy/client/net/MessageCache.java | 54 ----------------- src/main/java/envoy/client/ui/Startup.java | 10 ++-- 4 files changed, 65 insertions(+), 62 deletions(-) create mode 100644 src/main/java/envoy/client/data/Cache.java delete mode 100644 src/main/java/envoy/client/net/MessageCache.java diff --git a/src/main/java/envoy/client/data/Cache.java b/src/main/java/envoy/client/data/Cache.java new file mode 100644 index 0000000..366f703 --- /dev/null +++ b/src/main/java/envoy/client/data/Cache.java @@ -0,0 +1,58 @@ +package envoy.client.data; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.function.Consumer; +import java.util.logging.Logger; + +import envoy.client.util.EnvoyLog; + +/** + * Stores elements in a queue to process them later.
+ *
+ * Project: envoy-client
+ * File: Cache.java
+ * Created: 6 Feb 2020
+ * + * @param the type of cached elements + * @author Kai S. K. Engelbart + * @since Envoy v0.3-alpha + */ +public class Cache implements Consumer { + + private final Queue elements = new LinkedList<>(); + private Consumer processor; + + private static final Logger logger = EnvoyLog.getLogger(Cache.class.getSimpleName()); + + /** + * Adds an element to the cache. + * + * @param element the element to add + * @since Envoy v0.3-alpha + */ + @Override + public void accept(T element) { + logger.info(String.format("Adding element %s to cache", element)); + elements.offer(element); + } + + /** + * Sets the processor to which cached elements are relayed. + * + * @param processor the processor to set + * @since Envoy v0.3-alpha + */ + public void setProcessor(Consumer processor) { this.processor = processor; } + + /** + * Relays all cached elements to the processor. + * + * @throws IllegalStateException if the processor is not initialized + * @since Envoy v0.3-alpha + */ + public void relay() { + if (processor == null) throw new IllegalStateException("Processor is not defined"); + elements.forEach(processor::accept); + } +} diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 91e9040..2e96b4a 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -10,6 +10,7 @@ import java.util.logging.Logger; import javax.naming.TimeLimitExceededException; import envoy.client.Config; +import envoy.client.data.Cache; import envoy.client.data.LocalDb; import envoy.client.util.EnvoyLog; import envoy.data.*; @@ -61,7 +62,7 @@ public class Client implements Closeable { * failed for some other reason * @since Envoy v0.2-alpha */ - public MessageCache onlineInit(LoginCredentials credentials, LocalDb localDb) throws Exception { + public Cache onlineInit(LoginCredentials credentials, LocalDb localDb) throws Exception { // Establish TCP connection logger.info(String.format("Attempting connection to server %s:%d...", config.getServer(), config.getPort())); socket = new Socket(config.getServer(), config.getPort()); @@ -71,7 +72,7 @@ public class Client implements Closeable { receiver = new Receiver(socket.getInputStream()); // Create cache for unread messages - final MessageCache cache = new MessageCache(); + final Cache cache = new Cache<>(); // Register user creation processor, contact list processor and message cache receiver.registerProcessor(User.class, sender -> { logger.info("Acquired user object " + sender); this.sender = sender; }); diff --git a/src/main/java/envoy/client/net/MessageCache.java b/src/main/java/envoy/client/net/MessageCache.java deleted file mode 100644 index 1fc49c3..0000000 --- a/src/main/java/envoy/client/net/MessageCache.java +++ /dev/null @@ -1,54 +0,0 @@ -package envoy.client.net; - -import java.util.LinkedList; -import java.util.Queue; -import java.util.function.Consumer; -import java.util.logging.Logger; - -import envoy.client.util.EnvoyLog; -import envoy.data.Message; - -/** - * Stores messages in a queue until the application initialization is complete. - * The messages can then be relayed to a processor.
- *
- * Project: envoy-client
- * File: MessageCache.java
- * Created: 4 Feb 2020
- * - * @author Kai S. K. Engelbart - * @since Envoy v0.3-alpha - */ -public class MessageCache implements Consumer { - - private final Queue messages = new LinkedList<>(); - private Consumer processor; - - private static final Logger logger = EnvoyLog.getLogger(MessageCache.class.getSimpleName()); - - /** - * Adds a message to the cache. - * - * @since Envoy v0.3-alpha - */ - @Override - public void accept(Message message) { - logger.info(String.format("Adding message %s to cache", message)); - messages.add(message); - } - - /** - * Sets the processor to which messages are relayed. - * - * @param processor the processor to set - * @since Envoy v0.3-alpha - */ - public void setProcessor(Consumer processor) { this.processor = processor; } - - /** - * Relays all cached messages to the processor. - * - * @since Envoy v0.3-alpha - */ - public void relayMessages() { messages.forEach(processor::accept); } -} diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 9f308dc..1a38da9 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -13,13 +13,11 @@ import javax.swing.SwingUtilities; import envoy.client.Config; import envoy.client.Settings; -import envoy.client.data.LocalDb; -import envoy.client.data.PersistentLocalDb; -import envoy.client.data.TransientLocalDb; +import envoy.client.data.*; import envoy.client.net.Client; -import envoy.client.net.MessageCache; import envoy.client.util.EnvoyLog; import envoy.data.LoginCredentials; +import envoy.data.Message; import envoy.data.User; import envoy.exception.EnvoyException; @@ -108,7 +106,7 @@ public class Startup { // Acquire the client user (with ID) either from the server or from the local // database, which triggers offline mode Client client = new Client(); - MessageCache cache = null; + Cache cache = null; try { // Try entering online mode first localDb.loadIdGenerator(); @@ -178,7 +176,7 @@ public class Startup { }); // Relay unread messages from cache - if (cache != null) cache.relayMessages(); + if (cache != null) cache.relay(); // Save Settings and PersistentLocalDb on shutdown Runtime.getRuntime().addShutdownHook(new Thread(() -> {