From 8b204b3715a7f03a9460ec9d4d917ff6ecfbedfb Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 2 Sep 2020 09:51:08 +0200 Subject: [PATCH] Fix write proxy initialization --- .../main/java/envoy/client/data/Cache.java | 6 --- .../main/java/envoy/client/data/Context.java | 33 ++++++++-------- .../java/envoy/client/net/WriteProxy.java | 39 +++++-------------- .../main/java/envoy/client/ui/Startup.java | 3 +- 4 files changed, 27 insertions(+), 54 deletions(-) diff --git a/client/src/main/java/envoy/client/data/Cache.java b/client/src/main/java/envoy/client/data/Cache.java index 4283800..25b985e 100644 --- a/client/src/main/java/envoy/client/data/Cache.java +++ b/client/src/main/java/envoy/client/data/Cache.java @@ -51,12 +51,6 @@ public final class Cache implements Consumer, Serializable { */ public void setProcessor(Consumer processor) { this.processor = processor; } - /** - * @return the processor - * @since Envoy Client v0.2-beta - */ - public Consumer getProcessor() { return processor; } - /** * Relays all cached elements to the processor. * diff --git a/client/src/main/java/envoy/client/data/Context.java b/client/src/main/java/envoy/client/data/Context.java index 26a3b0e..a692f2e 100644 --- a/client/src/main/java/envoy/client/data/Context.java +++ b/client/src/main/java/envoy/client/data/Context.java @@ -18,24 +18,33 @@ import envoy.client.ui.SceneContext; */ public class Context { + private WriteProxy writeProxy; + private LocalDB localDB; + private Stage stage; + private SceneContext sceneContext; + private final Client client = new Client(); - private WriteProxy writeProxy; - - private LocalDB localDB; - - private Stage stage; - - private SceneContext sceneContext; - private static final Context instance = new Context(); + private Context() {} + /** * @return the instance of {@code Context} used throughout Envoy * @since Envoy Client v0.2-beta */ public static Context getInstance() { return instance; } + /** + * Initializes the write proxy given that {@code localDB} is initialized. + * + * @since Envoy Client v0.2-beta + */ + public void initWriteProxy() { + if (localDB == null) throw new IllegalStateException("The LocalDB has to be initialized!"); + writeProxy = new WriteProxy(client, localDB); + } + /** * @return the localDB * @since Envoy Client v0.2-beta @@ -81,14 +90,6 @@ public class Context { */ public Stage getStage() { return stage; } - private Context() {} - - /** - * @param writeProxy the writeProxy to set - * @since Envoy Client v0.2-beta - */ - public void setWriteProxy(WriteProxy writeProxy) { this.writeProxy = writeProxy; } - /** * @param stage the stage to set * @since Envoy Client v0.2-beta diff --git a/client/src/main/java/envoy/client/net/WriteProxy.java b/client/src/main/java/envoy/client/net/WriteProxy.java index b953e5f..c2c672b 100644 --- a/client/src/main/java/envoy/client/net/WriteProxy.java +++ b/client/src/main/java/envoy/client/net/WriteProxy.java @@ -5,7 +5,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import envoy.client.data.Cache; -import envoy.client.data.Context; import envoy.client.data.LocalDB; import envoy.data.Message; import envoy.event.MessageStatusChange; @@ -25,8 +24,8 @@ import envoy.util.EnvoyLog; */ public final class WriteProxy { - private final Client client = Context.getInstance().getClient(); - private final LocalDB localDB = Context.getInstance().getLocalDB(); + private final Client client; + private final LocalDB localDB; private static final Logger logger = EnvoyLog.getLogger(WriteProxy.class); @@ -34,9 +33,14 @@ public final class WriteProxy { * Initializes a write proxy using a client and a local database. The * corresponding cache processors are injected into the caches. * + * @param client the client instance used to send messages and events if online + * @param localDB the local database used to cache messages and events if + * offline * @since Envoy Client v0.3-alpha */ - public WriteProxy() { + public WriteProxy(Client client, LocalDB localDB) { + this.client = client; + this.localDB = localDB; // Initialize cache processors for messages and message status change events localDB.getCacheMap().get(Message.class).setProcessor(msg -> { @@ -63,32 +67,7 @@ public final class WriteProxy { * * @since Envoy Client v0.3-alpha */ - public void flushCache() { - - // supplying default values if not initialized - for some reason these - // processors can be not defined... - final var messageCache = localDB.getCacheMap().get(Message.class); - if (messageCache.getProcessor() == null) messageCache.setProcessor(msg -> { - try { - logger.log(Level.FINER, "Sending cached " + msg); - client.sendMessage(msg); - } catch (final IOException e) { - logger.log(Level.SEVERE, "Could not send cached message: ", e); - } - }); - final var messageStatusCache = localDB.getCacheMap().get(MessageStatusChange.class); - if (messageStatusCache.getProcessor() == null) messageStatusCache.setProcessor(evt -> { - logger.log(Level.FINER, "Sending cached " + evt); - try { - client.sendEvent(evt); - } catch (final IOException e) { - logger.log(Level.SEVERE, "Could not send cached message status change event: ", e); - } - }); - - // sending these solely local objects to the server - localDB.getCacheMap().getMap().values().forEach(Cache::relay); - } + public void flushCache() { localDB.getCacheMap().getMap().values().forEach(Cache::relay); } /** * Delivers a message to the server if online. Otherwise the message is cached diff --git a/client/src/main/java/envoy/client/ui/Startup.java b/client/src/main/java/envoy/client/ui/Startup.java index 51a929d..370215c 100644 --- a/client/src/main/java/envoy/client/ui/Startup.java +++ b/client/src/main/java/envoy/client/ui/Startup.java @@ -15,7 +15,6 @@ import javafx.stage.Stage; import envoy.client.data.*; import envoy.client.net.Client; -import envoy.client.net.WriteProxy; import envoy.client.ui.SceneContext.SceneInfo; import envoy.client.ui.controller.LoginScene; import envoy.data.*; @@ -91,7 +90,6 @@ public final class Startup extends Application { final var sceneContext = new SceneContext(stage); context.setSceneContext(sceneContext); - context.setWriteProxy(new WriteProxy()); // Perform automatic login if configured if (config.hasLoginCredentials()) @@ -174,6 +172,7 @@ public final class Startup extends Application { try { localDB.initializeUserStorage(); localDB.loadUserData(); + context.initWriteProxy(); } catch (final FileNotFoundException e) { // The local database file has not yet been created, probably first login } catch (final Exception e) {