Implemented groups support on server
Implemented support for group messages in MessageProcessor and adjusted IdGeneratorRequestProcessor
This commit is contained in:
parent
1bedd5fb7f
commit
24b7e15ff2
@ -14,6 +14,7 @@ import envoy.server.net.ObjectWriteProxy;
|
||||
* Created: <strong>28 Jan 2020</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @author Maximilian Käfer
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorRequest> {
|
||||
@ -26,13 +27,19 @@ public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorR
|
||||
@Override
|
||||
public void process(IDGeneratorRequest input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
||||
System.out.println("Received id generation request.");
|
||||
|
||||
ConfigItem currentId = PersistenceManager.getInstance().getConfigItemById("currentMessageId");
|
||||
IDGenerator generator = new IDGenerator(Integer.parseInt(currentId.getValue()), ID_RANGE);
|
||||
currentId.setValue(String.valueOf(Integer.parseInt(currentId.getValue()) + ID_RANGE));
|
||||
PersistenceManager.getInstance().updateConfigItem(currentId);
|
||||
var generator = createIDGenerator();
|
||||
|
||||
System.out.println("Sending new id generator " + generator);
|
||||
writeProxy.write(socketId, generator);
|
||||
}
|
||||
|
||||
public static IDGenerator createIDGenerator() { return createIDGenerator(ID_RANGE); }
|
||||
|
||||
public static IDGenerator createIDGenerator(long range) {
|
||||
ConfigItem currentId = PersistenceManager.getInstance().getConfigItemById("currentMessageId");
|
||||
IDGenerator generator = new IDGenerator(Integer.parseInt(currentId.getValue()), range);
|
||||
currentId.setValue(String.valueOf(Integer.parseInt(currentId.getValue()) + range));
|
||||
PersistenceManager.getInstance().updateConfigItem(currentId);
|
||||
return generator;
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,10 @@ import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
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;
|
||||
@ -17,6 +20,7 @@ import envoy.server.net.ObjectWriteProxy;
|
||||
* Created: <strong>30.12.2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @author Maximilian Käfer
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public class MessageProcessor implements ObjectProcessor<Message> {
|
||||
@ -24,21 +28,53 @@ public class MessageProcessor implements ObjectProcessor<Message> {
|
||||
@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
|
||||
|
Reference in New Issue
Block a user