Fold client receivers into event handlers

This commit is contained in:
Kai S. K. Engelbart 2020-09-25 15:56:08 +02:00
parent f6eeeee79b
commit 0efd1e5594
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
3 changed files with 19 additions and 25 deletions

View File

@ -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; }
/**

View File

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

View File

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