Rename all remaining occurences of “sequence” to “dialog”
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ClientHelloImpl.java
index a87cd30..cb85f60 100644 (file)
@@ -3,11 +3,9 @@ package net.pterodactylus.fcp.quelaton;
 import java.io.IOException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 
 import net.pterodactylus.fcp.ClientHello;
-import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
 import net.pterodactylus.fcp.FcpConnection;
 import net.pterodactylus.fcp.NodeHello;
 
@@ -16,7 +14,7 @@ import com.google.common.util.concurrent.ListeningExecutorService;
 import com.google.common.util.concurrent.MoreExecutors;
 
 /**
- * Internal <code>ClientHello</code> implementation based on {@link FcpReplySequence}.
+ * Internal <code>ClientHello</code> implementation based on {@link FcpDialog}.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
@@ -39,48 +37,46 @@ public class ClientHelloImpl {
        }
 
        private ListenableFuture<FcpConnection> execute() {
-               return threadPool.submit(() -> establishConnection());
+               return threadPool.submit(this::establishConnection);
        }
 
        private FcpConnection establishConnection() throws IOException {
                FcpConnection connection = new FcpConnection(hostname, port);
                connection.connect();
-               FcpReplySequence<?> nodeHelloSequence = new ClientHelloReplySequence(connection);
                ClientHello clientHello = new ClientHello(clientName.get(), "2.0");
-               try {
-                       nodeHelloSequence.send(clientHello).get();
+               try (ClientHelloDialog clientHelloDialog = new ClientHelloDialog(connection)) {
+                       if (clientHelloDialog.send(clientHello).get()) {
+                               return connection;
+                       }
                } catch (InterruptedException | ExecutionException e) {
                        connection.close();
                        throw new IOException(String.format("Could not connect to %s:%d.", hostname, port), e);
                }
-               return connection;
+               connection.close();
+               throw new IOException(String.format("Could not connect to %s:%d.", hostname, port));
        }
 
-       private class ClientHelloReplySequence extends FcpReplySequence<Void> {
+       private class ClientHelloDialog extends FcpDialog<Boolean> {
 
-               private final AtomicReference<NodeHello> receivedNodeHello;
-               private final AtomicBoolean receivedClosed;
+               private final AtomicReference<NodeHello> receivedNodeHello = new AtomicReference<>();
 
-               public ClientHelloReplySequence(FcpConnection connection) {
+               public ClientHelloDialog(FcpConnection connection) {
                        super(ClientHelloImpl.this.threadPool, connection);
-                       receivedNodeHello = new AtomicReference<>();
-                       receivedClosed = new AtomicBoolean();
                }
 
                @Override
                protected boolean isFinished() {
-                       return receivedNodeHello.get() != null || receivedClosed.get();
+                       return receivedNodeHello.get() != null;
                }
 
                @Override
-               protected void consumeNodeHello(NodeHello nodeHello) {
-                       receivedNodeHello.set(nodeHello);
+               protected Boolean getResult() {
+                       return receivedNodeHello.get() != null;
                }
 
                @Override
-               protected void consumeCloseConnectionDuplicateClientName(
-                       CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
-                       receivedClosed.set(true);
+               protected void consumeNodeHello(NodeHello nodeHello) {
+                       receivedNodeHello.set(nodeHello);
                }
 
        }