From 2012a2dad86529e1a01528c075e9a87fcb31f156 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 10 Jul 2015 23:16:23 +0200 Subject: [PATCH] Reconnect when connection fails --- .../java/net/pterodactylus/fcp/FcpConnection.java | 4 ++++ .../fcp/quelaton/DefaultFcpClient.java | 4 ++-- .../fcp/quelaton/DefaultFcpClientTest.java | 27 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/pterodactylus/fcp/FcpConnection.java b/src/main/java/net/pterodactylus/fcp/FcpConnection.java index 66377a2..cc13d4a 100644 --- a/src/main/java/net/pterodactylus/fcp/FcpConnection.java +++ b/src/main/java/net/pterodactylus/fcp/FcpConnection.java @@ -132,6 +132,10 @@ public class FcpConnection implements Closeable { this.port = port; } + public synchronized boolean isClosed() { + return connectionHandler == null; + } + // // LISTENER MANAGEMENT // diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java index 8d2afc0..d40659a 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java @@ -33,11 +33,11 @@ public class DefaultFcpClient implements FcpClient { private FcpConnection connect() throws IOException { FcpConnection fcpConnection = this.fcpConnection.get(); - if (fcpConnection != null) { + if ((fcpConnection != null) && !fcpConnection.isClosed()) { return fcpConnection; } fcpConnection = createConnection(); - this.fcpConnection.compareAndSet(null, fcpConnection); + this.fcpConnection.set(fcpConnection); return fcpConnection; } diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index 47b787d..bfb2b78 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -33,6 +33,7 @@ import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeDiagnosingMatcher; import org.junit.After; +import org.junit.Assert; import org.junit.Test; /** @@ -267,6 +268,32 @@ public class DefaultFcpClientTest { } @Test + public void defaultFcpClientCanReconnectAfterConnectionHasBeenClosed() + throws InterruptedException, ExecutionException, IOException { + Future keyPair = fcpClient.generateKeypair().execute(); + connectNode(); + fcpServer.collectUntil(is("EndMessage")); + fcpServer.close(); + try { + keyPair.get(); + Assert.fail(); + } catch (ExecutionException e) { + } + keyPair = fcpClient.generateKeypair().execute(); + connectNode(); + List lines = fcpServer.collectUntil(is("EndMessage")); + String identifier = extractIdentifier(lines); + fcpServer.writeLine( + "SSKKeypair", + "InsertURI=" + INSERT_URI + "", + "RequestURI=" + REQUEST_URI + "", + "Identifier=" + identifier, + "EndMessage" + ); + keyPair.get(); + } + + @Test public void clientGetWithIgnoreDataStoreSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException { fcpClient.clientGet().ignoreDataStore().uri("KSK@foo.txt").execute(); -- 2.7.4