Fix write proxy initialization

This commit is contained in:
Kai S. K. Engelbart 2020-09-02 09:51:08 +02:00
parent efbca9cbc9
commit 8b204b3715
4 changed files with 27 additions and 54 deletions

View File

@ -51,12 +51,6 @@ public final class Cache<T> implements Consumer<T>, Serializable {
*/ */
public void setProcessor(Consumer<T> processor) { this.processor = processor; } public void setProcessor(Consumer<T> processor) { this.processor = processor; }
/**
* @return the processor
* @since Envoy Client v0.2-beta
*/
public Consumer<T> getProcessor() { return processor; }
/** /**
* Relays all cached elements to the processor. * Relays all cached elements to the processor.
* *

View File

@ -18,24 +18,33 @@ import envoy.client.ui.SceneContext;
*/ */
public class Context { public class Context {
private WriteProxy writeProxy;
private LocalDB localDB;
private Stage stage;
private SceneContext sceneContext;
private final Client client = new Client(); 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 static final Context instance = new Context();
private Context() {}
/** /**
* @return the instance of {@code Context} used throughout Envoy * @return the instance of {@code Context} used throughout Envoy
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta
*/ */
public static Context getInstance() { return instance; } 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 * @return the localDB
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta
@ -81,14 +90,6 @@ public class Context {
*/ */
public Stage getStage() { return stage; } 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 * @param stage the stage to set
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta

View File

@ -5,7 +5,6 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import envoy.client.data.Cache; import envoy.client.data.Cache;
import envoy.client.data.Context;
import envoy.client.data.LocalDB; import envoy.client.data.LocalDB;
import envoy.data.Message; import envoy.data.Message;
import envoy.event.MessageStatusChange; import envoy.event.MessageStatusChange;
@ -25,8 +24,8 @@ import envoy.util.EnvoyLog;
*/ */
public final class WriteProxy { public final class WriteProxy {
private final Client client = Context.getInstance().getClient(); private final Client client;
private final LocalDB localDB = Context.getInstance().getLocalDB(); private final LocalDB localDB;
private static final Logger logger = EnvoyLog.getLogger(WriteProxy.class); 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 * Initializes a write proxy using a client and a local database. The
* corresponding cache processors are injected into the caches. * 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 * @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 // Initialize cache processors for messages and message status change events
localDB.getCacheMap().get(Message.class).setProcessor(msg -> { localDB.getCacheMap().get(Message.class).setProcessor(msg -> {
@ -63,32 +67,7 @@ public final class WriteProxy {
* *
* @since Envoy Client v0.3-alpha * @since Envoy Client v0.3-alpha
*/ */
public void flushCache() { public void flushCache() { localDB.getCacheMap().getMap().values().forEach(Cache::relay); }
// 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);
}
/** /**
* Delivers a message to the server if online. Otherwise the message is cached * Delivers a message to the server if online. Otherwise the message is cached

View File

@ -15,7 +15,6 @@ import javafx.stage.Stage;
import envoy.client.data.*; import envoy.client.data.*;
import envoy.client.net.Client; import envoy.client.net.Client;
import envoy.client.net.WriteProxy;
import envoy.client.ui.SceneContext.SceneInfo; import envoy.client.ui.SceneContext.SceneInfo;
import envoy.client.ui.controller.LoginScene; import envoy.client.ui.controller.LoginScene;
import envoy.data.*; import envoy.data.*;
@ -91,7 +90,6 @@ public final class Startup extends Application {
final var sceneContext = new SceneContext(stage); final var sceneContext = new SceneContext(stage);
context.setSceneContext(sceneContext); context.setSceneContext(sceneContext);
context.setWriteProxy(new WriteProxy());
// Perform automatic login if configured // Perform automatic login if configured
if (config.hasLoginCredentials()) if (config.hasLoginCredentials())
@ -174,6 +172,7 @@ public final class Startup extends Application {
try { try {
localDB.initializeUserStorage(); localDB.initializeUserStorage();
localDB.loadUserData(); localDB.loadUserData();
context.initWriteProxy();
} catch (final FileNotFoundException e) { } catch (final FileNotFoundException e) {
// The local database file has not yet been created, probably first login // The local database file has not yet been created, probably first login
} catch (final Exception e) { } catch (final Exception e) {