From 334b1a2bc23bd7159f564f27d28671e6dcfa0d7c Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 30 Jan 2020 12:52:27 +0100 Subject: [PATCH] Refactored EventProcessor into MessageStatusChangeProcessor additionally fixed some minor Javadoc errors --- src/main/java/envoy/server/Startup.java | 2 +- .../java/envoy/server/data/ConfigItem.java | 4 +- .../server/database/PersistenceManager.java | 5 ++ .../server/processors/EventProcessor.java | 73 ------------------- .../MessageStatusChangeProcessor.java | 56 ++++++++++++++ 5 files changed, 64 insertions(+), 76 deletions(-) delete mode 100644 src/main/java/envoy/server/processors/EventProcessor.java create mode 100644 src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index 98e8b3d..097399e 100644 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -35,7 +35,7 @@ public class Startup { Set> processors = new HashSet<>(); processors.add(new LoginCredentialProcessor()); processors.add(new MessageProcessor()); - processors.add(new EventProcessor()); + processors.add(new MessageStatusChangeProcessor()); processors.add(new IdGeneratorRequestProcessor()); Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors)); diff --git a/src/main/java/envoy/server/data/ConfigItem.java b/src/main/java/envoy/server/data/ConfigItem.java index 2fdb8ba..5f5d080 100644 --- a/src/main/java/envoy/server/data/ConfigItem.java +++ b/src/main/java/envoy/server/data/ConfigItem.java @@ -30,8 +30,8 @@ public class ConfigItem { /** * Creates an instance of @link{ConfigItem}. * - * @param key - * @param value + * @param key the name of this {@link ConfigItem} + * @param value the value of this {@link ConfigItem} * @since Envoy Server Standalone v0.1-alpha */ public ConfigItem(String key, String value) { diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index 97d2a3a..3ec686a 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -137,6 +137,11 @@ public class PersistenceManager { */ public Message getMessageById(long id) { return entityManager.find(Message.class, id); } + /** + * @param key the name of this {@link ConfigItem} + * @return the {@link ConfigItem} with the given name + * @since Envoy Server Standalone v0.1-alpha + */ public ConfigItem getConfigItemById(String key) { return entityManager.find(ConfigItem.class, key); } /** diff --git a/src/main/java/envoy/server/processors/EventProcessor.java b/src/main/java/envoy/server/processors/EventProcessor.java deleted file mode 100644 index 024d22b..0000000 --- a/src/main/java/envoy/server/processors/EventProcessor.java +++ /dev/null @@ -1,73 +0,0 @@ -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: envoy-server-standalone
- * File: EventProcessor.java
- * Created: 10 Jan 2020
- * - * @author Leon Hofmeister - * @since Envoy Server Standalone v0.1-alpha - */ -@SuppressWarnings("rawtypes") -public class EventProcessor implements ObjectProcessor { - - /** - * Creates an instance of @link{EventProcessor}. - * - * @since Envoy Server Standalone v0.1-alpha - */ - public EventProcessor() {} - - @Override - public Class 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 - * @param writeProxy allows sending objects to clients - * @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); - } -} diff --git a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java new file mode 100644 index 0000000..7a659e0 --- /dev/null +++ b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java @@ -0,0 +1,56 @@ +package envoy.server.processors; + +import java.io.IOException; + +import envoy.data.Message.MessageStatus; +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: envoy-server-standalone
+ * File: MessageStatusChangeProcessor.java
+ * Created: 10 Jan 2020
+ * + * @author Leon Hofmeister + * @since Envoy Server Standalone v0.1-alpha + */ +public class MessageStatusChangeProcessor implements ObjectProcessor { + + /** + * Creates an instance of @link{MessageStatusChangeProcessor}. + * + * @since Envoy Server Standalone v0.1-alpha + */ + public MessageStatusChangeProcessor() {} + + @Override + public Class getInputClass() { return MessageStatusChangeEvent.class; } + + @Override + public void process(MessageStatusChangeEvent input, long socketId, ObjectWriteProxy writeProxy) throws IOException { + try { + if (!(input.get() == MessageStatus.READ))// check that no invalid MessageStatuses are sent + throw new EnvoyException("Message" + input.getId() + "has an invalid status"); + + ConnectionManager conMan = ConnectionManager.getInstance(); + PersistenceManager perMan = PersistenceManager.getPersistenceManager(); + envoy.server.data.Message msg = perMan.getMessageById(input.getId()); + + msg.setStatus(input.get()); + msg.setReadDate(input.getDate()); + + if (conMan.isOnline(msg.getRecipient().getId())) writeProxy.write(conMan.getSocketId(msg.getRecipient().getId()), input); + + perMan.updateMessage(msg); + + } catch (IOException | EnvoyException e) { + if (e.getClass() == IOException.class) + System.err.println("Recipient online. Failed to send MessageStatusChangedMessageStatusChangeEvent at message" + input.getId()); + e.printStackTrace(); + } + } +}