2020-02-06 21:03:08 +01:00
|
|
|
package envoy.client.net;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2020-03-24 18:38:47 +01:00
|
|
|
import envoy.client.data.LocalDB;
|
2020-02-06 21:03:08 +01:00
|
|
|
import envoy.data.Message;
|
|
|
|
import envoy.event.MessageStatusChangeEvent;
|
2020-03-05 14:27:40 +01:00
|
|
|
import envoy.util.EnvoyLog;
|
2020-02-06 21:03:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements methods to send {@link Message}s and
|
|
|
|
* {@link MessageStatusChangeEvent}s to the server or cache them inside a
|
2020-03-24 18:38:47 +01:00
|
|
|
* {@link LocalDB} depending on the online status.<br>
|
2020-02-06 21:03:08 +01:00
|
|
|
* <br>
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>WriteProxy.java</strong><br>
|
|
|
|
* Created: <strong>6 Feb 2020</strong><br>
|
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-06 21:03:08 +01:00
|
|
|
*/
|
|
|
|
public class WriteProxy {
|
|
|
|
|
|
|
|
private final Client client;
|
2020-03-24 18:38:47 +01:00
|
|
|
private final LocalDB localDB;
|
2020-02-06 21:03:08 +01:00
|
|
|
|
2020-02-18 07:16:35 +01:00
|
|
|
private static final Logger logger = EnvoyLog.getLogger(WriteProxy.class);
|
2020-02-06 21:03:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a write proxy using a client and a local database. The
|
|
|
|
* corresponding cache processors are injected into the caches.
|
|
|
|
*
|
|
|
|
* @param client the client used to send messages and message status change
|
|
|
|
* events
|
2020-03-24 18:38:47 +01:00
|
|
|
* @param localDB the local database used to cache messages and message status
|
2020-02-06 21:03:08 +01:00
|
|
|
* change events
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-06 21:03:08 +01:00
|
|
|
*/
|
2020-03-24 18:38:47 +01:00
|
|
|
public WriteProxy(Client client, LocalDB localDB) {
|
2020-02-06 21:03:08 +01:00
|
|
|
this.client = client;
|
2020-03-24 18:38:47 +01:00
|
|
|
this.localDB = localDB;
|
2020-02-06 21:03:08 +01:00
|
|
|
|
|
|
|
// Initialize cache processors for messages and message status change events
|
2020-03-24 18:38:47 +01:00
|
|
|
localDB.getMessageCache().setProcessor(msg -> {
|
2020-02-06 21:03:08 +01:00
|
|
|
try {
|
2020-02-18 16:34:14 +01:00
|
|
|
logger.finer("Sending cached " + msg);
|
2020-02-06 21:03:08 +01:00
|
|
|
client.sendMessage(msg);
|
2020-03-14 16:58:19 +01:00
|
|
|
|
2020-03-26 16:06:18 +01:00
|
|
|
// Update message state to SENT in localDB
|
2020-06-10 13:05:44 +02:00
|
|
|
localDB.getMessage(msg.getID()).ifPresent(Message::nextStatus);
|
2020-02-06 21:03:08 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
logger.log(Level.SEVERE, "Could not send cached message", e);
|
|
|
|
}
|
|
|
|
});
|
2020-03-24 18:38:47 +01:00
|
|
|
localDB.getStatusCache().setProcessor(evt -> {
|
2020-02-18 16:34:14 +01:00
|
|
|
logger.finer("Sending cached " + evt);
|
2020-02-06 21:03:08 +01:00
|
|
|
try {
|
|
|
|
client.sendEvent(evt);
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.log(Level.SEVERE, "Could not send cached message status change event", e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends cached {@link Message}s and {@link MessageStatusChangeEvent}s to the
|
|
|
|
* server.
|
2020-02-07 09:39:15 +01:00
|
|
|
*
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-06 21:03:08 +01:00
|
|
|
*/
|
|
|
|
public void flushCache() {
|
|
|
|
// Send messages
|
2020-03-24 18:38:47 +01:00
|
|
|
localDB.getMessageCache().relay();
|
2020-02-06 21:03:08 +01:00
|
|
|
|
|
|
|
// Send message status change events
|
2020-03-24 18:38:47 +01:00
|
|
|
localDB.getStatusCache().relay();
|
2020-02-06 21:03:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delivers a message to the server if online. Otherwise the message is cached
|
|
|
|
* inside the local database.
|
|
|
|
*
|
|
|
|
* @param message the message to send
|
|
|
|
* @throws IOException if the message could not be sent
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-06 21:03:08 +01:00
|
|
|
*/
|
|
|
|
public void writeMessage(Message message) throws IOException {
|
|
|
|
if (client.isOnline()) client.sendMessage(message);
|
2020-03-24 18:38:47 +01:00
|
|
|
else localDB.getMessageCache().accept(message);
|
2020-02-06 21:03:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delivers a message status change event to the server if online. Otherwise the
|
|
|
|
* event is cached inside the local database.
|
|
|
|
*
|
|
|
|
* @param evt the event to send
|
|
|
|
* @throws IOException if the event could not be sent
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-06 21:03:08 +01:00
|
|
|
*/
|
|
|
|
public void writeMessageStatusChangeEvent(MessageStatusChangeEvent evt) throws IOException {
|
|
|
|
if (client.isOnline()) client.sendEvent(evt);
|
2020-03-24 18:38:47 +01:00
|
|
|
else localDB.getStatusCache().accept(evt);
|
2020-02-06 21:03:08 +01:00
|
|
|
}
|
|
|
|
}
|