diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index f9ca937..b63910d 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -7,9 +7,7 @@ import java.util.concurrent.TimeoutException; import java.util.logging.Level; import java.util.logging.Logger; -import envoy.client.data.CacheMap; -import envoy.client.data.ClientConfig; -import envoy.client.data.LocalDB; +import envoy.client.data.*; import envoy.client.event.SendEvent; import envoy.data.*; import envoy.event.*; @@ -60,9 +58,7 @@ public class Client implements Closeable { * @throws InterruptedException if the current thread is interrupted while * waiting for the handshake response */ - public void performHandshake(LoginCredentials credentials, - CacheMap cacheMap) - throws TimeoutException, IOException, InterruptedException { + public void performHandshake(LoginCredentials credentials, CacheMap cacheMap) throws TimeoutException, IOException, InterruptedException { if (online) throw new IllegalStateException("Handshake has already been performed successfully"); // Establish TCP connection @@ -104,9 +100,6 @@ public class Client implements Closeable { online = true; - // Remove all processors as they are only used during the handshake - receiver.removeAllProcessors(); - logger.log(Level.INFO, "Handshake completed."); } @@ -124,22 +117,23 @@ public class Client implements Closeable { public void initReceiver(LocalDB localDB, CacheMap cacheMap) throws IOException { checkOnline(); + // Remove all processors as they are only used during the handshake + receiver.removeAllProcessors(); + // Process incoming messages - final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor(); - final ReceivedGroupMessageProcessor receivedGroupMessageProcessor = new ReceivedGroupMessageProcessor(); - final MessageStatusChangeProcessor messageStatusChangeProcessor = new MessageStatusChangeProcessor(); - final GroupMessageStatusChangeProcessor groupMessageStatusChangeProcessor = new GroupMessageStatusChangeProcessor(); + final var receivedMessageProcessor = new ReceivedMessageProcessor(); + final var receivedGroupMessageProcessor = new ReceivedGroupMessageProcessor(); + final var messageStatusChangeProcessor = new MessageStatusChangeProcessor(); + final var groupMessageStatusChangeProcessor = new GroupMessageStatusChangeProcessor(); receiver.registerProcessor(GroupMessage.class, receivedGroupMessageProcessor); receiver.registerProcessor(Message.class, receivedMessageProcessor); receiver.registerProcessor(MessageStatusChange.class, messageStatusChangeProcessor); receiver.registerProcessor(GroupMessageStatusChange.class, groupMessageStatusChangeProcessor); - // Relay cached unread messages and unread groupMessages + // Relay caches cacheMap.get(Message.class).setProcessor(receivedMessageProcessor); cacheMap.get(GroupMessage.class).setProcessor(receivedGroupMessageProcessor); - - // Relay cached status changes cacheMap.get(MessageStatusChange.class).setProcessor(messageStatusChangeProcessor); cacheMap.get(GroupMessageStatusChange.class).setProcessor(groupMessageStatusChangeProcessor); @@ -172,18 +166,10 @@ 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(); - } - /** - * Creates a new write proxy that uses this client to communicate with the - * server. - * - * @param localDB the local database that the write proxy will use to access - * caches - * @return a new write proxy - * @since Envoy Client v0.3-alpha - */ - public WriteProxy createWriteProxy(LocalDB localDB) { return new WriteProxy(this, localDB); } + // Relay caches + cacheMap.getMap().values().forEach(Cache::relay); + } /** * Sends a message to the server. The message's status will be incremented once diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index 7dfb467..b29613a 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -13,6 +13,7 @@ import javafx.scene.control.Alert.AlertType; import envoy.client.data.*; import envoy.client.net.Client; +import envoy.client.net.WriteProxy; import envoy.client.ui.ClearableTextField; import envoy.client.ui.SceneContext; import envoy.client.ui.Startup; @@ -134,8 +135,8 @@ public final class LoginScene { try { client.performHandshake(credentials, cacheMap); if (client.isOnline()) { - client.initReceiver(localDB, cacheMap); loadChatScene(); + client.initReceiver(localDB, cacheMap); } } catch (IOException | InterruptedException | TimeoutException e) { logger.log(Level.INFO, "Could not connect to server. Entering offline mode..."); @@ -175,7 +176,7 @@ public final class LoginScene { } // Initialize write proxy - final var writeProxy = client.createWriteProxy(localDB); + final var writeProxy = new WriteProxy(client, localDB); localDB.synchronize(); @@ -195,8 +196,5 @@ public final class LoginScene { sceneContext.getStage().setMinWidth(350); sceneContext.load(SceneContext.SceneInfo.CHAT_SCENE); sceneContext.getController().initializeData(sceneContext, localDB, client, writeProxy); - - // Relay the caches if online - if (client.isOnline()) cacheMap.getMap().values().forEach(Cache::relay); } }