From 92ee3f0c13b0eb2aec6d8a01a44be9a2fbc40201 Mon Sep 17 00:00:00 2001
From: DieGurke <55625494+DieGurke@users.noreply.github.com>
Date: Wed, 25 Mar 2020 18:22:58 +0100
Subject: [PATCH] Implemented groups support on server
Implemented support for group messages in MessageProcessor and adjusted
IdGeneratorRequestProcessor
---
.../IDGeneratorRequestProcessor.java | 17 +++++--
.../server/processors/MessageProcessor.java | 46 +++++++++++++++++--
2 files changed, 53 insertions(+), 10 deletions(-)
diff --git a/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java b/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java
index 7800c9c..bb6be36 100755
--- a/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java
+++ b/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java
@@ -14,6 +14,7 @@ import envoy.server.net.ObjectWriteProxy;
* Created: 28 Jan 2020
*
* @author Kai S. K. Engelbart
+ * @author Maximilian Käfer
* @since Envoy Server Standalone v0.1-alpha
*/
public class IDGeneratorRequestProcessor implements ObjectProcessor {
@@ -26,13 +27,19 @@ public class IDGeneratorRequestProcessor implements ObjectProcessor30.12.2019
*
* @author Kai S. K. Engelbart
+ * @author Maximilian Käfer
* @since Envoy Server Standalone v0.1-alpha
*/
public class MessageProcessor implements ObjectProcessor {
@@ -24,23 +28,55 @@ public class MessageProcessor implements ObjectProcessor {
@Override
public void process(Message message, long socketId, ObjectWriteProxy writeProxy) {
- ConnectionManager connectionManager = ConnectionManager.getInstance();
message.nextStatus();
+ ConnectionManager connectionManager = ConnectionManager.getInstance();
+ Contact recipient = PersistenceManager.getInstance().getContactById(message.getId());
+
+ if (recipient instanceof envoy.server.data.User) {
+ System.out.println("The received message is a direct message.");
+ sendToUser(connectionManager, message, writeProxy);
+ // Sending a messageStatusChangeEvent to the sender
+ try {
+ writeProxy.write(socketId, new MessageStatusChangeEvent(message));
+ } catch (IOException e) {
+ System.err.println("Could not send messageStatusChangeEvent to the sender of this message with ID: " + message.getId());
+ e.printStackTrace();
+ }
+ } else {
+ System.out.println("The received message is a group message.");
+ final var members = PersistenceManager.getInstance().getGroupById(message.getRecipientId()).getMembers();
+ final var generator = IDGeneratorRequestProcessor.createIDGenerator(members.size());
+ members.forEach(user -> {
+ envoy.data.Message returnMessage = new MessageBuilder(message.getRecipientId(), user.getID(), generator)
+ .setDate(message.getCreationDate())
+ .setText(message.getText())
+ .setAttachment(message.getAttachment())
+ .setStatus(MessageStatus.SENT)
+ .setForwarded(message.isForwarded())
+ .build();
+ sendToUser(connectionManager, returnMessage, writeProxy);
+
+ // TODO Handle messageStatusChangeEvent because we have many recipients and
+ // copies of the original message. (Not at this location)
+ });
+ }
+ PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message));
+ }
+
+ 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(new Date());
message.nextStatus();
- writeProxy.write(socketId, new MessageStatusChangeEvent(message));
+
} catch (IOException e) {
System.err.println("Recipient online. Failed to send message" + message.getId());
e.printStackTrace();
}
- PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message));
}
-
+
@Override
public Class getInputClass() { return Message.class; }
}