Implemented groups support on server
Implemented support for group messages in MessageProcessor and adjusted IdGeneratorRequestProcessor
This commit is contained in:
parent
8c6a8e160b
commit
92ee3f0c13
@ -14,6 +14,7 @@ import envoy.server.net.ObjectWriteProxy;
|
|||||||
* Created: <strong>28 Jan 2020</strong><br>
|
* Created: <strong>28 Jan 2020</strong><br>
|
||||||
*
|
*
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
|
* @author Maximilian Käfer
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorRequest> {
|
public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorRequest> {
|
||||||
@ -26,13 +27,19 @@ public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorR
|
|||||||
@Override
|
@Override
|
||||||
public void process(IDGeneratorRequest input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
public void process(IDGeneratorRequest input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
||||||
System.out.println("Received id generation request.");
|
System.out.println("Received id generation request.");
|
||||||
|
var generator = createIDGenerator();
|
||||||
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);
|
|
||||||
|
|
||||||
System.out.println("Sending new id generator " + generator);
|
System.out.println("Sending new id generator " + generator);
|
||||||
writeProxy.write(socketId, 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 java.util.Date;
|
||||||
|
|
||||||
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;
|
||||||
@ -17,6 +20,7 @@ import envoy.server.net.ObjectWriteProxy;
|
|||||||
* Created: <strong>30.12.2019</strong><br>
|
* Created: <strong>30.12.2019</strong><br>
|
||||||
*
|
*
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
|
* @author Maximilian Käfer
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public class MessageProcessor implements ObjectProcessor<Message> {
|
public class MessageProcessor implements ObjectProcessor<Message> {
|
||||||
@ -24,21 +28,53 @@ public class MessageProcessor implements ObjectProcessor<Message> {
|
|||||||
@Override
|
@Override
|
||||||
public void process(Message message, long socketId, ObjectWriteProxy writeProxy) {
|
public void process(Message message, long socketId, ObjectWriteProxy writeProxy) {
|
||||||
|
|
||||||
ConnectionManager connectionManager = ConnectionManager.getInstance();
|
|
||||||
message.nextStatus();
|
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 (connectionManager.isOnline(message.getRecipientId())) try {
|
||||||
// If recipient is online, send the message directly
|
// If recipient is online, send the message directly
|
||||||
writeProxy.write(connectionManager.getSocketId(message.getRecipientId()), message);
|
writeProxy.write(connectionManager.getSocketId(message.getRecipientId()), message);
|
||||||
|
|
||||||
// Update the message status to RECEIVED
|
// Update the message status to RECEIVED
|
||||||
message.setReceivedDate(new Date());
|
message.setReceivedDate(new Date());
|
||||||
message.nextStatus();
|
message.nextStatus();
|
||||||
writeProxy.write(socketId, new MessageStatusChangeEvent(message));
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Recipient online. Failed to send message" + message.getId());
|
System.err.println("Recipient online. Failed to send message" + message.getId());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Reference in New Issue
Block a user