Refactoring #55

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

View File

@ -5,10 +5,11 @@ import java.nio.channels.*;
import java.nio.file.StandardOpenOption;
import java.time.Instant;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.*;
import envoy.client.event.EnvoyCloseEvent;
import envoy.data.*;
import envoy.data.Message.MessageStatus;
import envoy.event.*;
import envoy.exception.EnvoyException;
import envoy.util.*;
@ -45,6 +46,8 @@ public final class LocalDB implements EventListener {
private final File dbDir, idGeneratorFile, lastLoginFile, usersFile;
private static final Logger logger = EnvoyLog.getLogger(LocalDB.class);
/**
* Constructs an empty local database.
*
@ -219,6 +222,22 @@ public final class LocalDB implements EventListener {
}
}
@Event(priority = 150)
private void onMessage(Message msg) { if (msg.getStatus() == MessageStatus.SENT) msg.nextStatus(); }
@Event(priority = 150)
private void onGroupMessage(GroupMessage msg) {
// TODO: Cancel event once EventBus is updated
if (msg.getStatus() == MessageStatus.WAITING || msg.getStatus() == MessageStatus.READ)
logger.warning("The groupMessage has the unexpected status " + msg.getStatus());
}
@Event(priority = 150, includeSubtypes = true)
private void onMessageStatusChange(MessageStatusChange evt) {
// TODO: Cancel event once EventBus is updated
if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.warning("Received invalid " + evt);
}
/**
* Stores a new authentication token.
*

View File

@ -125,21 +125,16 @@ public final class Client implements EventListener, Closeable {
receiver.removeAllProcessors();
// Process incoming messages
final var receivedMessageProcessor = new ReceivedMessageProcessor();
final var receivedGroupMessageProcessor = new ReceivedGroupMessageProcessor();
final var messageStatusChangeProcessor = new MessageStatusChangeProcessor();
final var groupMessageStatusChangeProcessor = new GroupMessageStatusChangeProcessor();
receiver.registerProcessor(GroupMessage.class, receivedGroupMessageProcessor);
receiver.registerProcessor(Message.class, receivedMessageProcessor);
receiver.registerProcessor(MessageStatusChange.class, messageStatusChangeProcessor);
receiver.registerProcessor(GroupMessageStatusChange.class, groupMessageStatusChangeProcessor);
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(receivedMessageProcessor);
cacheMap.get(GroupMessage.class).setProcessor(receivedGroupMessageProcessor);
cacheMap.get(MessageStatusChange.class).setProcessor(messageStatusChangeProcessor);
cacheMap.get(GroupMessageStatusChange.class).setProcessor(groupMessageStatusChangeProcessor);
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);
@ -176,7 +171,7 @@ public final class Client implements EventListener, Closeable {
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();
if (!localDB.hasIDGenerator() || !localDB.getIDGenerator().hasNext()) requestIDGenerator();
// Relay caches
cacheMap.getMap().values().forEach(Cache::relay);
@ -190,7 +185,7 @@ public final class Client implements EventListener, Closeable {
* @throws RuntimeException if the object serialization failed
* @since Envoy Client v0.2-beta
*/
public void send(Object obj) throws IllegalStateException, RuntimeException {
public void send(Serializable obj) throws IllegalStateException, RuntimeException {
checkOnline();
logger.log(Level.FINE, "Sending " + obj);
try {
@ -217,7 +212,7 @@ public final class Client implements EventListener, Closeable {
*
* @since Envoy Client v0.3-alpha
*/
public void requestIdGenerator() {
public void requestIDGenerator() {
logger.log(Level.INFO, "Requesting new id generator...");
send(new IDGeneratorRequest());
}

View File

@ -1,25 +0,0 @@
package envoy.client.net;
import java.util.function.Consumer;
import java.util.logging.Logger;
import envoy.data.Message.MessageStatus;
import envoy.event.GroupMessageStatusChange;
import envoy.util.EnvoyLog;
import dev.kske.eventbus.EventBus;
/**
* @author Maximilian K&auml;fer
* @since Envoy Client v0.1-beta
*/
public final class GroupMessageStatusChangeProcessor implements Consumer<GroupMessageStatusChange> {
private static final Logger logger = EnvoyLog.getLogger(GroupMessageStatusChangeProcessor.class);
@Override
public void accept(GroupMessageStatusChange evt) {
if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.warning("Received invalid group message status change " + evt);
else EventBus.getInstance().dispatch(evt);
}
}

View File

@ -1,32 +0,0 @@
package envoy.client.net;
import java.util.function.Consumer;
import java.util.logging.Logger;
import envoy.data.Message.MessageStatus;
import envoy.event.MessageStatusChange;
import envoy.util.EnvoyLog;
import dev.kske.eventbus.EventBus;
/**
* @author Kai S. K. Engelbart
* @since Envoy Client v0.3-alpha
*/
public final class MessageStatusChangeProcessor implements Consumer<MessageStatusChange> {
private static final Logger logger = EnvoyLog.getLogger(MessageStatusChangeProcessor.class);
/**
* Dispatches a {@link MessageStatusChange} if the status is
* {@code RECEIVED} or {@code READ}.
*
* @param evt the status change event
* @since Envoy Client v0.3-alpha
*/
@Override
public void accept(MessageStatusChange evt) {
if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.warning("Received invalid message status change " + evt);
else EventBus.getInstance().dispatch(evt);
}
}

View File

@ -1,29 +0,0 @@
package envoy.client.net;
import java.util.function.Consumer;
import java.util.logging.Logger;
import envoy.data.GroupMessage;
import envoy.data.Message.MessageStatus;
import envoy.util.EnvoyLog;
import dev.kske.eventbus.EventBus;
/**
* @author Maximilian K&auml;fer
* @since Envoy Client v0.1-beta
*/
public final class ReceivedGroupMessageProcessor implements Consumer<GroupMessage> {
private static final Logger logger = EnvoyLog.getLogger(ReceivedGroupMessageProcessor.class);
@Override
public void accept(GroupMessage groupMessage) {
if (groupMessage.getStatus() == MessageStatus.WAITING || groupMessage.getStatus() == MessageStatus.READ)
logger.warning("The groupMessage has the unexpected status " + groupMessage.getStatus());
// Dispatch event
EventBus.getInstance().dispatch(groupMessage);
}
}

View File

@ -1,24 +0,0 @@
package envoy.client.net;
import java.util.function.Consumer;
import envoy.data.Message;
import envoy.data.Message.MessageStatus;
import dev.kske.eventbus.EventBus;
/**
* @author Kai S. K. Engelbart
* @since Envoy Client v0.3-alpha
*/
public final class ReceivedMessageProcessor implements Consumer<Message> {
@Override
public void accept(Message message) {
// Update status to RECEIVED
if (message.getStatus() == MessageStatus.SENT) message.nextStatus();
// Dispatch message
EventBus.getInstance().dispatch(message);
}
}

View File

@ -694,7 +694,7 @@ public final class ChatScene implements EventListener, Restorable {
scrollToMessageListEnd();
// Request a new ID generator if all IDs were used
if (!localDB.getIDGenerator().hasNext() && client.isOnline()) client.requestIdGenerator();
if (!localDB.getIDGenerator().hasNext() && client.isOnline()) client.requestIDGenerator();
}
// Clear text field and disable post button

View File

@ -15,8 +15,7 @@ public final class ReflectionUtil {
/**
* Gets all declared variable values of the given instance that have the
* specified
* class.
* specified class.
* <p>
* (i.e. can get all {@code JComponents} (Swing) or {@code Nodes} (JavaFX) in a
* GUI class).