Dispatch received events to the event bus by default

This commit is contained in:
Kai S. K. Engelbart 2020-09-25 16:03:15 +02:00
parent 0efd1e5594
commit 86e189a40a
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
2 changed files with 12 additions and 47 deletions

View File

@ -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();

View File

@ -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,6 +24,7 @@ public final class Receiver extends Thread {
private final InputStream in;
private final Map<Class<?>, Consumer<?>> processors = new HashMap<>();
private static final EventBus eventBus = EventBus.getInstance();
private static final Logger logger = EnvoyLog.getLogger(Receiver.class);
/**
@ -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.