73 lines
2.4 KiB
Java
73 lines
2.4 KiB
Java
package envoy.server.processors;
|
|
|
|
import java.io.IOException;
|
|
|
|
import envoy.data.Message;
|
|
import envoy.data.Message.MessageStatus;
|
|
import envoy.event.Event;
|
|
import envoy.event.MessageStatusChangeEvent;
|
|
import envoy.exception.EnvoyException;
|
|
import envoy.server.ConnectionManager;
|
|
import envoy.server.ObjectProcessor;
|
|
import envoy.server.database.PersistenceManager;
|
|
import envoy.server.net.ObjectWriteProxy;
|
|
|
|
/**
|
|
* Project: <strong>envoy-server-standalone</strong><br>
|
|
* File: <strong>EventProcessor.java</strong><br>
|
|
* Created: <strong>10 Jan 2020</strong><br>
|
|
*
|
|
* @author Leon Hofmeister
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
@SuppressWarnings("rawtypes")
|
|
public class EventProcessor implements ObjectProcessor<Event> {
|
|
|
|
/**
|
|
* Creates an instance of @link{EventProcessor}.
|
|
*
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public EventProcessor() {}
|
|
|
|
@Override
|
|
public Class<Event> getInputClass() { return Event.class; }
|
|
|
|
@Override
|
|
public void process(Event input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
|
if (input instanceof MessageStatusChangeEvent) try {
|
|
applyMessageStatusChange((MessageStatusChangeEvent) input, writeProxy);
|
|
} catch (EnvoyException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Redirects messageStatus changes to the database and to the recipient of the
|
|
* {@link Message}.
|
|
*
|
|
* @param event the {@link MessageStatusChangeEvent} to adjust
|
|
* @throws EnvoyException if the {@link Message} has an invalid state
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
private void applyMessageStatusChange(MessageStatusChangeEvent event, ObjectWriteProxy writeProxy) throws EnvoyException {
|
|
if (!(event.get() == MessageStatus.READ))// check that no invalid MessageStatuses are sent
|
|
throw new EnvoyException("Message" + event.getId() + "has an invalid status");
|
|
|
|
ConnectionManager conMan = ConnectionManager.getInstance();
|
|
PersistenceManager perMan = PersistenceManager.getPersistenceManager();
|
|
envoy.server.data.Message msg = perMan.getMessageById(event.getId());
|
|
|
|
msg.setStatus(event.get());
|
|
msg.setReadDate(event.getDate());
|
|
|
|
if (conMan.isOnline(msg.getRecipient().getId())) try {
|
|
writeProxy.write(conMan.getSocketId(msg.getRecipient().getId()), event);
|
|
} catch (IOException e) {
|
|
System.err.println("Recipient online. Failed to send MessageStatusChangedEvent at message" + event.getId());
|
|
e.printStackTrace();
|
|
}
|
|
perMan.updateMessage(msg);
|
|
}
|
|
}
|