Shorten event names, compatibility verification

This commit is contained in:
Kai S. K. Engelbart 2020-06-20 10:00:38 +02:00
parent 6b204ca8db
commit e50078cc35
13 changed files with 134 additions and 109 deletions

View File

@ -28,7 +28,7 @@
<dependency> <dependency>
<groupId>com.github.informatik-ag-ngl</groupId> <groupId>com.github.informatik-ag-ngl</groupId>
<artifactId>envoy-common</artifactId> <artifactId>envoy-common</artifactId>
<version>develop-SNAPSHOT</version> <version>f~compatibility_verification-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.openjfx</groupId> <groupId>org.openjfx</groupId>

View File

@ -9,7 +9,7 @@ import java.util.Objects;
import envoy.client.net.WriteProxy; import envoy.client.net.WriteProxy;
import envoy.data.*; import envoy.data.*;
import envoy.data.Message.MessageStatus; import envoy.data.Message.MessageStatus;
import envoy.event.MessageStatusChangeEvent; import envoy.event.MessageStatusChange;
/** /**
* Represents a chat between two {@link User}s <br> * Represents a chat between two {@link User}s <br>
@ -71,7 +71,7 @@ public final class Chat implements Serializable {
* *
* @param writeProxy the write proxy instance used to notify the server about * @param writeProxy the write proxy instance used to notify the server about
* the message status changes * the message status changes
* @throws IOException if a {@link MessageStatusChangeEvent} could not be * @throws IOException if a {@link MessageStatusChange} could not be
* delivered to the server * delivered to the server
* @since Envoy Client v0.3-alpha * @since Envoy Client v0.3-alpha
*/ */
@ -81,7 +81,7 @@ public final class Chat implements Serializable {
if (m.getSenderID() == recipient.getID()) if (m.getStatus() == MessageStatus.READ) break; if (m.getSenderID() == recipient.getID()) if (m.getStatus() == MessageStatus.READ) break;
else { else {
m.setStatus(MessageStatus.READ); m.setStatus(MessageStatus.READ);
writeProxy.writeMessageStatusChangeEvent(new MessageStatusChangeEvent(m)); writeProxy.writeMessageStatusChange(new MessageStatusChange(m));
} }
} }
} }

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.util.function.Function; import java.util.function.Function;
import java.util.logging.Level; import java.util.logging.Level;
import envoy.client.ui.Startup;
import envoy.data.Config; import envoy.data.Config;
import envoy.data.ConfigItem; import envoy.data.ConfigItem;
import envoy.data.LoginCredentials; import envoy.data.LoginCredentials;
@ -109,5 +110,5 @@ public class ClientConfig extends Config {
* the registration option * the registration option
* @since Envoy Client v0.3-alpha * @since Envoy Client v0.3-alpha
*/ */
public LoginCredentials getLoginCredentials() { return new LoginCredentials(getUser(), getPassword(), false); } public LoginCredentials getLoginCredentials() { return new LoginCredentials(getUser(), getPassword(), false, Startup.VERSION); }
} }

View File

