Refactor FCP dialog
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ClientHelloImpl.java
index 9c03291..551b52d 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,16 +37,15 @@ 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();
-               ClientHelloReplySequence nodeHelloSequence = new ClientHelloReplySequence(connection);
                ClientHello clientHello = new ClientHello(clientName.get(), "2.0");
-               try {
-                       if (nodeHelloSequence.send(clientHello).get()) {
+               try (ClientHelloDialog clientHelloDialog = new ClientHelloDialog(connection)) {
+                       if (clientHelloDialog.send(clientHello).get()) {
                                return connection;
                        }
                } catch (InterruptedException | ExecutionException e) {
@@ -59,34 +56,15 @@ public class ClientHelloImpl {
                throw new IOException(String.format("Could not connect to %s:%d.", hostname, port));
        }
 
-       private class ClientHelloReplySequence extends FcpReplySequence<Boolean> {
+       private class ClientHelloDialog extends FcpDialog<Boolean> {
 
-               private final AtomicReference<NodeHello> receivedNodeHello = new AtomicReference<>();
-               private final AtomicBoolean receivedClosed = new AtomicBoolean();
-
-               public ClientHelloReplySequence(FcpConnection connection) {
-                       super(ClientHelloImpl.this.threadPool, connection);
-               }
-
-               @Override
-               protected boolean isFinished() {
-                       return receivedNodeHello.get() != null || receivedClosed.get();
-               }
-
-               @Override
-               protected Boolean getResult() {
-                       return receivedNodeHello.get() != null;
+               public ClientHelloDialog(FcpConnection connection) {
+                       super(ClientHelloImpl.this.threadPool, connection, false);
                }
 
                @Override
                protected void consumeNodeHello(NodeHello nodeHello) {
-                       receivedNodeHello.set(nodeHello);
-               }
-
-               @Override
-               protected void consumeCloseConnectionDuplicateClientName(
-                       CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
-                       receivedClosed.set(true);
+                       setResult(true);
                }
 
        }