2020-01-11 18:28:48 +01:00
|
|
|
package envoy.server.processors;
|
|
|
|
|
2020-08-22 18:14:26 +02:00
|
|
|
import static envoy.server.Startup.config;
|
|
|
|
|
2020-09-25 14:29:23 +02:00
|
|
|
import java.util.logging.*;
|
2020-01-11 18:28:48 +01:00
|
|
|
|
2020-04-09 22:31:20 +02:00
|
|
|
import javax.persistence.EntityExistsException;
|
|
|
|
|
2020-01-11 18:28:48 +01:00
|
|
|
import envoy.data.Message;
|
2020-09-25 14:29:23 +02:00
|
|
|
import envoy.event.*;
|
2020-10-19 18:17:51 +02:00
|
|
|
import envoy.util.EnvoyLog;
|
|
|
|
|
2020-02-15 22:43:20 +01:00
|
|
|
import envoy.server.data.PersistenceManager;
|
2020-09-25 14:29:23 +02:00
|
|
|
import envoy.server.net.*;
|
2020-01-11 18:28:48 +01:00
|
|
|
|
|
|
|
/**
|
2020-06-25 17:00:41 +02:00
|
|
|
* This {@link ObjectProcessor} handles incoming {@link Message}s.
|
2020-01-11 18:28:48 +01:00
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
2020-03-25 18:22:58 +01:00
|
|
|
* @author Maximilian Käfer
|
2020-01-11 18:28:48 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-08-22 13:37:07 +02:00
|
|
|
public final class MessageProcessor implements ObjectProcessor<Message> {
|
2020-01-11 18:28:48 +01:00
|
|
|
|
2020-06-25 17:00:41 +02:00
|
|
|
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
|
|
|
|
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
|
2020-10-19 18:17:51 +02:00
|
|
|
private static final Logger logger =
|
|
|
|
EnvoyLog.getLogger(MessageProcessor.class);
|
2020-06-11 12:06:06 +02:00
|
|
|
|
2020-01-11 18:28:48 +01:00
|
|
|
@Override
|
2020-03-26 20:14:09 +01:00
|
|
|
public void process(Message message, long socketID, ObjectWriteProxy writeProxy) {
|
2020-01-11 18:28:48 +01:00
|
|
|
message.nextStatus();
|
2020-03-25 18:22:58 +01:00
|
|
|
|
2020-06-25 17:00:41 +02:00
|
|
|
// Convert to server message
|
|
|
|
final var serverMessage = new envoy.server.data.Message(message);
|
2020-08-22 18:14:26 +02:00
|
|
|
// Telling the server to reload the message without the attachment
|
|
|
|
if (!config.isAttachmentSupportEnabled() && message.hasAttachment()) {
|
|
|
|
message = serverMessage.toCommon();
|
|
|
|
writeProxy.write(socketID, new NoAttachments());
|
|
|
|
}
|
2020-06-25 17:00:41 +02:00
|
|
|
|
2020-04-09 22:31:20 +02:00
|
|
|
try {
|
2020-03-25 18:22:58 +01:00
|
|
|
|
2020-06-25 17:00:41 +02:00
|
|
|
// Persist the message
|
|
|
|
persistenceManager.addMessage(serverMessage);
|
|
|
|
|
|
|
|
// Send the message to the recipient if online
|
|
|
|
if (connectionManager.isOnline(message.getRecipientID())) {
|
|
|
|
writeProxy.write(connectionManager.getSocketID(message.getRecipientID()), message);
|
|
|
|
|
|
|
|
// Increment status
|
|
|
|
message.nextStatus();
|
|
|
|
serverMessage.received();
|
|
|
|
persistenceManager.updateMessage(serverMessage);
|
|
|
|
|
|
|
|
// Notify the sender about the delivery
|
|
|
|
// Note that the exact time stamp might differ slightly
|
|
|
|
writeProxy.write(socketID, new MessageStatusChange(message));
|
|
|
|
}
|
2020-08-22 18:14:26 +02:00
|
|
|
} catch (final EntityExistsException e) {
|
2020-06-25 17:00:41 +02:00
|
|
|
logger.log(Level.WARNING, "Received " + message + " with an ID that already exists!");
|
2020-01-11 18:28:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|