Current working status

This commit is contained in:
delvh 2020-07-09 09:12:41 +02:00
parent 7c5cc2f220
commit 4f654cc2a5
3 changed files with 48 additions and 133 deletions

View File

@ -3,7 +3,9 @@ package envoy.client.net;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.util.Map;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -38,6 +40,8 @@ public class Client implements Closeable {
private Receiver receiver; private Receiver receiver;
private boolean online; private boolean online;
private Map<Class<?>, Cache<?>> cacheMap;
// Asynchronously initialized during handshake // Asynchronously initialized during handshake
private volatile User sender; private volatile User sender;
private volatile boolean rejected; private volatile boolean rejected;
@ -53,33 +57,14 @@ public class Client implements Closeable {
* will block for up to 5 seconds. If the handshake does exceed this time limit, * will block for up to 5 seconds. If the handshake does exceed this time limit,
* an exception is thrown. * an exception is thrown.
* *
* @param credentials the login credentials of the * @param credentials the login credentials of the user
* user * @param cacheMap the map of all caches needed
* @param receivedMessageCache a message cache containing all
* unread messages from the server
* that can be relayed after
* initialization
* @param receivedGroupMessageCache a groupMessage cache containing
* all unread groupMessages from
* the server that can be relayed
* after initialization
* @param receivedMessageStatusChangeCache an event cache containing all
* received
* messageStatusChangeEvents from
* the server that can be relayed
* after initialization
* @param receivedGroupMessageStatusChangeCache an event cache containing all
* received
* groupMessageStatusChangeEvents
* from the server that can be
* relayed after initialization
* @throws TimeoutException if the server could not be reached * @throws TimeoutException if the server could not be reached
* @throws IOException if the login credentials could not be written * @throws IOException if the login credentials could not be written
* @throws InterruptedException if the current thread is interrupted while * @throws InterruptedException if the current thread is interrupted while
* waiting for the handshake response * waiting for the handshake response
*/ */
public void performHandshake(LoginCredentials credentials, Cache<Message> receivedMessageCache, Cache<GroupMessage> receivedGroupMessageCache, public void performHandshake(LoginCredentials credentials, Map<Class<?>, Cache<?>> cacheMap)
Cache<MessageStatusChange> receivedMessageStatusChangeCache, Cache<GroupMessageStatusChange> receivedGroupMessageStatusChangeCache)
throws TimeoutException, IOException, InterruptedException { throws TimeoutException, IOException, InterruptedException {
if (online) throw new IllegalStateException("Handshake has already been performed successfully"); if (online) throw new IllegalStateException("Handshake has already been performed successfully");
@ -93,10 +78,7 @@ public class Client implements Closeable {
// Register user creation processor, contact list processor and message cache // Register user creation processor, contact list processor and message cache
receiver.registerProcessor(User.class, sender -> this.sender = sender); receiver.registerProcessor(User.class, sender -> this.sender = sender);
receiver.registerProcessor(Message.class, receivedMessageCache); cacheMap.forEach((inputclass, cache) -> receiver.registerProcessor(inputclass, cache));
receiver.registerProcessor(GroupMessage.class, receivedGroupMessageCache);
receiver.registerProcessor(MessageStatusChange.class, receivedMessageStatusChangeCache);
receiver.registerProcessor(GroupMessageStatusChange.class, receivedGroupMessageStatusChangeCache);
receiver.registerProcessor(HandshakeRejection.class, evt -> { rejected = true; eventBus.dispatch(evt); }); receiver.registerProcessor(HandshakeRejection.class, evt -> { rejected = true; eventBus.dispatch(evt); });
rejected = false; rejected = false;
@ -135,43 +117,14 @@ public class Client implements Closeable {
* Initializes the {@link Receiver} used to process data sent from the server to * Initializes the {@link Receiver} used to process data sent from the server to
* this client. * this client.
* *
* @param localDB the local database used to * @param localDB the local database used to persist the current
* persist
* the current
* {@link IDGenerator} * {@link IDGenerator}
* @param receivedMessageCache a message cache containing all * @param cacheMap the map of all caches needed
* unread
* messages
* from the server that can be
* relayed
* after
* initialization
* @param receivedGroupMessageCache a groupMessage cache containing
* all
* unread
* groupMessages
* from the server that can be
* relayed
* after
* initialization
* @param receivedMessageStatusChangeCache an event cache containing all
* received
* messageStatusChangeEvents
* from the server that can be
* relayed
* after initialization
* @param receivedGroupMessageStatusChangeCache an event cache containing all
* received
* groupMessageStatusChangeEvents
* from the server that can be
* relayed after initialization
* @throws IOException if no {@link IDGenerator} is present and none could be * @throws IOException if no {@link IDGenerator} is present and none could be
* requested from the server * requested from the server
* @since Envoy Client v0.2-alpha * @since Envoy Client v0.2-alpha
*/ */
public void initReceiver(LocalDB localDB, Cache<Message> receivedMessageCache, Cache<GroupMessage> receivedGroupMessageCache, public void initReceiver(LocalDB localDB, Map<Class<?>, Cache<?>> cacheMap) throws IOException {
Cache<MessageStatusChange> receivedMessageStatusChangeCache, Cache<GroupMessageStatusChange> receivedGroupMessageStatusChangeCache)
throws IOException {
checkOnline(); checkOnline();
// Process incoming messages // Process incoming messages
@ -188,12 +141,11 @@ public class Client implements Closeable {
receiver.registerProcessor(GroupMessageStatusChange.class, groupMessageStatusChangeProcessor); receiver.registerProcessor(GroupMessageStatusChange.class, groupMessageStatusChangeProcessor);
// Relay cached unread messages and unread groupMessages // Relay cached unread messages and unread groupMessages
receivedMessageCache.setProcessor(receivedMessageProcessor); cacheMap.get(Message.class).setProcessor((Consumer<?>) receivedMessageProcessor);
receivedGroupMessageCache.setProcessor(receivedGroupMessageProcessor); cacheMap.get(GroupMessage.class).setProcessor((Consumer<?>) receivedGroupMessageProcessor);
// Process message status changes // Process message status changes
receivedMessageStatusChangeCache.setProcessor(messageStatusChangeProcessor); cacheMap.get(MessageStatusChange.class).setProcessor((Consumer<?>) messageStatusChangeProcessor);
receivedGroupMessageStatusChangeCache.setProcessor(groupMessageStatusChangeProcessor); cacheMap.get(GroupMessageStatusChange.class).setProcessor((Consumer<?>) groupMessageStatusChangeProcessor);
// Process user status changes // Process user status changes
receiver.registerProcessor(UserStatusChange.class, eventBus::dispatch); receiver.registerProcessor(UserStatusChange.class, eventBus::dispatch);

View File

@ -2,6 +2,7 @@ package envoy.client.ui;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -44,10 +45,6 @@ public final class Startup extends Application {
private LocalDB localDB; private LocalDB localDB;
private Client client; private Client client;
private Cache<Message> messageCache;
private Cache<GroupMessage> groupMessageCache;
private Cache<MessageStatusChange> messageStatusCache;
private Cache<GroupMessageStatusChange> groupMessageStatusCache;
private static final ClientConfig config = ClientConfig.getInstance(); private static final ClientConfig config = ClientConfig.getInstance();
private static final Logger logger = EnvoyLog.getLogger(Startup.class); private static final Logger logger = EnvoyLog.getLogger(Startup.class);
@ -102,18 +99,19 @@ public final class Startup extends Application {
// Initialize client and unread message cache // Initialize client and unread message cache
client = new Client(); client = new Client();
messageCache = new Cache<>();
groupMessageCache = new Cache<>(); final var cacheMap = new HashMap<Class<?>, Cache<?>>();
messageStatusCache = new Cache<>(); cacheMap.put(Message.class, new Cache<Message>());
groupMessageStatusCache = new Cache<>(); cacheMap.put(GroupMessage.class, new Cache<GroupMessage>());
cacheMap.put(MessageStatusChange.class, new Cache<MessageStatusChange>());
cacheMap.put(GroupMessageStatusChange.class, new Cache<GroupMessageStatusChange>());
stage.setTitle("Envoy"); stage.setTitle("Envoy");
stage.getIcons().add(IconUtil.loadIcon("envoy_logo")); stage.getIcons().add(IconUtil.loadIcon("envoy_logo"));
final var sceneContext = new SceneContext(stage); final var sceneContext = new SceneContext(stage);
sceneContext.load(SceneInfo.LOGIN_SCENE); sceneContext.load(SceneInfo.LOGIN_SCENE);
sceneContext.<LoginScene>getController() sceneContext.<LoginScene>getController().initializeData(client, localDB, cacheMap, sceneContext);
.initializeData(client, localDB, messageCache, groupMessageCache, messageStatusCache, groupMessageStatusCache, sceneContext);
} }
/** /**

View File

@ -2,6 +2,7 @@ package envoy.client.ui.controller;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -16,9 +17,11 @@ import envoy.client.net.Client;
import envoy.client.ui.ClearableTextField; import envoy.client.ui.ClearableTextField;
import envoy.client.ui.SceneContext; import envoy.client.ui.SceneContext;
import envoy.client.ui.Startup; import envoy.client.ui.Startup;
import envoy.data.*; import envoy.data.LoginCredentials;
import envoy.data.User;
import envoy.data.User.UserStatus; import envoy.data.User.UserStatus;
import envoy.event.*; import envoy.event.EventBus;
import envoy.event.HandshakeRejection;
import envoy.exception.EnvoyException; import envoy.exception.EnvoyException;
import envoy.util.Bounds; import envoy.util.Bounds;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
@ -54,10 +57,7 @@ public final class LoginScene {
private Client client; private Client client;
private LocalDB localDB; private LocalDB localDB;
private Cache<Message> receivedMessageCache; private Map<Class<?>, Cache<?>> cacheMap;
private Cache<GroupMessage> receivedGroupMessageCache;
private Cache<MessageStatusChange> receivedMessageStatusChangeCache;
private Cache<GroupMessageStatusChange> receivedGroupMessageStatusChangeCache;
private SceneContext sceneContext; private SceneContext sceneContext;
private static final Logger logger = EnvoyLog.getLogger(LoginScene.class); private static final Logger logger = EnvoyLog.getLogger(LoginScene.class);
@ -75,40 +75,16 @@ public final class LoginScene {
/** /**
* Loads the login dialog using the FXML file {@code LoginDialog.fxml}. * Loads the login dialog using the FXML file {@code LoginDialog.fxml}.
* *
* @param client the client used to perform the * @param client the client used to perform the handshake
* handshake * @param localDB the local database used for offline login
* @param localDB the local database used for * @param cacheMap the map of all caches needed
* offline * @param sceneContext the scene context used to initialize the chat scene
* login
* @param receivedMessageCache the cache storing messages
* received
* during
* the handshake
* @param receivedGroupMessageCache the cache storing groupMessages
* received during the handshake
* @param receivedMessageStatusChangeCache the cache storing
* messageStatusChangeEvents
* received
* during handshake
* @param receivedGroupMessageStatusChangeCache the cache storing
* groupMessageStatusChangeEvents
* received
* during handshake
* @param sceneContext the scene context used to
* initialize
* the chat
* scene
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public void initializeData(Client client, LocalDB localDB, Cache<Message> receivedMessageCache, Cache<GroupMessage> receivedGroupMessageCache, public void initializeData(Client client, LocalDB localDB, Map<Class<?>, Cache<?>> cacheMap, SceneContext sceneContext) {
Cache<MessageStatusChange> receivedMessageStatusChangeCache, Cache<GroupMessageStatusChange> receivedGroupMessageStatusChangeCache,
SceneContext sceneContext) {
this.client = client; this.client = client;
this.localDB = localDB; this.localDB = localDB;
this.receivedMessageCache = receivedMessageCache; this.cacheMap = cacheMap;
this.receivedGroupMessageCache = receivedGroupMessageCache;
this.receivedMessageStatusChangeCache = receivedMessageStatusChangeCache;
this.receivedGroupMessageStatusChangeCache = receivedGroupMessageStatusChangeCache;
this.sceneContext = sceneContext; this.sceneContext = sceneContext;
// Prepare handshake // Prepare handshake
@ -157,17 +133,9 @@ public final class LoginScene {
private void performHandshake(LoginCredentials credentials) { private void performHandshake(LoginCredentials credentials) {
try { try {
client.performHandshake(credentials, client.performHandshake(credentials, cacheMap);
receivedMessageCache,
receivedGroupMessageCache,
receivedMessageStatusChangeCache,
receivedGroupMessageStatusChangeCache);
if (client.isOnline()) { if (client.isOnline()) {
client.initReceiver(localDB, client.initReceiver(localDB, cacheMap);
receivedMessageCache,
receivedGroupMessageCache,
receivedMessageStatusChangeCache,
receivedGroupMessageStatusChangeCache);
loadChatScene(); loadChatScene();
} }
} catch (IOException | InterruptedException | TimeoutException e) { } catch (IOException | InterruptedException | TimeoutException e) {
@ -230,9 +198,6 @@ public final class LoginScene {
sceneContext.<ChatScene>getController().initializeData(sceneContext, localDB, client, writeProxy); sceneContext.<ChatScene>getController().initializeData(sceneContext, localDB, client, writeProxy);
// Relay unread messages from cache // Relay unread messages from cache
if (receivedMessageCache != null && client.isOnline()) receivedMessageCache.relay(); if (client.isOnline()) cacheMap.values().forEach(cache -> { if (cache != null) cache.relay(); });
if (receivedGroupMessageCache != null && client.isOnline()) receivedGroupMessageCache.relay();
if (receivedMessageStatusChangeCache != null && client.isOnline()) receivedMessageStatusChangeCache.relay();
if (receivedGroupMessageStatusChangeCache != null && client.isOnline()) receivedGroupMessageStatusChangeCache.relay();
} }
} }