Improved logout mechanism a bit, still pretty buggy

(and fixed some inconsistencies)
This commit is contained in:
Leon Hofmeister 2020-09-24 18:00:59 +02:00
parent 05d4917bb2
commit af219274f5
Signed by: delvh
GPG Key ID: 3DECE05F6D9A647C
8 changed files with 48 additions and 11 deletions

View File

@ -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<T> implements Consumer<T>, 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(); }
}

View File

@ -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();
}
/**

View File

@ -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;
}

View File

@ -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);

View File

@ -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; }

View File

@ -37,6 +37,7 @@ public final class Receiver extends Thread {
public Receiver(InputStream in) {
super("Receiver");
this.in = in;
setDaemon(true);
}
/**

View File

@ -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(); }

View File

@ -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);