From 22fd99bd704da33c6916a0c41301eb02557d70fb Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 25 Jun 2020 17:00:41 +0200 Subject: [PATCH 1/3] Fix notifying the sender about a message delivery This addresses bugs in two instances of delivery notification: * the sender is online -> no event was sent * the sender comes online later -> wrong status (SENT) was sent --- .../processors/LoginCredentialProcessor.java | 11 ++++- .../server/processors/MessageProcessor.java | 47 +++++++++++-------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 9877f36..ad7addd 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -109,14 +109,21 @@ public final class LoginCredentialProcessor implements ObjectProcessor - *
+ * This {@link ObjectProcessor} handles incoming {@link Message}s. + *

* Project: envoy-server-standalone
* File: MessageProcessor.java
* Created: 30.12.2019
@@ -26,6 +27,8 @@ import envoy.util.EnvoyLog; */ public class MessageProcessor implements ObjectProcessor { + private static final PersistenceManager persistenceManager = PersistenceManager.getInstance(); + private static final ConnectionManager connectionManager = ConnectionManager.getInstance(); private static final Logger logger = EnvoyLog.getLogger(MessageProcessor.class); @Override @@ -35,26 +38,32 @@ public class MessageProcessor implements ObjectProcessor { return; } message.nextStatus(); - ConnectionManager connectionManager = ConnectionManager.getInstance(); - sendToUser(connectionManager, message, writeProxy); + // Convert to server message + final var serverMessage = new envoy.server.data.Message(message); + try { - PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message)); - } catch (EntityExistsException e) { - logger.warning("Received a message with an id that already exists"); - } - } - private void sendToUser(ConnectionManager connectionManager, Message message, ObjectWriteProxy writeProxy) { - if (connectionManager.isOnline(message.getRecipientID())) try { - // If recipient is online, send the message directly - writeProxy.write(connectionManager.getSocketID(message.getRecipientID()), message); - // Update the message status to RECEIVED - message.setReceivedDate(LocalDateTime.now()); - message.nextStatus(); + // Persist the message + persistenceManager.addMessage(serverMessage); + + // Send the message to the recipient if online + if (connectionManager.isOnline(message.getRecipientID())) { + writeProxy.write(connectionManager.getSocketID(message.getRecipientID()), message); + + // Increment status + message.nextStatus(); + serverMessage.received(); + persistenceManager.updateMessage(serverMessage); + + // Notify the sender about the delivery + // Note that the exact time stamp might differ slightly + writeProxy.write(socketID, new MessageStatusChange(message)); + } + } catch (EntityExistsException e) { + logger.log(Level.WARNING, "Received " + message + " with an ID that already exists!"); } catch (IOException e) { - logger.warning("Recipient online. Failed to send message" + message.getID()); - e.printStackTrace(); + logger.log(Level.WARNING, "Failed to deliver " + message + ":", e); } } From c1e40c7c24cba19d972a3d1ead3ae5c2baea9bdb Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 26 Jun 2020 20:48:18 +0200 Subject: [PATCH 2/3] Fixed monstrosity not showing Envoy logo --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 79e92f3..a514d85 100755 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # envoy-server-standalone -Standalone server for the Envoy project +Standalone server for the +Envoy project From cd48ce67553e65c9b6bcdbdad6cb4c96d48543d7 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Fri, 26 Jun 2020 22:04:40 +0200 Subject: [PATCH 3/3] Validate user name during registration --- .../processors/LoginCredentialProcessor.java | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index ad7addd..0a248c9 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -20,6 +20,7 @@ import envoy.server.data.PersistenceManager; import envoy.server.data.User; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; +import envoy.util.Bounds; import envoy.util.EnvoyLog; /** @@ -38,7 +39,7 @@ public final class LoginCredentialProcessor implements ObjectProcessor()); - persistenceManager.addContact(user); - logger.info("Registered new " + user); } + try { + // Checking that no user already has this identifier + PersistenceManager.getInstance().getUserByName(credentials.getIdentifier()); + + // This code only gets executed if this user already exists + logger.info("The requested user already exists."); + writeProxy.write(socketID, new HandshakeRejection(USERNAME_TAKEN)); + return; + } catch (NoResultException e) { + // Creation of a new user + user = new User(); + user.setName(credentials.getIdentifier()); + user.setLastSeen(LocalDateTime.now()); + user.setStatus(ONLINE); + user.setPasswordHash(credentials.getPasswordHash()); + user.setContacts(new HashSet<>()); + persistenceManager.addContact(user); + logger.info("Registered new " + user); + } } logger.info(user + " successfully authenticated.");