MessageProcessor and GroupMessageProcessor

Moved groupMessage stuff form MessageProcessor to extra class and
finished both processors
This commit is contained in:
DieGurke 2020-04-18 13:27:43 +02:00
parent a457261ee5
commit 7716af678a
2 changed files with 64 additions and 29 deletions

View File

@ -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: <strong>envoy-server-standalone</strong><br>
* File: <strong>GroupMessageProcessor.java</strong><br>
* Created: <strong>18.04.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-beta
*/
public class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
@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<GroupMessage> getInputClass() { return GroupMessage.class; }
}

View File

@ -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<Message> {
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));