Replaced MessageCache by Cache<T>
This class will be used to implement the offline cache for both messages and events.
This commit is contained in:
		
							
								
								
									
										58
									
								
								src/main/java/envoy/client/data/Cache.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								src/main/java/envoy/client/data/Cache.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,58 @@ | |||||||
|  | package envoy.client.data; | ||||||
|  |  | ||||||
|  | import java.util.LinkedList; | ||||||
|  | import java.util.Queue; | ||||||
|  | import java.util.function.Consumer; | ||||||
|  | import java.util.logging.Logger; | ||||||
|  |  | ||||||
|  | import envoy.client.util.EnvoyLog; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Stores elements in a queue to process them later.<br> | ||||||
|  |  * <br> | ||||||
|  |  * Project: <strong>envoy-client</strong><br> | ||||||
|  |  * File: <strong>Cache.java</strong><br> | ||||||
|  |  * Created: <strong>6 Feb 2020</strong><br> | ||||||
|  |  * | ||||||
|  |  * @param <T> the type of cached elements | ||||||
|  |  * @author Kai S. K. Engelbart | ||||||
|  |  * @since Envoy v0.3-alpha | ||||||
|  |  */ | ||||||
|  | public class Cache<T> implements Consumer<T> { | ||||||
|  |  | ||||||
|  | 	private final Queue<T>	elements	= new LinkedList<>(); | ||||||
|  | 	private Consumer<T>		processor; | ||||||
|  |  | ||||||
|  | 	private static final Logger logger = EnvoyLog.getLogger(Cache.class.getSimpleName()); | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Adds an element to the cache. | ||||||
|  | 	 * | ||||||
|  | 	 * @param element the element to add | ||||||
|  | 	 * @since Envoy v0.3-alpha | ||||||
|  | 	 */ | ||||||
|  | 	@Override | ||||||
|  | 	public void accept(T element) { | ||||||
|  | 		logger.info(String.format("Adding element %s to cache", element)); | ||||||
|  | 		elements.offer(element); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Sets the processor to which cached elements are relayed. | ||||||
|  | 	 * | ||||||
|  | 	 * @param processor the processor to set | ||||||
|  | 	 * @since Envoy v0.3-alpha | ||||||
|  | 	 */ | ||||||
|  | 	public void setProcessor(Consumer<T> processor) { this.processor = processor; } | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Relays all cached elements to the processor. | ||||||
|  | 	 * | ||||||
|  | 	 * @throws IllegalStateException if the processor is not initialized | ||||||
|  | 	 * @since Envoy v0.3-alpha | ||||||
|  | 	 */ | ||||||
|  | 	public void relay() { | ||||||
|  | 		if (processor == null) throw new IllegalStateException("Processor is not defined"); | ||||||
|  | 		elements.forEach(processor::accept); | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @@ -10,6 +10,7 @@ import java.util.logging.Logger; | |||||||
| import javax.naming.TimeLimitExceededException; | import javax.naming.TimeLimitExceededException; | ||||||
|  |  | ||||||
| import envoy.client.Config; | import envoy.client.Config; | ||||||
|  | import envoy.client.data.Cache; | ||||||
| import envoy.client.data.LocalDb; | import envoy.client.data.LocalDb; | ||||||
| import envoy.client.util.EnvoyLog; | import envoy.client.util.EnvoyLog; | ||||||
| import envoy.data.*; | import envoy.data.*; | ||||||
| @@ -61,7 +62,7 @@ public class Client implements Closeable { | |||||||
| 	 *                   failed for some other reason | 	 *                   failed for some other reason | ||||||
| 	 * @since Envoy v0.2-alpha | 	 * @since Envoy v0.2-alpha | ||||||
| 	 */ | 	 */ | ||||||
| 	public MessageCache onlineInit(LoginCredentials credentials, LocalDb localDb) throws Exception { | 	public Cache<Message> onlineInit(LoginCredentials credentials, LocalDb localDb) throws Exception { | ||||||
| 		// Establish TCP connection | 		// Establish TCP connection | ||||||
| 		logger.info(String.format("Attempting connection to server %s:%d...", config.getServer(), config.getPort())); | 		logger.info(String.format("Attempting connection to server %s:%d...", config.getServer(), config.getPort())); | ||||||
| 		socket = new Socket(config.getServer(), config.getPort()); | 		socket = new Socket(config.getServer(), config.getPort()); | ||||||
| @@ -71,7 +72,7 @@ public class Client implements Closeable { | |||||||
| 		receiver = new Receiver(socket.getInputStream()); | 		receiver = new Receiver(socket.getInputStream()); | ||||||
|  |  | ||||||
| 		// Create cache for unread messages | 		// Create cache for unread messages | ||||||
| 		final MessageCache cache = new MessageCache(); | 		final Cache<Message> cache = new Cache<>(); | ||||||
|  |  | ||||||
| 		// Register user creation processor, contact list processor and message cache | 		// Register user creation processor, contact list processor and message cache | ||||||
| 		receiver.registerProcessor(User.class, sender -> { logger.info("Acquired user object " + sender); this.sender = sender; }); | 		receiver.registerProcessor(User.class, sender -> { logger.info("Acquired user object " + sender); this.sender = sender; }); | ||||||
|   | |||||||
| @@ -1,54 +0,0 @@ | |||||||
| package envoy.client.net; |  | ||||||
|  |  | ||||||
| import java.util.LinkedList; |  | ||||||
| import java.util.Queue; |  | ||||||
| import java.util.function.Consumer; |  | ||||||
| import java.util.logging.Logger; |  | ||||||
|  |  | ||||||
| import envoy.client.util.EnvoyLog; |  | ||||||
| import envoy.data.Message; |  | ||||||
|  |  | ||||||
| /** |  | ||||||
|  * Stores messages in a queue until the application initialization is complete. |  | ||||||
|  * The messages can then be relayed to a processor.<br> |  | ||||||
|  * <br> |  | ||||||
|  * Project: <strong>envoy-client</strong><br> |  | ||||||
|  * File: <strong>MessageCache.java</strong><br> |  | ||||||
|  * Created: <strong>4 Feb 2020</strong><br> |  | ||||||
|  * |  | ||||||
|  * @author Kai S. K. Engelbart |  | ||||||
|  * @since Envoy v0.3-alpha |  | ||||||
|  */ |  | ||||||
| public class MessageCache implements Consumer<Message> { |  | ||||||
|  |  | ||||||
| 	private final Queue<Message>	messages	= new LinkedList<>(); |  | ||||||
| 	private Consumer<Message>		processor; |  | ||||||
|  |  | ||||||
| 	private static final Logger logger = EnvoyLog.getLogger(MessageCache.class.getSimpleName()); |  | ||||||
|  |  | ||||||
| 	/** |  | ||||||
| 	 * Adds a message to the cache. |  | ||||||
| 	 * |  | ||||||
| 	 * @since Envoy v0.3-alpha |  | ||||||
| 	 */ |  | ||||||
| 	@Override |  | ||||||
| 	public void accept(Message message) { |  | ||||||
| 		logger.info(String.format("Adding message %s to cache", message)); |  | ||||||
| 		messages.add(message); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	/** |  | ||||||
| 	 * Sets the processor to which messages are relayed. |  | ||||||
| 	 * |  | ||||||
| 	 * @param processor the processor to set |  | ||||||
| 	 * @since Envoy v0.3-alpha |  | ||||||
| 	 */ |  | ||||||
| 	public void setProcessor(Consumer<Message> processor) { this.processor = processor; } |  | ||||||
|  |  | ||||||
| 	/** |  | ||||||
| 	 * Relays all cached messages to the processor. |  | ||||||
| 	 * |  | ||||||
| 	 * @since Envoy v0.3-alpha |  | ||||||
| 	 */ |  | ||||||
| 	public void relayMessages() { messages.forEach(processor::accept); } |  | ||||||
| } |  | ||||||
| @@ -13,13 +13,11 @@ import javax.swing.SwingUtilities; | |||||||
|  |  | ||||||
| import envoy.client.Config; | import envoy.client.Config; | ||||||
| import envoy.client.Settings; | import envoy.client.Settings; | ||||||
| import envoy.client.data.LocalDb; | import envoy.client.data.*; | ||||||
| import envoy.client.data.PersistentLocalDb; |  | ||||||
| import envoy.client.data.TransientLocalDb; |  | ||||||
| import envoy.client.net.Client; | import envoy.client.net.Client; | ||||||
| import envoy.client.net.MessageCache; |  | ||||||
| import envoy.client.util.EnvoyLog; | import envoy.client.util.EnvoyLog; | ||||||
| import envoy.data.LoginCredentials; | import envoy.data.LoginCredentials; | ||||||
|  | import envoy.data.Message; | ||||||
| import envoy.data.User; | import envoy.data.User; | ||||||
| import envoy.exception.EnvoyException; | import envoy.exception.EnvoyException; | ||||||
|  |  | ||||||
| @@ -108,7 +106,7 @@ public class Startup { | |||||||
| 		// Acquire the client user (with ID) either from the server or from the local | 		// Acquire the client user (with ID) either from the server or from the local | ||||||
| 		// database, which triggers offline mode | 		// database, which triggers offline mode | ||||||
| 		Client			client	= new Client(); | 		Client			client	= new Client(); | ||||||
| 		MessageCache	cache	= null; | 		Cache<Message>	cache	= null; | ||||||
| 		try { | 		try { | ||||||
| 			// Try entering online mode first | 			// Try entering online mode first | ||||||
| 			localDb.loadIdGenerator(); | 			localDb.loadIdGenerator(); | ||||||
| @@ -178,7 +176,7 @@ public class Startup { | |||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		// Relay unread messages from cache | 		// Relay unread messages from cache | ||||||
| 		if (cache != null) cache.relayMessages(); | 		if (cache != null) cache.relay(); | ||||||
|  |  | ||||||
| 		// Save Settings and PersistentLocalDb on shutdown | 		// Save Settings and PersistentLocalDb on shutdown | ||||||
| 		Runtime.getRuntime().addShutdownHook(new Thread(() -> { | 		Runtime.getRuntime().addShutdownHook(new Thread(() -> { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user