@ -3,9 +3,9 @@ package envoy.client.data;
import java.util.*; import java.util.*;
import envoy.data.*; import envoy.data.*;
import envoy.event.GroupResizeEvent; import envoy.event.GroupResize;
import envoy.event.MessageStatusChangeEvent; import envoy.event.MessageStatusChange;
import envoy.event.NameChangeEvent; import envoy.event.NameChange;
/** /**
* Stores information about the current {@link User} and their {@link Chat}s. * Stores information about the current {@link User} and their {@link Chat}s.
@ -25,7 +25,7 @@ public abstract class LocalDB {
protected List<Chat> chats = new ArrayList<>(); protected List<Chat> chats = new ArrayList<>();
protected IDGenerator idGenerator; protected IDGenerator idGenerator;
protected Cache<Message> messageCache = new Cache<>(); protected Cache<Message> messageCache = new Cache<>();
protected Cache<MessageStatusChangeEvent> statusCache = new Cache<>(); protected Cache<MessageStatusChange> statusCache = new Cache<>();
/** /**
* Initializes a storage space for a user-specific list of chats. * Initializes a storage space for a user-specific list of chats.
@ -136,13 +136,13 @@ public abstract class LocalDB {
* @return the offline status cache * @return the offline status cache
* @since Envoy Client v0.3-alpha * @since Envoy Client v0.3-alpha
*/ */
public Cache<MessageStatusChangeEvent> getStatusCache() { return statusCache; } public Cache<MessageStatusChange> getStatusCache() { return statusCache; }
/** /**
* @param statusCache the offline status cache to set * @param statusCache the offline status cache to set
* @since Envoy Client v0.3-alpha * @since Envoy Client v0.3-alpha
*/ */
public void setStatusCache(Cache<MessageStatusChangeEvent> statusCache) { this.statusCache = statusCache; } public void setStatusCache(Cache<MessageStatusChange> statusCache) { this.statusCache = statusCache; }
/** /**
* Searches for a message by ID. * Searches for a message by ID.
@ -169,20 +169,20 @@ public abstract class LocalDB {
/** /**
* Performs a contact name change if the corresponding contact is present. * Performs a contact name change if the corresponding contact is present.
* *
* @param event the {@link NameChangeEvent} to process * @param event the {@link NameChange} to process
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public void replaceContactName(NameChangeEvent event) { public void replaceContactName(NameChange event) {
chats.stream().map(Chat::getRecipient).filter(c -> c.getID() == event.getID()).findAny().ifPresent(c -> c.setName(event.get())); 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. * Performs a group resize operation if the corresponding group is present.
* *
* @param event the {@link GroupResizeEvent} to process * @param event the {@link GroupResize} to process
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public void updateGroup(GroupResizeEvent event) { public void updateGroup(GroupResize event) {
chats.stream() chats.stream()
.map(Chat::getRecipient) .map(Chat::getRecipient)
.filter(Group.class::isInstance) .filter(Group.class::isInstance)

View File

@ -15,7 +15,7 @@ import envoy.client.data.LocalDB;
import envoy.client.event.SendEvent; import envoy.client.event.SendEvent;
import envoy.data.*; import envoy.data.*;
import envoy.event.*; import envoy.event.*;
import envoy.event.contact.ContactOperationEvent; import envoy.event.contact.ContactOperation;
import envoy.event.contact.ContactSearchResult; import envoy.event.contact.ContactSearchResult;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
import envoy.util.SerializationUtils; import envoy.util.SerializationUtils;
@ -56,11 +56,16 @@ public class Client implements Closeable {
* will block for up to 5 seconds. If the handshake does exceed this time limit, * will block for up to 5 seconds. If the handshake does exceed this time limit,
* an exception is thrown. * an exception is thrown.
* *
* @param credentials the login credentials of the user * @param credentials the login credentials of the user
* @param receivedMessageCache a message cache containing all unread messages * @param receivedMessageCache a message cache containing all unread
* from the server that can be relayed after * messages
* initialization * from the server that can be relayed
* @param receivedMessageStatusChangeEventCache an event cache containing all received messageStatusChangeEvents from the server that can be relayed after initialization * after
* initialization
* @param receivedMessageStatusChangeCache an event cache containing all
* received messageStatusChangeEvents
* from the server that can be relayed
* after initialization
* @throws TimeoutException if the server could not be reached * @throws TimeoutException if the server could not be reached
* @throws IOException if the login credentials could not be * @throws IOException if the login credentials could not be
* written * written
@ -68,7 +73,7 @@ public class Client implements Closeable {
* waiting for the handshake response * waiting for the handshake response
*/ */
public void performHandshake(LoginCredentials credentials, Cache<Message> receivedMessageCache, public void performHandshake(LoginCredentials credentials, Cache<Message> receivedMessageCache,
Cache<MessageStatusChangeEvent> receivedMessageStatusChangeEventCache) Cache<MessageStatusChange> receivedMessageStatusChangeCache)
throws TimeoutException, IOException, InterruptedException { throws TimeoutException, IOException, InterruptedException {
if (online) throw new IllegalStateException("Handshake has already been performed successfully"); if (online) throw new IllegalStateException("Handshake has already been performed successfully");
// Establish TCP connection // Establish TCP connection
@ -82,8 +87,8 @@ public class Client implements Closeable {
// Register user creation processor, contact list processor and message cache // Register user creation processor, contact list processor and message cache
receiver.registerProcessor(User.class, sender -> { this.sender = sender; contacts = sender.getContacts(); }); receiver.registerProcessor(User.class, sender -> { this.sender = sender; contacts = sender.getContacts(); });
receiver.registerProcessor(Message.class, receivedMessageCache); receiver.registerProcessor(Message.class, receivedMessageCache);
receiver.registerProcessor(MessageStatusChangeEvent.class, receivedMessageStatusChangeEventCache); receiver.registerProcessor(MessageStatusChange.class, receivedMessageStatusChangeCache);
receiver.registerProcessor(HandshakeRejectionEvent.class, evt -> { rejected = true; eventBus.dispatch(evt); }); receiver.registerProcessor(HandshakeRejection.class, evt -> { rejected = true; eventBus.dispatch(evt); });
rejected = false; rejected = false;
@ -121,23 +126,29 @@ public class Client implements Closeable {
* Initializes the {@link Receiver} used to process data sent from the server to * Initializes the {@link Receiver} used to process data sent from the server to
* this client. * this client.
* *
* @param localDB the local database used to persist the current * @param localDB the local database used to persist
* {@link IDGenerator} * the current
* @param receivedMessageCache a message cache containing all unread messages * {@link IDGenerator}
* from the server that can be relayed after * @param receivedMessageCache a message cache containing all unread
* initialization * messages
* @param receivedMessageStatusChangeEventCache an event cache containing all received messageStatusChangeEvents from the server that can be relayed after initialization * from the server that can be relayed
* after
* initialization
* @param receivedMessageStatusChangeCache an event cache containing all
* received messageStatusChangeEvents
* from the server that can be relayed
* after initialization
* @throws IOException if no {@link IDGenerator} is present and none could be * @throws IOException if no {@link IDGenerator} is present and none could be
* requested from the server * requested from the server
* @since Envoy Client v0.2-alpha * @since Envoy Client v0.2-alpha
*/ */
public void initReceiver(LocalDB localDB, Cache<Message> receivedMessageCache, public void initReceiver(LocalDB localDB, Cache<Message> receivedMessageCache,
Cache<MessageStatusChangeEvent> receivedMessageStatusChangeEventCache) throws IOException { Cache<MessageStatusChange> receivedMessageStatusChangeCache) throws IOException {
checkOnline(); checkOnline();
// Process incoming messages // Process incoming messages
final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor(); final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor();
final MessageStatusChangeEventProcessor messageStatusChangeEventProcessor = new MessageStatusChangeEventProcessor(); final MessageStatusChangeProcessor messageStatusChangeEventProcessor = new MessageStatusChangeProcessor();
receiver.registerProcessor(Message.class, receivedMessageProcessor); receiver.registerProcessor(Message.class, receivedMessageProcessor);
@ -145,26 +156,26 @@ public class Client implements Closeable {
receivedMessageCache.setProcessor(receivedMessageProcessor); receivedMessageCache.setProcessor(receivedMessageProcessor);
// Process message status changes // Process message status changes
receiver.registerProcessor(MessageStatusChangeEvent.class, messageStatusChangeEventProcessor); receiver.registerProcessor(MessageStatusChange.class, messageStatusChangeEventProcessor);
receivedMessageStatusChangeEventCache.setProcessor(messageStatusChangeEventProcessor); receivedMessageStatusChangeCache.setProcessor(messageStatusChangeEventProcessor);
// Process user status changes // Process user status changes
receiver.registerProcessor(UserStatusChangeEvent.class, eventBus::dispatch); receiver.registerProcessor(UserStatusChange.class, eventBus::dispatch);
// Process message ID generation // Process message ID generation
receiver.registerProcessor(IDGenerator.class, localDB::setIDGenerator); receiver.registerProcessor(IDGenerator.class, localDB::setIDGenerator);
// Process name changes // Process name changes
receiver.registerProcessor(NameChangeEvent.class, evt -> { localDB.replaceContactName(evt); eventBus.dispatch(evt); }); receiver.registerProcessor(NameChange.class, evt -> { localDB.replaceContactName(evt); eventBus.dispatch(evt); });
// Process contact searches // Process contact searches
receiver.registerProcessor(ContactSearchResult.class, eventBus::dispatch); receiver.registerProcessor(ContactSearchResult.class, eventBus::dispatch);
// Process contact operations // Process contact operations
receiver.registerProcessor(ContactOperationEvent.class, eventBus::dispatch); receiver.registerProcessor(ContactOperation.class, eventBus::dispatch);
// Process group size changes // Process group size changes
receiver.registerProcessor(GroupResizeEvent.class, evt -> { localDB.updateGroup(evt); eventBus.dispatch(evt); }); receiver.registerProcessor(GroupResize.class, evt -> { localDB.updateGroup(evt); eventBus.dispatch(evt); });
// Send event // Send event
eventBus.register(SendEvent.class, evt -> { eventBus.register(SendEvent.class, evt -> {

View File

@ -5,30 +5,30 @@ import java.util.logging.Logger;
import envoy.data.Message.MessageStatus; import envoy.data.Message.MessageStatus;
import envoy.event.EventBus; import envoy.event.EventBus;
import envoy.event.MessageStatusChangeEvent; import envoy.event.MessageStatusChange;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
* File: <strong>MessageStatusChangeEventProcessor.java</strong><br> * File: <strong>MessageStatusChangeProcessor.java</strong><br>
* Created: <strong>4 Feb 2020</strong><br> * Created: <strong>4 Feb 2020</strong><br>
* *
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @since Envoy Client v0.3-alpha * @since Envoy Client v0.3-alpha
*/ */
public class MessageStatusChangeEventProcessor implements Consumer<MessageStatusChangeEvent> { public class MessageStatusChangeProcessor implements Consumer<MessageStatusChange> {
private static final Logger logger = EnvoyLog.getLogger(MessageStatusChangeEventProcessor.class); private static final Logger logger = EnvoyLog.getLogger(MessageStatusChangeProcessor.class);
/** /**
* Dispatches a {@link MessageStatusChangeEvent} if the status is * Dispatches a {@link MessageStatusChange} if the status is
* {@code RECEIVED} or {@code READ}. * {@code RECEIVED} or {@code READ}.
* *
* @param evt the status change event * @param evt the status change event
* @since Envoy Client v0.3-alpha * @since Envoy Client v0.3-alpha
*/ */
@Override @Override
public void accept(MessageStatusChangeEvent evt) { public void accept(MessageStatusChange evt) {
if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.warning("Received invalid message status change " + evt); if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.warning("Received invalid message status change " + evt);
else EventBus.getInstance().dispatch(evt); else EventBus.getInstance().dispatch(evt);
} }

View File

@ -6,12 +6,12 @@ import java.util.logging.Logger;
import envoy.client.data.LocalDB; import envoy.client.data.LocalDB;
import envoy.data.Message; import envoy.data.Message;
import envoy.event.MessageStatusChangeEvent; import envoy.event.MessageStatusChange;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
/** /**
* Implements methods to send {@link Message}s and * Implements methods to send {@link Message}s and
* {@link MessageStatusChangeEvent}s to the server or cache them inside a * {@link MessageStatusChange}s to the server or cache them inside a
* {@link LocalDB} depending on the online status.<br> * {@link LocalDB} depending on the online status.<br>
* <br> * <br>
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -65,7 +65,7 @@ public class WriteProxy {
} }
/** /**
* Sends cached {@link Message}s and {@link MessageStatusChangeEvent}s to the * Sends cached {@link Message}s and {@link MessageStatusChange}s to the
* server. * server.
* *
* @since Envoy Client v0.3-alpha * @since Envoy Client v0.3-alpha
@ -99,7 +99,7 @@ public class WriteProxy {
* @throws IOException if the event could not be sent * @throws IOException if the event could not be sent
* @since Envoy Client v0.3-alpha * @since Envoy Client v0.3-alpha
*/ */
public void writeMessageStatusChangeEvent(MessageStatusChangeEvent evt) throws IOException { public void writeMessageStatusChange(MessageStatusChange evt) throws IOException {
if (client.isOnline()) client.sendEvent(evt); if (client.isOnline()) client.sendEvent(evt);
else localDB.getStatusCache().accept(evt); else localDB.getStatusCache().accept(evt);
} }

View File

@ -1,7 +1,7 @@
package envoy.client.ui; package envoy.client.ui;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat; import java.time.format.DateTimeFormatter;
import java.util.Map; import java.util.Map;
import javafx.scene.control.Label; import javafx.scene.control.Label;
@ -26,7 +26,7 @@ import envoy.data.Message.MessageStatus;
*/ */
public class MessageListCell extends ListCell<Message> { public class MessageListCell extends ListCell<Message> {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm"); private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
private static Map<MessageStatus, Image> statusImages; private static Map<MessageStatus, Image> statusImages;
static { static {

View File

@ -16,7 +16,7 @@ import envoy.client.net.Client;
import envoy.client.ui.SceneContext.SceneInfo; import envoy.client.ui.SceneContext.SceneInfo;
import envoy.client.ui.controller.LoginScene; import envoy.client.ui.controller.LoginScene;
import envoy.data.Message; import envoy.data.Message;
import envoy.event.MessageStatusChangeEvent; import envoy.event.MessageStatusChange;
import envoy.exception.EnvoyException; import envoy.exception.EnvoyException;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
@ -33,10 +33,17 @@ import envoy.util.EnvoyLog;
*/ */
public final class Startup extends Application { public final class Startup extends Application {
private LocalDB localDB; /**
private Client client; * The version of this client. Used to verify compatibility with the server.
private Cache<Message> messageCache; *
private Cache<MessageStatusChangeEvent> messageStatusCache; * @since Envoy Client v0.1-beta
*/
public static final String VERSION = "0.1-beta";
private LocalDB localDB;
private Client client;
private Cache<Message> messageCache;
private Cache<MessageStatusChange> messageStatusCache;
private static final ClientConfig config = ClientConfig.getInstance(); private static final ClientConfig config = ClientConfig.getInstance();
private static final Logger logger = EnvoyLog.getLogger(Startup.class); private static final Logger logger = EnvoyLog.getLogger(Startup.class);
@ -89,8 +96,8 @@ public final class Startup extends Application {
} }
// Initialize client and unread message cache // Initialize client and unread message cache
client = new Client(); client = new Client();
messageCache = new Cache<>(); messageCache = new Cache<>();
messageStatusCache = new Cache<>(); messageStatusCache = new Cache<>();
stage.setTitle("Envoy"); stage.setTitle("Envoy");

View File

@ -25,9 +25,9 @@ import envoy.client.ui.MessageListCell;
import envoy.client.ui.SceneContext; import envoy.client.ui.SceneContext;
import envoy.data.*; import envoy.data.*;
import envoy.event.EventBus; import envoy.event.EventBus;
import envoy.event.MessageStatusChangeEvent; import envoy.event.MessageStatusChange;
import envoy.event.UserStatusChangeEvent; import envoy.event.UserStatusChange;
import envoy.event.contact.ContactOperationEvent; import envoy.event.contact.ContactOperation;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
/** /**
@ -103,8 +103,7 @@ public final class ChatScene {
}); });
// Listen to message status changes // Listen to message status changes
eventBus.register(MessageStatusChangeEvent.class, e -> eventBus.register(MessageStatusChange.class, e -> localDB.getMessage(e.getID()).ifPresent(message -> {
localDB.getMessage(e.getID()).ifPresent(message -> {
message.setStatus(e.get()); message.setStatus(e.get());
// Update UI if in current chat // Update UI if in current chat
@ -112,19 +111,15 @@ public final class ChatScene {
})); }));
// Listen to user status changes // Listen to user status changes
eventBus.register(UserStatusChangeEvent.class, e -> eventBus.register(UserStatusChange.class,
userList.getItems() e -> userList.getItems()
.stream() .stream()
.filter(c -> c.getID() == e.getID()) .filter(c -> c.getID() == e.getID())
.findAny() .findAny()
.ifPresent(u -> { .ifPresent(u -> { ((User) u).setStatus(e.get()); Platform.runLater(userList::refresh); }));
((User) u).setStatus(e.get());
Platform.runLater(userList::refresh);
})
);
// Listen to contacts changes // Listen to contacts changes
eventBus.register(ContactOperationEvent.class, e -> { eventBus.register(ContactOperation.class, e -> {
final var contact = e.get(); final var contact = e.get();
switch (e.getOperationType()) { switch (e.getOperationType()) {
case ADD: case ADD:
@ -174,8 +169,7 @@ public final class ChatScene {
// LEON: JFC <===> JAVA FRIED CHICKEN <=/=> Java Foundation Classes // LEON: JFC <===> JAVA FRIED CHICKEN <=/=> Java Foundation Classes
// Load the chat or create a new one and add it to the LocalDB // Load the chat or create a new one and add it to the LocalDB
currentChat = localDB currentChat = localDB.getChat(user.getID())
.getChat(user.getID())
.orElseGet(() -> { final var chat = new Chat(user); localDB.getChats().add(chat); return chat; }); .orElseGet(() -> { final var chat = new Chat(user); localDB.getChats().add(chat); return chat; });
messageList.setItems(FXCollections.observableList(currentChat.getMessages())); messageList.setItems(FXCollections.observableList(currentChat.getMessages()));

View File

@ -15,7 +15,7 @@ import envoy.client.ui.SceneContext;
import envoy.data.Contact; import envoy.data.Contact;
import envoy.event.ElementOperation; import envoy.event.ElementOperation;
import envoy.event.EventBus; import envoy.event.EventBus;
import envoy.event.contact.ContactOperationEvent; import envoy.event.contact.ContactOperation;
import envoy.event.contact.ContactSearchRequest; import envoy.event.contact.ContactSearchRequest;
import envoy.event.contact.ContactSearchResult; import envoy.event.contact.ContactSearchResult;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
@ -57,6 +57,7 @@ public class ContactSearchScene {
/** /**
* @param sceneContext enables the user to return to the chat scene * @param sceneContext enables the user to return to the chat scene
* @param localDB the local database to which new contacts are added
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public void initializeData(SceneContext sceneContext, LocalDB localDB) { public void initializeData(SceneContext sceneContext, LocalDB localDB) {
@ -108,7 +109,7 @@ public class ContactSearchScene {
} }
/** /**
* Sends an {@link ContactOperationEvent} for every selected contact to the * Sends an {@link ContactOperation} for every selected contact to the
* server. * server.
* *
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
@ -121,7 +122,7 @@ public class ContactSearchScene {
alert.setTitle("Add Contact to Contact List"); alert.setTitle("Add Contact to Contact List");
alert.setHeaderText("Add the user " + contact.getName() + " to your contact list?"); alert.setHeaderText("Add the user " + contact.getName() + " to your contact list?");
alert.showAndWait().filter(btn -> btn == ButtonType.OK).ifPresent(btn -> { alert.showAndWait().filter(btn -> btn == ButtonType.OK).ifPresent(btn -> {
final var event = new ContactOperationEvent(contact, ElementOperation.ADD); final var event = new ContactOperation(contact, ElementOperation.ADD);
// Sends the event to the server // Sends the event to the server
eventBus.dispatch(new SendEvent(event)); eventBus.dispatch(new SendEvent(event));
// Updates the UI // Updates the UI

View File

@ -13,7 +13,7 @@ import envoy.client.ui.SceneContext;
import envoy.data.Contact; import envoy.data.Contact;
import envoy.data.User; import envoy.data.User;
import envoy.event.EventBus; import envoy.event.EventBus;
import envoy.event.GroupCreationEvent; import envoy.event.GroupCreation;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -39,7 +39,7 @@ public class GroupCreationScene {
private SceneContext sceneContext; private SceneContext sceneContext;
private static EventBus eventBus = EventBus.getInstance(); private static EventBus eventBus = EventBus.getInstance();
@FXML @FXML
private void initialize() { private void initialize() {
@ -49,10 +49,12 @@ public class GroupCreationScene {
/** /**
* @param sceneContext enables the user to return to the chat scene * @param sceneContext enables the user to return to the chat scene
* @param localDB the local database from which potential group members can
* be selected
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public void initializeData(SceneContext sceneContext, LocalDB localDB) { public void initializeData(SceneContext sceneContext, LocalDB localDB) {
this.sceneContext = sceneContext; this.sceneContext = sceneContext;
Platform.runLater(() -> contactList.getItems() Platform.runLater(() -> contactList.getItems()
.addAll(localDB.getUsers() .addAll(localDB.getUsers()
.values() .values()
@ -62,13 +64,13 @@ public class GroupCreationScene {
} }
/** /**
* Sends a {@link GroupCreationEvent} to the server. * Sends a {@link GroupCreation} to the server.
* *
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
@FXML @FXML
private void sendGroupObject() { private void sendGroupObject() {
eventBus.dispatch(new SendEvent(new GroupCreationEvent(groupNameBar.getText(), eventBus.dispatch(new SendEvent(new GroupCreation(groupNameBar.getText(),
contactList.getSelectionModel().getSelectedItems().stream().map(Contact::getID).collect(Collectors.toSet())))); contactList.getSelectionModel().getSelectedItems().stream().map(Contact::getID).collect(Collectors.toSet()))));
} }

View File

@ -15,13 +15,14 @@ import envoy.client.data.ClientConfig;
import envoy.client.data.LocalDB; import envoy.client.data.LocalDB;
import envoy.client.net.Client; import envoy.client.net.Client;
import envoy.client.ui.SceneContext; import envoy.client.ui.SceneContext;
import envoy.client.ui.Startup;
import envoy.data.LoginCredentials; import envoy.data.LoginCredentials;
import envoy.data.Message; import envoy.data.Message;
import envoy.data.User; import envoy.data.User;
import envoy.data.User.UserStatus; import envoy.data.User.UserStatus;
import envoy.event.EventBus; import envoy.event.EventBus;
import envoy.event.HandshakeRejectionEvent; import envoy.event.HandshakeRejection;
import envoy.event.MessageStatusChangeEvent; import envoy.event.MessageStatusChange;
import envoy.exception.EnvoyException; import envoy.exception.EnvoyException;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
@ -54,11 +55,11 @@ public final class LoginScene {
@FXML @FXML
private Label connectionLabel; private Label connectionLabel;
private Client client; private Client client;
private LocalDB localDB; private LocalDB localDB;
private Cache<Message> receivedMessageCache; private Cache<Message> receivedMessageCache;
private Cache<MessageStatusChangeEvent> receivedMessageStatusChangeEventCache; private Cache<MessageStatusChange> receivedMessageStatusChangeCache;
private SceneContext sceneContext; private SceneContext sceneContext;
private static final Logger logger = EnvoyLog.getLogger(LoginScene.class); private static final Logger logger = EnvoyLog.getLogger(LoginScene.class);
private static final EventBus eventBus = EventBus.getInstance(); private static final EventBus eventBus = EventBus.getInstance();
@ -69,29 +70,36 @@ public final class LoginScene {
connectionLabel.setText("Server: " + config.getServer() + ":" + config.getPort()); connectionLabel.setText("Server: " + config.getServer() + ":" + config.getPort());
// Show an alert after an unsuccessful handshake // Show an alert after an unsuccessful handshake
eventBus.register(HandshakeRejectionEvent.class, eventBus.register(
HandshakeRejection.class,
e -> Platform.runLater(() -> { clearPasswordFields(); new Alert(AlertType.ERROR, e.get()).showAndWait(); })); e -> Platform.runLater(() -> { clearPasswordFields(); new Alert(AlertType.ERROR, e.get()).showAndWait(); }));
} }
/** /**
* Loads the login dialog using the FXML file {@code LoginDialog.fxml}. * Loads the login dialog using the FXML file {@code LoginDialog.fxml}.
* *
* @param client the client used to perform the handshake * @param client the client used to perform the
* @param localDB the local database used for offline login * handshake
* @param receivedMessageCache the cache storing messages received during * @param localDB the local database used for offline
* the handshake * login
* @param receivedMessageStatusChangeEventCache the cache storing messageStatusChangeEvents received during handshake * @param receivedMessageCache the cache storing messages received
* @param sceneContext the scene context used to initialize the chat * during
* scene * the handshake
* @param receivedMessageStatusChangeCache the cache storing
* messageStatusChangeEvents received
* during handshake
* @param sceneContext the scene context used to initialize
* the chat
* scene
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public void initializeData(Client client, LocalDB localDB, Cache<Message> receivedMessageCache, public void initializeData(Client client, LocalDB localDB, Cache<Message> receivedMessageCache,
Cache<MessageStatusChangeEvent> receivedMessageStatusChangeEventCache, SceneContext sceneContext) { Cache<MessageStatusChange> receivedMessageStatusChangeCache, SceneContext sceneContext) {
this.client = client; this.client = client;
this.localDB = localDB; this.localDB = localDB;
this.receivedMessageCache = receivedMessageCache; this.receivedMessageCache = receivedMessageCache;
this.receivedMessageStatusChangeEventCache = receivedMessageStatusChangeEventCache; this.receivedMessageStatusChangeCache = receivedMessageStatusChangeCache;
this.sceneContext = sceneContext; this.sceneContext = sceneContext;
// Prepare handshake // Prepare handshake
localDB.loadIDGenerator(); localDB.loadIDGenerator();
@ -110,12 +118,13 @@ public final class LoginScene {
if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) { if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) {
clearPasswordFields(); clearPasswordFields();
new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait(); new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait();
} else performHandshake(new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected())); } else performHandshake(
new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected(), Startup.VERSION));
} }
@FXML @FXML
private void offlineModeButtonPressed() { private void offlineModeButtonPressed() {
attemptOfflineMode(new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), false)); attemptOfflineMode(new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), false, Startup.VERSION));
} }
@FXML @FXML
@ -135,9 +144,9 @@ public final class LoginScene {
private void performHandshake(LoginCredentials credentials) { private void performHandshake(LoginCredentials credentials) {
try { try {
client.performHandshake(credentials, receivedMessageCache, receivedMessageStatusChangeEventCache); client.performHandshake(credentials, receivedMessageCache, receivedMessageStatusChangeCache);
if (client.isOnline()) { if (client.isOnline()) {
client.initReceiver(localDB, receivedMessageCache, receivedMessageStatusChangeEventCache); client.initReceiver(localDB, receivedMessageCache, receivedMessageStatusChangeCache);
loadChatScene(); loadChatScene();
} }
} catch (IOException | InterruptedException | TimeoutException e) { } catch (IOException | InterruptedException | TimeoutException e) {
@ -200,7 +209,7 @@ public final class LoginScene {
// Relay unread messages from cache // Relay unread messages from cache
if (receivedMessageCache != null && client.isOnline()) receivedMessageCache.relay(); if (receivedMessageCache != null && client.isOnline()) receivedMessageCache.relay();
if (receivedMessageStatusChangeEventCache != null && client.isOnline()) receivedMessageStatusChangeEventCache.relay(); if (receivedMessageStatusChangeCache != null && client.isOnline()) receivedMessageStatusChangeCache.relay();
} }
private void clearPasswordFields() { private void clearPasswordFields() {