Added improved logging capabilities

This commit is contained in:
delvh
2020-06-13 22:36:52 +02:00
parent 2d92a3afcb
commit 9bf28acfcb
14 changed files with 157 additions and 129 deletions

View File

@@ -7,6 +7,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import envoy.client.data.Cache;
@@ -56,25 +57,29 @@ 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 receivedMessageCache a message cache containing all unread messages
* from the server that can be relayed after
* initialization
* @param receivedMessageStatusChangeEventCache an event cache containing all received messageStatusChangeEvents from the server that can be relayed after initialization
* @param credentials the login credentials of the
* user
* @param receivedMessageCache a message cache containing all
* unread messages from the server
* that can be relayed after
* initialization
* @param receivedMessageStatusChangeEventCache an event cache containing all
* received
* messageStatusChangeEvents from
* the server that can be relayed
* after initialization
* @throws TimeoutException if the server could not be reached
* @throws IOException if the login credentials could not be
* written
* @throws IOException if the login credentials could not be written
* @throws InterruptedException if the current thread is interrupted while
* waiting for the handshake response
*/
public void performHandshake(LoginCredentials credentials, Cache<Message> receivedMessageCache,
Cache<MessageStatusChangeEvent> receivedMessageStatusChangeEventCache)
throws TimeoutException, IOException, InterruptedException {
Cache<MessageStatusChangeEvent> receivedMessageStatusChangeEventCache) throws TimeoutException, IOException, InterruptedException {
if (online) throw new IllegalStateException("Handshake has already been performed successfully");
// Establish TCP connection
logger.finer(String.format("Attempting connection to server %s:%d...", config.getServer(), config.getPort()));
logger.log(Level.FINER, String.format("Attempting connection to server %s:%d...", config.getServer(), config.getPort()));
socket = new Socket(config.getServer(), config.getPort());
logger.fine("Successfully established TCP connection to server");
logger.log(Level.FINE, "Successfully established TCP connection to server");
// Create object receiver
receiver = new Receiver(socket.getInputStream());
@@ -114,19 +119,25 @@ public class Client implements Closeable {
// Remove all processors as they are only used during the handshake
receiver.removeAllProcessors();
logger.info("Handshake completed.");
logger.log(Level.INFO, "Handshake completed.");
}
/**
* Initializes the {@link Receiver} used to process data sent from the server to
* this client.
*
* @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
* @param receivedMessageStatusChangeEventCache an event cache containing all received messageStatusChangeEvents from the server that can be relayed after initialization
* @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
* @param receivedMessageStatusChangeEventCache an event cache containing all
* received
* messageStatusChangeEvents from
* the server that can be relayed
* after initialization
* @throws IOException if no {@link IDGenerator} is present and none could be
* requested from the server
* @since Envoy Client v0.2-alpha
@@ -136,7 +147,7 @@ public class Client implements Closeable {
checkOnline();
// Process incoming messages
final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor();
final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor();
final MessageStatusChangeEventProcessor messageStatusChangeEventProcessor = new MessageStatusChangeEventProcessor();
receiver.registerProcessor(Message.class, receivedMessageProcessor);
@@ -172,6 +183,7 @@ public class Client implements Closeable {
sendEvent(evt.get());
} catch (final IOException e) {
e.printStackTrace();
logger.log(Level.WARNING, "An error occurred when trying to send Event " + evt, e);
}
});
@@ -218,7 +230,7 @@ public class Client implements Closeable {
* @since Envoy Client v0.3-alpha
*/
public void requestIdGenerator() throws IOException {
logger.info("Requesting new id generator...");
logger.log(Level.INFO, "Requesting new id generator...");
writeObject(new IDGeneratorRequest());
}
@@ -240,7 +252,7 @@ public class Client implements Closeable {
private void writeObject(Object obj) throws IOException {
checkOnline();
logger.fine("Sending " + obj);
logger.log(Level.FINE, "Sending " + obj);
SerializationUtils.writeBytesWithLength(obj, socket.getOutputStream());
}
@@ -258,7 +270,7 @@ public class Client implements Closeable {
* @param clientUser the client user to set
* @since Envoy Client v0.2-alpha
*/
public void setSender(User clientUser) { this.sender = clientUser; }
public void setSender(User clientUser) { sender = clientUser; }
/**
* @return the {@link Receiver} used by this {@link Client}