From 0efd1e559447a62ee1439cdd0a084931fda63f37 Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 25 Sep 2020 15:56:08 +0200 Subject: [PATCH] Fold client receivers into event handlers --- .../main/java/envoy/client/data/LocalDB.java | 29 ++++++------------- .../main/java/envoy/client/net/Client.java | 11 ++++--- .../src/main/java/envoy/data/IDGenerator.java | 4 ++- 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/client/src/main/java/envoy/client/data/LocalDB.java b/client/src/main/java/envoy/client/data/LocalDB.java index 44cd639..c5e154f 100644 --- a/client/src/main/java/envoy/client/data/LocalDB.java +++ b/client/src/main/java/envoy/client/data/LocalDB.java @@ -173,26 +173,6 @@ public final class LocalDB implements EventListener { }, 2000, ClientConfig.getInstance().getLocalDBSaveInterval() * 60000); } - /** - * Performs a contact name change if the corresponding contact is present. - * - * @param event the {@link NameChange} to process - * @since Envoy Client v0.1-beta - */ - public void replaceContactName(NameChange event) { - chats.stream().map(Chat::getRecipient).filter(c -> c.getID() == event.getID()).findAny().ifPresent(c -> c.setName(event.get())); - } - - /** - * Performs a group resize operation if the corresponding group is present. - * - * @param event the {@link GroupResize} to process - * @since Envoy Client v0.1-beta - */ - public void updateGroup(GroupResize event) { - getChat(event.getGroupID()).map(Chat::getRecipient).map(Group.class::cast).ifPresent(event::apply); - } - /** * Stores all users. If the client user is specified, their chats will be stored * as well. The message id generator will also be saved if present. @@ -238,6 +218,14 @@ public final class LocalDB implements EventListener { if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.warning("Received invalid " + evt); } + @Event(priority = 150) + private void onGroupResize(GroupResize evt) { getChat(evt.getGroupID()).map(Chat::getRecipient).map(Group.class::cast).ifPresent(evt::apply); } + + @Event(priority = 150) + private void onNameChange(NameChange evt) { + chats.stream().map(Chat::getRecipient).filter(c -> c.getID() == evt.getID()).findAny().ifPresent(c -> c.setName(evt.get())); + } + /** * Stores a new authentication token. * @@ -308,6 +296,7 @@ public final class LocalDB implements EventListener { * @param idGenerator the message ID generator to set * @since Envoy Client v0.3-alpha */ + @Event(priority = 150) public void setIDGenerator(IDGenerator idGenerator) { this.idGenerator = idGenerator; } /** diff --git a/client/src/main/java/envoy/client/net/Client.java b/client/src/main/java/envoy/client/net/Client.java index 149052b..1a327b0 100644 --- a/client/src/main/java/envoy/client/net/Client.java +++ b/client/src/main/java/envoy/client/net/Client.java @@ -75,7 +75,7 @@ public final class Client implements EventListener, Closeable { // authentication token receiver.registerProcessor(User.class, sender -> this.sender = sender); receiver.registerProcessors(cacheMap.getMap()); - receiver.registerProcessor(HandshakeRejection.class, evt -> { rejected = true; eventBus.dispatch(evt); }); + receiver.registerProcessor(HandshakeRejection.class, eventBus::dispatch); receiver.registerProcessor(NewAuthToken.class, eventBus::dispatch); rejected = false; @@ -140,10 +140,10 @@ public final class Client implements EventListener, Closeable { receiver.registerProcessor(UserStatusChange.class, eventBus::dispatch); // Process message ID generation - receiver.registerProcessor(IDGenerator.class, localDB::setIDGenerator); + receiver.registerProcessor(IDGenerator.class, eventBus::dispatch); // Process name changes - receiver.registerProcessor(NameChange.class, evt -> { localDB.replaceContactName(evt); eventBus.dispatch(evt); }); + receiver.registerProcessor(NameChange.class, eventBus::dispatch); // Process contact searches receiver.registerProcessor(UserSearchResult.class, eventBus::dispatch); @@ -152,7 +152,7 @@ public final class Client implements EventListener, Closeable { receiver.registerProcessor(ContactOperation.class, eventBus::dispatch); // Process group size changes - receiver.registerProcessor(GroupResize.class, evt -> { localDB.updateGroup(evt); eventBus.dispatch(evt); }); + receiver.registerProcessor(GroupResize.class, eventBus::dispatch); // Process IsTyping events receiver.registerProcessor(IsTyping.class, eventBus::dispatch); @@ -217,6 +217,9 @@ public final class Client implements EventListener, Closeable { send(new IDGeneratorRequest()); } + @Event(eventType = HandshakeRejection.class, priority = 1000) + private void onHandshakeRejection() { rejected = true; } + @Override @Event(eventType = EnvoyCloseEvent.class, priority = 800) public void close() { diff --git a/common/src/main/java/envoy/data/IDGenerator.java b/common/src/main/java/envoy/data/IDGenerator.java index 3040538..774c348 100644 --- a/common/src/main/java/envoy/data/IDGenerator.java +++ b/common/src/main/java/envoy/data/IDGenerator.java @@ -2,13 +2,15 @@ package envoy.data; import java.io.Serializable; +import dev.kske.eventbus.IEvent; + /** * Generates increasing IDs between two numbers. * * @author Kai S. K. Engelbart * @since Envoy Common v0.2-alpha */ -public final class IDGenerator implements Serializable { +public final class IDGenerator implements IEvent, Serializable { private final long end; private long current;