MessageProcessor and GroupMessageProcessor
Moved groupMessage stuff form MessageProcessor to extra class and finished both processors
This commit is contained in:
parent
53bde70529
commit
f30732c71e
@ -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ä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; }
|
||||||
|
}
|
@ -6,10 +6,7 @@ import java.util.Date;
|
|||||||
import javax.persistence.EntityExistsException;
|
import javax.persistence.EntityExistsException;
|
||||||
|
|
||||||
import envoy.data.Message;
|
import envoy.data.Message;
|
||||||
import envoy.data.Message.MessageStatus;
|
|
||||||
import envoy.data.MessageBuilder;
|
|
||||||
import envoy.event.MessageStatusChangeEvent;
|
import envoy.event.MessageStatusChangeEvent;
|
||||||
import envoy.server.data.Contact;
|
|
||||||
import envoy.server.data.PersistenceManager;
|
import envoy.server.data.PersistenceManager;
|
||||||
import envoy.server.net.ConnectionManager;
|
import envoy.server.net.ConnectionManager;
|
||||||
import envoy.server.net.ObjectWriteProxy;
|
import envoy.server.net.ObjectWriteProxy;
|
||||||
@ -31,9 +28,7 @@ public class MessageProcessor implements ObjectProcessor<Message> {
|
|||||||
public void process(Message message, long socketID, ObjectWriteProxy writeProxy) {
|
public void process(Message message, long socketID, ObjectWriteProxy writeProxy) {
|
||||||
message.nextStatus();
|
message.nextStatus();
|
||||||
ConnectionManager connectionManager = ConnectionManager.getInstance();
|
ConnectionManager connectionManager = ConnectionManager.getInstance();
|
||||||
Contact recipient = PersistenceManager.getInstance().getContactByID(message.getRecipientID());
|
|
||||||
|
|
||||||
if (recipient instanceof envoy.server.data.User) {
|
|
||||||
sendToUser(connectionManager, message, writeProxy);
|
sendToUser(connectionManager, message, writeProxy);
|
||||||
// Sending a messageStatusChangeEvent to the sender
|
// Sending a messageStatusChangeEvent to the sender
|
||||||
try {
|
try {
|
||||||
@ -42,23 +37,6 @@ public class MessageProcessor implements ObjectProcessor<Message> {
|
|||||||
System.err.println("Could not send messageStatusChangeEvent to the sender of this message with ID: " + message.getID());
|
System.err.println("Could not send messageStatusChangeEvent to the sender of this message with ID: " + message.getID());
|
||||||
e.printStackTrace();
|
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)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message));
|
PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message));
|
||||||
} catch (EntityExistsException e) {
|
} catch (EntityExistsException e) {
|
||||||
|
Reference in New Issue
Block a user