Rework connection sequence to return whether it was successful
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 10 Jul 2015 12:20:18 +0000 (14:20 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 10 Jul 2015 12:20:18 +0000 (14:20 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java

index a87cd30..9c03291 100644 (file)
@@ -45,26 +45,27 @@ public class ClientHelloImpl {
        private FcpConnection establishConnection() throws IOException {
                FcpConnection connection = new FcpConnection(hostname, port);
                connection.connect();
-               FcpReplySequence<?> nodeHelloSequence = new ClientHelloReplySequence(connection);
+               ClientHelloReplySequence nodeHelloSequence = new ClientHelloReplySequence(connection);
                ClientHello clientHello = new ClientHello(clientName.get(), "2.0");
                try {
-                       nodeHelloSequence.send(clientHello).get();
+                       if (nodeHelloSequence.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 ClientHelloReplySequence extends FcpReplySequence<Boolean> {
 
-               private final AtomicReference<NodeHello> receivedNodeHello;
-               private final AtomicBoolean receivedClosed;
+               private final AtomicReference<NodeHello> receivedNodeHello = new AtomicReference<>();
+               private final AtomicBoolean receivedClosed = new AtomicBoolean();
 
                public ClientHelloReplySequence(FcpConnection connection) {
                        super(ClientHelloImpl.this.threadPool, connection);
-                       receivedNodeHello = new AtomicReference<>();
-                       receivedClosed = new AtomicBoolean();
                }
 
                @Override
@@ -73,6 +74,11 @@ public class ClientHelloImpl {
                }
 
                @Override
+               protected Boolean getResult() {
+                       return receivedNodeHello.get() != null;
+               }
+
+               @Override
                protected void consumeNodeHello(NodeHello nodeHello) {
                        receivedNodeHello.set(nodeHello);
                }