Fix write proxy initialization
This commit is contained in:
		@@ -51,12 +51,6 @@ public final class Cache<T> implements Consumer<T>, Serializable {
 | 
			
		||||
	 */
 | 
			
		||||
	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.
 | 
			
		||||
	 *
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user