Merge pull request #16 from informatik-ag-ngl/f/refactored_EventProcessor
refactored EventProcessor into MessageStatusChangeProcessor and fixed some minor documentation errors
This commit is contained in:
		@@ -35,7 +35,7 @@ public class Startup {
 | 
				
			|||||||
		Set<ObjectProcessor<?>> processors = new HashSet<>();
 | 
							Set<ObjectProcessor<?>> processors = new HashSet<>();
 | 
				
			||||||
		processors.add(new LoginCredentialProcessor());
 | 
							processors.add(new LoginCredentialProcessor());
 | 
				
			||||||
		processors.add(new MessageProcessor());
 | 
							processors.add(new MessageProcessor());
 | 
				
			||||||
		processors.add(new EventProcessor());
 | 
							processors.add(new MessageStatusChangeProcessor());
 | 
				
			||||||
		processors.add(new IdGeneratorRequestProcessor());
 | 
							processors.add(new IdGeneratorRequestProcessor());
 | 
				
			||||||
		Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
 | 
							Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,8 +30,8 @@ public class ConfigItem {
 | 
				
			|||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Creates an instance of @link{ConfigItem}.
 | 
						 * Creates an instance of @link{ConfigItem}.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param key
 | 
						 * @param key   the name of this {@link ConfigItem}
 | 
				
			||||||
	 * @param value
 | 
						 * @param value the value of this {@link ConfigItem}
 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public ConfigItem(String key, String value) {
 | 
						public ConfigItem(String key, String value) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -137,6 +137,11 @@ public class PersistenceManager {
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	public Message getMessageById(long id) { return entityManager.find(Message.class, id); }
 | 
						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); }
 | 
						public ConfigItem getConfigItemById(String key) { return entityManager.find(ConfigItem.class, key); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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: <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
 | 
					 | 
				
			||||||
	 * @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);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -0,0 +1,45 @@
 | 
				
			|||||||
 | 
					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: <strong>envoy-server-standalone</strong><br>
 | 
				
			||||||
 | 
					 * File: <strong>MessageStatusChangeProcessor.java</strong><br>
 | 
				
			||||||
 | 
					 * Created: <strong>10 Jan 2020</strong><br>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Leon Hofmeister
 | 
				
			||||||
 | 
					 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class MessageStatusChangeProcessor implements ObjectProcessor<MessageStatusChangeEvent> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Override
 | 
				
			||||||
 | 
						public Class<MessageStatusChangeEvent> getInputClass() { return MessageStatusChangeEvent.class; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Override
 | 
				
			||||||
 | 
						public void process(MessageStatusChangeEvent input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
 | 
				
			||||||
 | 
							try {
 | 
				
			||||||
 | 
								// any other status than read is not supposed to be sent to the server
 | 
				
			||||||
 | 
								if (input.get() != MessageStatus.READ) throw new EnvoyException("Message" + input.getId() + "has an invalid status");
 | 
				
			||||||
 | 
							} catch (EnvoyException e) {
 | 
				
			||||||
 | 
								e.printStackTrace();
 | 
				
			||||||
 | 
								return;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							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());
 | 
				
			||||||
 | 
							perMan.updateMessage(msg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (conMan.isOnline(msg.getRecipient().getId())) writeProxy.write(conMan.getSocketId(msg.getRecipient().getId()), input);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user