From 9498bf216d98046fe7822b124813376d4e098fb9 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 12 Feb 2020 07:53:24 +0100 Subject: [PATCH] Split Client#onlineInit method up into performHandshake and initReceiver --- pom.xml | 2 +- src/main/java/envoy/client/net/Client.java | 46 ++++++++++++++++++---- src/main/java/envoy/client/ui/Startup.java | 5 ++- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index ec25577..6106227 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - develop-SNAPSHOT + f~advanced_login-SNAPSHOT diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 9f84364..b404807 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -42,6 +42,7 @@ public class Client implements Closeable { // Asynchronously initialized during handshake private volatile User sender; private volatile Contacts contacts; + private volatile boolean rejected; // Configuration and logging private static final Config config = Config.getInstance(); @@ -54,16 +55,19 @@ public class Client implements Closeable { * an exception is thrown. * * @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 + * @throws TimeLimitExceededException if the server could not be reached + * @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 onlineInit(LoginCredentials credentials, LocalDb localDb, Cache receivedMessageCache) throws Exception { + public void performHandshake(LoginCredentials credentials, Cache receivedMessageCache) + throws TimeLimitExceededException, IOException, InterruptedException { + if (online) throw new IllegalStateException("Handshake has already been performed successfully"); + // Establish TCP connection logger.info(String.format("Attempting connection to server %s:%d...", config.getServer(), config.getPort())); socket = new Socket(config.getServer(), config.getPort()); @@ -76,7 +80,9 @@ public class Client implements Closeable { 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, receivedMessageCache); - receiver.registerProcessor(HandshakeRejectionEvent.class, EventBus.getInstance()::dispatch); + receiver.registerProcessor(HandshakeRejectionEvent.class, evt -> { rejected = true; EventBus.getInstance().dispatch(evt); }); + + rejected = false; // Start receiver new Thread(receiver).start(); @@ -88,6 +94,15 @@ public class Client implements Closeable { // Wait for a maximum of five seconds to acquire the sender object long start = System.currentTimeMillis(); while (sender == null || contacts == null) { + + // Quit immediately after handshake rejection + // This method can then be called again + if (rejected) { + socket.close(); + receiver.removeAllProcessors(); + return; + } + if (System.currentTimeMillis() - start > 5000) throw new TimeLimitExceededException("Did not log in after 5 seconds"); Thread.sleep(500); } @@ -97,8 +112,23 @@ public class Client implements Closeable { // Remove user creation processor receiver.removeAllProcessors(); + } - // Register processors for message and status handling + /** + * 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 + * @throws IOException if no {@link IdGenerator} is present and none could be + * requested from the server + * @since Envoy v0.2-alpha + */ + public void initReceiver(LocalDb localDb, Cache receivedMessageCache) throws IOException { + checkOnline(); // Process incoming messages final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor(); diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index db55c53..f31cfa3 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -62,7 +62,7 @@ public class Startup { if (args.length > 0) config.load(args); // Check if all mandatory configuration values have been initialized - if (!config.isInitialized()) throw new EnvoyException("Server or port are not defined"); + if (!config.isInitialized()) throw new EnvoyException("Configuration is not fully initialized"); } catch (Exception e) { JOptionPane .showMessageDialog(null, "Error loading configuration values:\n" + e.toString(), "Configuration error", JOptionPane.ERROR_MESSAGE); @@ -109,7 +109,8 @@ public class Startup { try { // Try entering online mode first localDb.loadIdGenerator(); - client.onlineInit(credentials, localDb, cache); + client.performHandshake(credentials, cache); + client.initReceiver(localDb, cache); } catch (Exception e1) { logger.warning("Could not connect to server. Trying offline mode..."); e1.printStackTrace();