From 4db57d2ff54b90a07f3659e31bad206d07576136 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 10 Jul 2015 14:20:18 +0200 Subject: [PATCH] Rework connection sequence to return whether it was successful --- .../fcp/quelaton/ClientHelloImpl.java | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java index a87cd30..9c03291 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientHelloImpl.java @@ -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 { + private class ClientHelloReplySequence extends FcpReplySequence { - private final AtomicReference receivedNodeHello; - private final AtomicBoolean receivedClosed; + private final AtomicReference 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); } -- 2.7.4