Refactoring #55

Merged
kske merged 7 commits from refactoring into develop 2020-09-27 12:06:39 +02:00
2 changed files with 12 additions and 47 deletions
Showing only changes of commit 86e189a40a - Show all commits

View File

@ -9,7 +9,6 @@ import envoy.client.data.*;
import envoy.client.event.EnvoyCloseEvent; import envoy.client.event.EnvoyCloseEvent;
import envoy.data.*; import envoy.data.*;
import envoy.event.*; import envoy.event.*;
import envoy.event.contact.*;
import envoy.util.*; import envoy.util.*;
import dev.kske.eventbus.*; import dev.kske.eventbus.*;
@ -75,8 +74,6 @@ public final class Client implements EventListener, Closeable {
// authentication token // authentication token
receiver.registerProcessor(User.class, sender -> this.sender = sender); receiver.registerProcessor(User.class, sender -> this.sender = sender);
receiver.registerProcessors(cacheMap.getMap()); receiver.registerProcessors(cacheMap.getMap());
receiver.registerProcessor(HandshakeRejection.class, eventBus::dispatch);
receiver.registerProcessor(NewAuthToken.class, eventBus::dispatch);
rejected = false; rejected = false;
@ -124,52 +121,12 @@ public final class Client implements EventListener, Closeable {
// Remove all processors as they are only used during the handshake // Remove all processors as they are only used during the handshake
receiver.removeAllProcessors(); 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 // Relay cached messages and message status changes
cacheMap.get(Message.class).setProcessor(eventBus::dispatch); cacheMap.get(Message.class).setProcessor(eventBus::dispatch);
cacheMap.get(GroupMessage.class).setProcessor(eventBus::dispatch); cacheMap.get(GroupMessage.class).setProcessor(eventBus::dispatch);
cacheMap.get(MessageStatusChange.class).setProcessor(eventBus::dispatch); cacheMap.get(MessageStatusChange.class).setProcessor(eventBus::dispatch);
cacheMap.get(GroupMessageStatusChange.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 // Request a generator if none is present or the existing one is consumed
if (!localDB.hasIDGenerator() || !localDB.getIDGenerator().hasNext()) requestIDGenerator(); if (!localDB.hasIDGenerator() || !localDB.getIDGenerator().hasNext()) requestIDGenerator();

View File

@ -8,6 +8,8 @@ import java.util.logging.*;
import envoy.util.*; import envoy.util.*;
import dev.kske.eventbus.*;
/** /**
* Receives objects from the server and passes them to processor objects based * Receives objects from the server and passes them to processor objects based
* on their class. * on their class.
@ -22,7 +24,8 @@ public final class Receiver extends Thread {
private final InputStream in; private final InputStream in;
private final Map<Class<?>, Consumer<?>> processors = new HashMap<>(); private final Map<Class<?>, 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}. * Creates an instance of {@link Receiver}.
@ -77,9 +80,14 @@ public final class Receiver extends Thread {
// Get appropriate processor // Get appropriate processor
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
final Consumer processor = processors.get(obj.getClass()); 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())); // Dispatch to the processor if present
else processor.accept(obj); 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) { } catch (final SocketException | EOFException e) {
// Connection probably closed by client. // Connection probably closed by client.