From 7716af678a09e7dd683837d266bb3e08b8cf1067 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 18 Apr 2020 13:27:43 +0200 Subject: [PATCH] MessageProcessor and GroupMessageProcessor Moved groupMessage stuff form MessageProcessor to extra class and finished both processors --- .../processors/GroupMessageProcessor.java | 57 +++++++++++++++++++ .../server/processors/MessageProcessor.java | 36 +++--------- 2 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 src/main/java/envoy/server/processors/GroupMessageProcessor.java diff --git a/src/main/java/envoy/server/processors/GroupMessageProcessor.java b/src/main/java/envoy/server/processors/GroupMessageProcessor.java new file mode 100644 index 0000000..2b50e24 --- /dev/null +++ b/src/main/java/envoy/server/processors/GroupMessageProcessor.java @@ -0,0 +1,57 @@ +package envoy.server.processors; + +import java.io.IOException; + +import envoy.data.GroupMessage; +import envoy.data.Message.MessageStatus; +import envoy.event.MessageStatusChangeEvent; +import envoy.server.data.PersistenceManager; +import envoy.server.net.ConnectionManager; +import envoy.server.net.ObjectWriteProxy; + +/** + * Project: envoy-server-standalone
+ * File: GroupMessageProcessor.java
+ * Created: 18.04.2020
+ * + * @author Maximilian Käfer + * @since Envoy Server Standalone v0.1-beta + */ +public class GroupMessageProcessor implements ObjectProcessor { + + @Override + public void process(GroupMessage groupMessage, long socketID, ObjectWriteProxy writeProxy) { + groupMessage.nextStatus(); + ConnectionManager connectionManager = ConnectionManager.getInstance(); + try { + writeProxy.write(socketID, new MessageStatusChangeEvent(groupMessage)); + } catch (IOException e) { + System.err.println("Could not send messageStatusChangeEvent to the sender of this groupMessage with ID: " + groupMessage.getID()); + e.printStackTrace(); + } + + final var members = PersistenceManager.getInstance().getGroupByID(groupMessage.getRecipientID()).getContacts(); + members.forEach(user -> { sendToMember(connectionManager, groupMessage, user.getID(), writeProxy); }); + // TODO if all member message statuses are RECEIVED, send + // messageStatusChangeEvent to all members (we need to know if the sender is in + // the member list as well. If so insert continue on the loop one line above. If + // not we need to send the messageStatusChangeEvent separately to the sender). + + // TODO Implement separate DB entity for groupMesages so we can persist here. + } + + private void sendToMember(ConnectionManager connectionManager, GroupMessage groupMessage, long memberID, ObjectWriteProxy writeProxy) { + if (connectionManager.isOnline(memberID)) try { + // Update the message status of the member to RECEIVED + groupMessage.getMemberStatuses().replace(memberID, MessageStatus.RECEIVED); + // If recipient is online, send the groupMessage directly + writeProxy.write(connectionManager.getSocketId(memberID), groupMessage); + } catch (IOException e) { + System.err.println("Recipient online. Failed to send message" + groupMessage.getID()); + e.printStackTrace(); + } + } + + @Override + public Class getInputClass() { return GroupMessage.class; } +} diff --git a/src/main/java/envoy/server/processors/MessageProcessor.java b/src/main/java/envoy/server/processors/MessageProcessor.java index dff28ec..ce86408 100755 --- a/src/main/java/envoy/server/processors/MessageProcessor.java +++ b/src/main/java/envoy/server/processors/MessageProcessor.java @@ -6,10 +6,7 @@ import java.util.Date; import javax.persistence.EntityExistsException; import envoy.data.Message; -import envoy.data.Message.MessageStatus; -import envoy.data.MessageBuilder; import envoy.event.MessageStatusChangeEvent; -import envoy.server.data.Contact; import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; @@ -31,33 +28,14 @@ public class MessageProcessor implements ObjectProcessor { public void process(Message message, long socketID, ObjectWriteProxy writeProxy) { message.nextStatus(); ConnectionManager connectionManager = ConnectionManager.getInstance(); - Contact recipient = PersistenceManager.getInstance().getContactByID(message.getRecipientID()); - if (recipient instanceof envoy.server.data.User) { - 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 { - final var members = PersistenceManager.getInstance().getGroupByID(message.getRecipientID()).getContacts(); - 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) - }); + 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(); } try { PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message));