MessageStatusChangeEventCache

Added a cache storing all messageStatusChangeEvents received during
handshake just like the already existing cache for received messages
during the handshake
This new cache is needed because the functionality regarding the pending
messages (and now messageStatusChanges as well) has changed!
This commit is contained in:
DieGurke 2020-06-10 22:50:09 +02:00
parent 3439aee112
commit c2a627529a
3 changed files with 28 additions and 9 deletions

View File

@ -60,13 +60,15 @@ public class Client implements Closeable {
* @param receivedMessageCache a message cache containing all unread messages * @param receivedMessageCache a message cache containing all unread messages
* from the server that can be relayed after * from the server that can be relayed after
* initialization * initialization
* @param receivedMessageStatusChangeEventCache an event cache containing all received messageStatusChangeEvents 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 * @throws IOException if the login credentials could not be
* written * 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) public void performHandshake(LoginCredentials credentials, Cache<Message> receivedMessageCache,
Cache<MessageStatusChangeEvent> receivedMessageStatusChangeEventCache)
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");
// Establish TCP connection // Establish TCP connection
@ -80,6 +82,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; contacts = sender.getContacts(); }); receiver.registerProcessor(User.class, sender -> { this.sender = sender; contacts = sender.getContacts(); });
receiver.registerProcessor(Message.class, receivedMessageCache); receiver.registerProcessor(Message.class, receivedMessageCache);
receiver.registerProcessor(MessageStatusChangeEvent.class, receivedMessageStatusChangeEventCache);
receiver.registerProcessor(HandshakeRejectionEvent.class, evt -> { rejected = true; eventBus.dispatch(evt); }); receiver.registerProcessor(HandshakeRejectionEvent.class, evt -> { rejected = true; eventBus.dispatch(evt); });
rejected = false; rejected = false;
@ -123,22 +126,27 @@ public class Client implements Closeable {
* @param receivedMessageCache a message cache containing all unread messages * @param receivedMessageCache a message cache containing all unread messages
* from the server that can be relayed after * from the server that can be relayed after
* initialization * initialization
* @param receivedMessageStatusChangeEventCache an event cache containing all received messageStatusChangeEvents 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) throws IOException { public void initReceiver(LocalDB localDB, Cache<Message> receivedMessageCache,
Cache<MessageStatusChangeEvent> receivedMessageStatusChangeEventCache) throws IOException {
checkOnline(); checkOnline();
// Process incoming messages // Process incoming messages
final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor(); final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor();
final MessageStatusChangeEventProcessor messageStatusChangeEventProcessor = new MessageStatusChangeEventProcessor();
receiver.registerProcessor(Message.class, receivedMessageProcessor); receiver.registerProcessor(Message.class, receivedMessageProcessor);
// Relay cached unread messages // Relay cached unread messages
receivedMessageCache.setProcessor(receivedMessageProcessor); receivedMessageCache.setProcessor(receivedMessageProcessor);
// Process message status changes // Process message status changes
receiver.registerProcessor(MessageStatusChangeEvent.class, new MessageStatusChangeEventProcessor()); receiver.registerProcessor(MessageStatusChangeEvent.class, messageStatusChangeEventProcessor);
receivedMessageStatusChangeEventCache.setProcessor(messageStatusChangeEventProcessor);
// Process user status changes // Process user status changes
receiver.registerProcessor(UserStatusChangeEvent.class, eventBus::dispatch); receiver.registerProcessor(UserStatusChangeEvent.class, eventBus::dispatch);

View File

@ -16,6 +16,7 @@ import envoy.client.net.Client;
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.Message; import envoy.data.Message;
import envoy.event.MessageStatusChangeEvent;
import envoy.exception.EnvoyException; import envoy.exception.EnvoyException;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
@ -27,13 +28,15 @@ import envoy.util.EnvoyLog;
* Created: <strong>26.03.2020</strong><br> * Created: <strong>26.03.2020</strong><br>
* *
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public final class Startup extends Application { public final class Startup extends Application {
private LocalDB localDB; private LocalDB localDB;
private Client client; private Client client;
private Cache<Message> cache; private Cache<Message> messageCache;
private Cache<MessageStatusChangeEvent> messageStatusCache;
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);
@ -87,14 +90,15 @@ public final class Startup extends Application {
// Initialize client and unread message cache // Initialize client and unread message cache
client = new Client(); client = new Client();
cache = new Cache<>(); messageCache = new Cache<>();
messageStatusCache = new Cache<>();
stage.setTitle("Envoy"); stage.setTitle("Envoy");
stage.getIcons().add(IconUtil.load("/icons/envoy_logo.png")); stage.getIcons().add(IconUtil.load("/icons/envoy_logo.png"));
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().initializeData(client, localDB, cache, sceneContext); sceneContext.<LoginScene>getController().initializeData(client, localDB, messageCache, messageStatusCache, sceneContext);
} }
/** /**

View File

@ -21,6 +21,7 @@ import envoy.data.User;
import envoy.data.User.UserStatus; import envoy.data.User.UserStatus;
import envoy.event.EventBus; import envoy.event.EventBus;
import envoy.event.HandshakeRejectionEvent; import envoy.event.HandshakeRejectionEvent;
import envoy.event.MessageStatusChangeEvent;
import envoy.exception.EnvoyException; import envoy.exception.EnvoyException;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
@ -30,6 +31,7 @@ import envoy.util.EnvoyLog;
* Created: <strong>03.04.2020</strong><br> * Created: <strong>03.04.2020</strong><br>
* *
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public final class LoginScene { public final class LoginScene {
@ -55,6 +57,7 @@ public final class LoginScene {
private Client client; private Client client;
private LocalDB localDB; private LocalDB localDB;
private Cache<Message> receivedMessageCache; private Cache<Message> receivedMessageCache;
private Cache<MessageStatusChangeEvent> receivedMessageStatusChangeEventCache;
private SceneContext sceneContext; private SceneContext sceneContext;
private static final Logger logger = EnvoyLog.getLogger(LoginScene.class); private static final Logger logger = EnvoyLog.getLogger(LoginScene.class);
@ -77,14 +80,17 @@ public final class LoginScene {
* @param localDB the local database used for offline login * @param localDB the local database used for offline login
* @param receivedMessageCache the cache storing messages received during * @param receivedMessageCache the cache storing messages received during
* the handshake * the handshake
* @param receivedMessageStatusChangeEventCache the cache storing messageStatusChangeEvents received during handshake
* @param sceneContext the scene context used to initialize the chat * @param sceneContext the scene context used to initialize the chat
* scene * scene
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public void initializeData(Client client, LocalDB localDB, Cache<Message> receivedMessageCache, SceneContext sceneContext) { public void initializeData(Client client, LocalDB localDB, Cache<Message> receivedMessageCache,
Cache<MessageStatusChangeEvent> receivedMessageStatusChangeEventCache, SceneContext sceneContext) {
this.client = client; this.client = client;
this.localDB = localDB; this.localDB = localDB;
this.receivedMessageCache = receivedMessageCache; this.receivedMessageCache = receivedMessageCache;
this.receivedMessageStatusChangeEventCache = receivedMessageStatusChangeEventCache;
this.sceneContext = sceneContext; this.sceneContext = sceneContext;
// Prepare handshake // Prepare handshake
@ -129,9 +135,9 @@ public final class LoginScene {
private void performHandshake(LoginCredentials credentials) { private void performHandshake(LoginCredentials credentials) {
try { try {
client.performHandshake(credentials, receivedMessageCache); client.performHandshake(credentials, receivedMessageCache, receivedMessageStatusChangeEventCache);
if (client.isOnline()) { if (client.isOnline()) {
client.initReceiver(localDB, receivedMessageCache); client.initReceiver(localDB, receivedMessageCache, receivedMessageStatusChangeEventCache);
loadChatScene(); loadChatScene();
} }
} catch (IOException | InterruptedException | TimeoutException e) { } catch (IOException | InterruptedException | TimeoutException e) {
@ -193,6 +199,7 @@ public final class LoginScene {
// Relay unread messages from cache // Relay unread messages from cache
if (receivedMessageCache != null && client.isOnline()) receivedMessageCache.relay(); if (receivedMessageCache != null && client.isOnline()) receivedMessageCache.relay();
if (receivedMessageStatusChangeEventCache != null && client.isOnline()) receivedMessageStatusChangeEventCache.relay();
} }
private void clearPasswordFields() { private void clearPasswordFields() {