Reconnect when connection fails
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 10 Jul 2015 21:16:23 +0000 (23:16 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 10 Jul 2015 21:16:23 +0000 (23:16 +0200)
src/main/java/net/pterodactylus/fcp/FcpConnection.java
src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index 66377a2..cc13d4a 100644 (file)
@@ -132,6 +132,10 @@ public class FcpConnection implements Closeable {
                this.port = port;
        }
 
+       public synchronized boolean isClosed() {
+               return connectionHandler == null;
+       }
+
        //
        // LISTENER MANAGEMENT
        //
index 8d2afc0..d40659a 100644 (file)
@@ -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;
        }
 
index 47b787d..bfb2b78 100644 (file)
@@ -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<FcpKeyPair> 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<String> 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();