diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 6199b3b..0d6a9d3 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -51,16 +51,17 @@ public class Client implements Closeable { * will block for up to 5 seconds. If the handshake does exceed this time limit, * an exception is thrown. * - * @param credentials the login credentials of the user - * @param localDb the local database used to persist the current - * {@link IdGenerator} - * @return a message cache containing all unread messages from the server that - * can be relayed after initialization + * @param credentials the login credentials of the user + * @param localDb the local database used to persist the current + * {@link IdGenerator} + * @param receivedMessageCache a message cache containing all unread messages + * from the server that can be relayed after + * initialization * @throws Exception if the online mode could not be entered or the request * failed for some other reason * @since Envoy v0.2-alpha */ - public Cache onlineInit(LoginCredentials credentials, LocalDb localDb) throws Exception { + public void onlineInit(LoginCredentials credentials, LocalDb localDb, Cache receivedMessageCache) throws Exception { // Establish TCP connection logger.info(String.format("Attempting connection to server %s:%d...", config.getServer(), config.getPort())); socket = new Socket(config.getServer(), config.getPort()); @@ -69,13 +70,10 @@ public class Client implements Closeable { // Create message receiver receiver = new Receiver(socket.getInputStream()); - // Create cache for unread messages - final Cache cache = new Cache<>(); - // Register user creation processor, contact list processor and message cache receiver.registerProcessor(User.class, sender -> { logger.info("Acquired user object " + sender); this.sender = sender; }); receiver.registerProcessor(Contacts.class, contacts -> { logger.info("Acquired contacts object " + contacts); this.contacts = contacts; }); - receiver.registerProcessor(Message.class, cache); + receiver.registerProcessor(Message.class, receivedMessageCache); // Start receiver new Thread(receiver).start(); @@ -104,7 +102,7 @@ public class Client implements Closeable { receiver.registerProcessor(Message.class, receivedMessageProcessor); // Relay cached unread messages - cache.setProcessor(receivedMessageProcessor); + receivedMessageCache.setProcessor(receivedMessageProcessor); // Process message status changes receiver.registerProcessor(MessageStatusChangeEvent.class, new MessageStatusChangeEventProcessor()); @@ -117,8 +115,6 @@ public class Client implements Closeable { // Request a generator if none is present or the existing one is consumed if (!localDb.hasIdGenerator() || !localDb.getIdGenerator().hasNext()) requestIdGenerator(); - - return cache; } /** diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 13f8676..a976824 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -106,11 +106,11 @@ public class Startup { // Acquire the client user (with ID) either from the server or from the local // database, which triggers offline mode Client client = new Client(); - Cache cache = null; + Cache cache = new Cache<>(); try { // Try entering online mode first localDb.loadIdGenerator(); - cache = client.onlineInit(credentials, localDb); + client.onlineInit(credentials, localDb, cache); } catch (Exception e1) { logger.warning("Could not connect to server. Trying offline mode..."); e1.printStackTrace(); @@ -162,6 +162,9 @@ public class Startup { try { chatWindow.initContent(client, localDb, writeProxy); + // Relay unread messages from cache + if (cache != null) cache.relay(); + try { new StatusTrayIcon(chatWindow).show(); @@ -180,9 +183,6 @@ public class Startup { } }); - // Relay unread messages from cache - if (cache != null) cache.relay(); - // Save Settings and PersistentLocalDb on shutdown Runtime.getRuntime().addShutdownHook(new Thread(() -> { try {