From 86e189a40a51c397eaabc764e52647d5b59c9c4d Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 25 Sep 2020 16:03:15 +0200 Subject: [PATCH] Dispatch received events to the event bus by default --- .../main/java/envoy/client/net/Client.java | 43 ------------------- .../main/java/envoy/client/net/Receiver.java | 16 +++++-- 2 files changed, 12 insertions(+), 47 deletions(-) diff --git a/client/src/main/java/envoy/client/net/Client.java b/client/src/main/java/envoy/client/net/Client.java index 1a327b0..9e1f632 100644 --- a/client/src/main/java/envoy/client/net/Client.java +++ b/client/src/main/java/envoy/client/net/Client.java @@ -9,7 +9,6 @@ import envoy.client.data.*; import envoy.client.event.EnvoyCloseEvent; import envoy.data.*; import envoy.event.*; -import envoy.event.contact.*; import envoy.util.*; import dev.kske.eventbus.*; @@ -75,8 +74,6 @@ 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, eventBus::dispatch); - receiver.registerProcessor(NewAuthToken.class, eventBus::dispatch); rejected = false; @@ -124,52 +121,12 @@ public final class Client implements EventListener, Closeable { // Remove all processors as they are only used during the handshake receiver.removeAllProcessors(); - // Process incoming messages - receiver.registerProcessor(GroupMessage.class, eventBus::dispatch); - receiver.registerProcessor(Message.class, eventBus::dispatch); - receiver.registerProcessor(MessageStatusChange.class, eventBus::dispatch); - receiver.registerProcessor(GroupMessageStatusChange.class, eventBus::dispatch); - // Relay cached messages and message status changes cacheMap.get(Message.class).setProcessor(eventBus::dispatch); cacheMap.get(GroupMessage.class).setProcessor(eventBus::dispatch); cacheMap.get(MessageStatusChange.class).setProcessor(eventBus::dispatch); cacheMap.get(GroupMessageStatusChange.class).setProcessor(eventBus::dispatch); - // Process user status changes - receiver.registerProcessor(UserStatusChange.class, eventBus::dispatch); - - // Process message ID generation - receiver.registerProcessor(IDGenerator.class, eventBus::dispatch); - - // Process name changes - receiver.registerProcessor(NameChange.class, eventBus::dispatch); - - // Process contact searches - receiver.registerProcessor(UserSearchResult.class, eventBus::dispatch); - - // Process contact operations - receiver.registerProcessor(ContactOperation.class, eventBus::dispatch); - - // Process group size changes - receiver.registerProcessor(GroupResize.class, eventBus::dispatch); - - // Process IsTyping events - receiver.registerProcessor(IsTyping.class, eventBus::dispatch); - - // Process PasswordChangeResults - receiver.registerProcessor(PasswordChangeResult.class, eventBus::dispatch); - - // Process ProfilePicChanges - receiver.registerProcessor(ProfilePicChange.class, eventBus::dispatch); - - // Process requests to not send any more attachments as they will not be shown - // to other users - receiver.registerProcessor(NoAttachments.class, eventBus::dispatch); - - // Process group creation results - they might have been disabled on the server - receiver.registerProcessor(GroupCreationResult.class, eventBus::dispatch); - // Request a generator if none is present or the existing one is consumed if (!localDB.hasIDGenerator() || !localDB.getIDGenerator().hasNext()) requestIDGenerator(); diff --git a/client/src/main/java/envoy/client/net/Receiver.java b/client/src/main/java/envoy/client/net/Receiver.java index 2f96604..bc325a2 100644 --- a/client/src/main/java/envoy/client/net/Receiver.java +++ b/client/src/main/java/envoy/client/net/Receiver.java @@ -8,6 +8,8 @@ import java.util.logging.*; import envoy.util.*; +import dev.kske.eventbus.*; + /** * Receives objects from the server and passes them to processor objects based * on their class. @@ -22,7 +24,8 @@ public final class Receiver extends Thread { private final InputStream in; private final Map, Consumer> processors = new HashMap<>(); - private static final Logger logger = EnvoyLog.getLogger(Receiver.class); + private static final EventBus eventBus = EventBus.getInstance(); + private static final Logger logger = EnvoyLog.getLogger(Receiver.class); /** * Creates an instance of {@link Receiver}. @@ -77,9 +80,14 @@ public final class Receiver extends Thread { // Get appropriate processor @SuppressWarnings("rawtypes") final Consumer processor = processors.get(obj.getClass()); - if (processor == null) - logger.log(Level.WARNING, String.format("The received object has the %s for which no processor is defined.", obj.getClass())); - else processor.accept(obj); + + // Dispatch to the processor if present + if (processor != null) processor.accept(obj); + // Dispatch to the event bus if the object is an event without a processor + else if (obj instanceof IEvent) eventBus.dispatch((IEvent) obj); + // Notify if no processor could be located + else logger.log(Level.WARNING, + String.format("The received object has the %s for which no processor is defined.", obj.getClass())); } } catch (final SocketException | EOFException e) { // Connection probably closed by client.