Merge branch 'develop' into f/contacts

This commit is contained in:
2020-02-11 10:06:06 +01:00
committed by GitHub
15 changed files with 461 additions and 157 deletions

View File

@@ -9,7 +9,8 @@ import java.util.logging.Logger;
import javax.naming.TimeLimitExceededException;
import envoy.client.Config;
import envoy.client.data.Cache;
import envoy.client.data.Config;
import envoy.client.data.LocalDb;
import envoy.client.event.SearchResultEvent;
import envoy.client.util.EnvoyLog;
@@ -51,16 +52,17 @@ public class Client implements Closeable {
* will block for up to 5 seconds. If the handshake does exceed this time limit,
* an exception is thrown.
*
* @param credentials the login credentials of the user
* @param localDb the local database used to persist the current
* {@link IdGenerator}
* @return a message cache containing all unread messages from the server that
* can be relayed after initialization
* @param credentials the login credentials of the user
* @param localDb the local database used to persist the current
* {@link IdGenerator}
* @param receivedMessageCache a message cache containing all unread messages
* from the server that can be relayed after
* initialization
* @throws Exception if the online mode could not be entered or the request
* failed for some other reason
* @since Envoy v0.2-alpha
*/
public MessageCache onlineInit(LoginCredentials credentials, LocalDb localDb) throws Exception {
public void onlineInit(LoginCredentials credentials, LocalDb localDb, Cache<Message> receivedMessageCache) throws Exception {
// Establish TCP connection
logger.info(String.format("Attempting connection to server %s:%d...", config.getServer(), config.getPort()));
socket = new Socket(config.getServer(), config.getPort());
@@ -69,13 +71,10 @@ public class Client implements Closeable {
// Create message receiver
receiver = new Receiver(socket.getInputStream());
// Create cache for unread messages
final MessageCache cache = new MessageCache();
// 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(Contacts.class, contacts -> { logger.info("Acquired contacts object " + contacts); this.contacts = contacts; });
receiver.registerProcessor(Message.class, cache);
receiver.registerProcessor(Message.class, receivedMessageCache);
// Start receiver
new Thread(receiver).start();
@@ -98,15 +97,20 @@ public class Client implements Closeable {
receiver.removeAllProcessors();
// Register processors for message and status handling
// Process incoming messages
final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor();
receiver.registerProcessor(Message.class, receivedMessageProcessor);
// Relay cached unread messages
cache.setProcessor(receivedMessageProcessor);
receivedMessageCache.setProcessor(receivedMessageProcessor);
// Process message status changes
receiver.registerProcessor(MessageStatusChangeEvent.class, new MessageStatusChangeEventProcessor());
// Process user status changes
receiver.registerProcessor(UserStatusChangeEvent.class, new UserStatusChangeProcessor(this));
// Process message ID generation
receiver.registerProcessor(IdGenerator.class, localDb::setIdGenerator);
@@ -115,10 +119,19 @@ public class Client implements Closeable {
// Request a generator if none is present or the existing one is consumed
if (!localDb.hasIdGenerator() || !localDb.getIdGenerator().hasNext()) requestIdGenerator();
return cache;
}
/**
* Creates a new write proxy that uses this client to communicate with the
* server.
*
* @param localDb the local database that the write proxy will use to access
* caches
* @return a new write proxy
* @since Envoy Client v0.3-alpha
*/
public WriteProxy createWriteProxy(LocalDb localDb) { return new WriteProxy(this, localDb); }
/**
* Sends a message to the server. The message's status will be incremented once
* it was delivered successfully.
@@ -197,4 +210,16 @@ public class Client implements Closeable {
* @since Envoy v0.2-alpha
*/
public boolean isOnline() { return online; }
}
/**
* @return the contacts of this {@link Client}
* @since Envoy v0.3-alpha
*/
public Contacts getContacts() { return contacts; }
/**
* @param contacts the contacts to set
* @since Envoy v0.3-alpha
*/
public void setContacts(Contacts contacts) { this.contacts = contacts; }
}