From af219274f5b7e221209d26eb5490e391303e552a Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 24 Sep 2020 18:00:59 +0200 Subject: [PATCH] Improved logout mechanism a bit, still pretty buggy (and fixed some inconsistencies) --- .../main/java/envoy/client/data/Cache.java | 13 +++++++++---- .../main/java/envoy/client/data/LocalDB.java | 19 +++++++++++++++++-- .../main/java/envoy/client/event/Logout.java | 2 +- .../envoy/client/helper/ShutdownHelper.java | 4 ++++ .../main/java/envoy/client/net/Client.java | 6 ++++-- .../main/java/envoy/client/net/Receiver.java | 1 + .../java/envoy/client/ui/SceneContext.java | 9 ++++++++- .../envoy/client/ui/controller/ChatScene.java | 5 ++++- 8 files changed, 48 insertions(+), 11 deletions(-) diff --git a/client/src/main/java/envoy/client/data/Cache.java b/client/src/main/java/envoy/client/data/Cache.java index 25b985e..0c37f5f 100644 --- a/client/src/main/java/envoy/client/data/Cache.java +++ b/client/src/main/java/envoy/client/data/Cache.java @@ -1,11 +1,9 @@ package envoy.client.data; import java.io.Serializable; -import java.util.LinkedList; -import java.util.Queue; +import java.util.*; import java.util.function.Consumer; -import java.util.logging.Level; -import java.util.logging.Logger; +import java.util.logging.*; import envoy.util.EnvoyLog; @@ -62,4 +60,11 @@ public final class Cache implements Consumer, Serializable { elements.forEach(processor::accept); elements.clear(); } + + /** + * Clears this cache of all stored elements. + * + * @since Envoy Client v0.2-beta + */ + public void clear() { elements.clear(); } } diff --git a/client/src/main/java/envoy/client/data/LocalDB.java b/client/src/main/java/envoy/client/data/LocalDB.java index 524d086..5cf6bba 100644 --- a/client/src/main/java/envoy/client/data/LocalDB.java +++ b/client/src/main/java/envoy/client/data/LocalDB.java @@ -40,6 +40,10 @@ public final class LocalDB implements EventListener { private CacheMap cacheMap = new CacheMap(); private String authToken; + // auto save Timer + private Timer autoSaver; + private boolean autoSaveRestart = true; + // State management private Instant lastSync = Instant.EPOCH; @@ -167,7 +171,14 @@ public final class LocalDB implements EventListener { * @since Envoy Client v0.2-beta */ public void initAutoSave() { - new Timer("LocalDB Autosave", true).schedule(new TimerTask() { + + if (autoSaveRestart) { + // A logout happened so the timer should be restarted + autoSaver = new Timer("LocalDB Autosave", true); + autoSaveRestart = false; + } + + autoSaver.schedule(new TimerTask() { @Override public void run() { save(); } @@ -219,12 +230,16 @@ public final class LocalDB implements EventListener { */ @Event(eventType = Logout.class, priority = 100) public void onLogout() { + autoSaver.cancel(); + autoSaveRestart = true; lastLoginFile.delete(); userFile = null; user = null; authToken = null; chats.clear(); - cacheMap = new CacheMap(); + lastSync = Instant.EPOCH; + cacheMap.getMap().forEach((key, cache) -> cache.clear()); + // cacheMap = new CacheMap(); } /** diff --git a/client/src/main/java/envoy/client/event/Logout.java b/client/src/main/java/envoy/client/event/Logout.java index e845fb1..45e2a20 100644 --- a/client/src/main/java/envoy/client/event/Logout.java +++ b/client/src/main/java/envoy/client/event/Logout.java @@ -8,7 +8,7 @@ import envoy.event.Event.Valueless; * @author leon * @since Envoy Client v0.2-beta */ -public class Logout extends Valueless { +public final class Logout extends Valueless { private static final long serialVersionUID = 1L; } diff --git a/client/src/main/java/envoy/client/helper/ShutdownHelper.java b/client/src/main/java/envoy/client/helper/ShutdownHelper.java index 812c518..e871936 100644 --- a/client/src/main/java/envoy/client/helper/ShutdownHelper.java +++ b/client/src/main/java/envoy/client/helper/ShutdownHelper.java @@ -1,11 +1,14 @@ package envoy.client.helper; +import java.util.logging.Level; + import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import envoy.client.data.*; import envoy.client.event.*; import envoy.client.ui.SceneContext.SceneInfo; +import envoy.util.EnvoyLog; import dev.kske.eventbus.EventBus; @@ -47,6 +50,7 @@ public class ShutdownHelper { alert.setContentText("Are you sure you want to log out?"); AlertHelper.confirmAction(alert, () -> { + EnvoyLog.getLogger(ShutdownHelper.class).log(Level.INFO, "A logout was requested"); EventBus.getInstance().dispatch(new EnvoyCloseEvent()); EventBus.getInstance().dispatch(new Logout()); Context.getInstance().getSceneContext().load(SceneInfo.LOGIN_SCENE); diff --git a/client/src/main/java/envoy/client/net/Client.java b/client/src/main/java/envoy/client/net/Client.java index b2247b3..1833109 100644 --- a/client/src/main/java/envoy/client/net/Client.java +++ b/client/src/main/java/envoy/client/net/Client.java @@ -180,7 +180,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); @@ -204,6 +204,7 @@ public final class Client implements EventListener, Closeable { * * @param evt the event to send * @throws IOException if the event did not reach the server + * @since Envoy Client v0.3-alpha */ public void sendEvent(Event evt) throws IOException { if (online) writeObject(evt); } @@ -213,7 +214,7 @@ public final class Client implements EventListener, Closeable { * @throws IOException if the request does not reach the server * @since Envoy Client v0.3-alpha */ - public void requestIdGenerator() throws IOException { + public void requestIDGenerator() throws IOException { logger.log(Level.INFO, "Requesting new id generator..."); writeObject(new IDGeneratorRequest()); } @@ -271,6 +272,7 @@ public final class Client implements EventListener, Closeable { /** * @return the {@link Receiver} used by this {@link Client} + * @since v0.2-alpha */ public Receiver getReceiver() { return receiver; } diff --git a/client/src/main/java/envoy/client/net/Receiver.java b/client/src/main/java/envoy/client/net/Receiver.java index ac806ad..8e1467b 100644 --- a/client/src/main/java/envoy/client/net/Receiver.java +++ b/client/src/main/java/envoy/client/net/Receiver.java @@ -37,6 +37,7 @@ public final class Receiver extends Thread { public Receiver(InputStream in) { super("Receiver"); this.in = in; + setDaemon(true); } /** diff --git a/client/src/main/java/envoy/client/ui/SceneContext.java b/client/src/main/java/envoy/client/ui/SceneContext.java index c37b51d..d5b280c 100644 --- a/client/src/main/java/envoy/client/ui/SceneContext.java +++ b/client/src/main/java/envoy/client/ui/SceneContext.java @@ -11,7 +11,7 @@ import javafx.scene.input.*; import javafx.stage.Stage; import envoy.client.data.Settings; -import envoy.client.event.ThemeChangeEvent; +import envoy.client.event.*; import envoy.client.helper.ShutdownHelper; import envoy.util.EnvoyLog; @@ -97,6 +97,7 @@ public final class SceneContext implements EventListener { * @since Envoy Client v0.1-beta */ public void load(SceneInfo sceneInfo) { + EnvoyLog.getLogger(SceneContext.class).log(Level.FINER, "Loading scene " + sceneInfo); loader.setRoot(null); loader.setController(null); @@ -166,6 +167,12 @@ public final class SceneContext implements EventListener { } } + @Event(eventType = Logout.class, priority = 150) + private void onLogout() { + sceneStack.clear(); + controllerStack.clear(); + } + @Event(priority = 150, eventType = ThemeChangeEvent.class) private void onThemeChange() { applyCSS(); } 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 834c26c..ec40146 100644 --- a/client/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/client/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -325,6 +325,9 @@ public final class ChatScene implements EventListener, Restorable { else recipientProfilePic.setImage(IconUtil.loadIconThemeSensitive("group_icon", 43)); } + @Event(eventType = Logout.class, priority = 200) + private void onLogout() { eventBus.removeListener(this); } + /** * Initializes all {@code SystemCommands} used in {@code ChatScene}. * @@ -711,7 +714,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(); } catch (final IOException e) { logger.log(Level.SEVERE, "Error while sending message: ", e);