Move key pair generation command to its own class
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 6 Jul 2015 04:47:01 +0000 (06:47 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 6 Jul 2015 04:47:01 +0000 (06:47 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/ConnectionSupplier.java [new file with mode: 0644]
src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java
src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ConnectionSupplier.java b/src/main/java/net/pterodactylus/fcp/quelaton/ConnectionSupplier.java
new file mode 100644 (file)
index 0000000..2be80cc
--- /dev/null
@@ -0,0 +1,18 @@
+package net.pterodactylus.fcp.quelaton;
+
+import java.io.IOException;
+import java.util.function.Supplier;
+
+import net.pterodactylus.fcp.FcpConnection;
+
+/**
+ * Extension of the {@link Supplier} interfaces that declares an {@link IOException}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+@FunctionalInterface
+interface ConnectionSupplier {
+
+       FcpConnection get() throws IOException;
+
+}
index 6edce63..79c7f44 100644 (file)
@@ -15,14 +15,11 @@ import net.pterodactylus.fcp.ClientGet;
 import net.pterodactylus.fcp.ClientHello;
 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
 import net.pterodactylus.fcp.FcpConnection;
-import net.pterodactylus.fcp.FcpKeyPair;
 import net.pterodactylus.fcp.FcpUtils.TempInputStream;
-import net.pterodactylus.fcp.GenerateSSK;
 import net.pterodactylus.fcp.GetFailed;
 import net.pterodactylus.fcp.NodeHello;
 import net.pterodactylus.fcp.Priority;
 import net.pterodactylus.fcp.ReturnType;
-import net.pterodactylus.fcp.SSKKeypair;
 
 /**
  * Default {@link FcpClient} implementation.
@@ -91,36 +88,7 @@ public class DefaultFcpClient implements FcpClient {
 
        @Override
        public GenerateKeypairCommand generateKeypair() {
-               return new GenerateKeypairCommandImpl();
-       }
-
-       private class GenerateKeypairCommandImpl implements GenerateKeypairCommand {
-
-               @Override
-               public Future<FcpKeyPair> execute() {
-                       return threadPool.submit(() -> {
-                               connect();
-                               return new FcpReplySequence<FcpKeyPair>(threadPool, connect()) {
-                                       private AtomicReference<FcpKeyPair> keyPair = new AtomicReference<>();
-
-                                       @Override
-                                       protected boolean isFinished() {
-                                               return keyPair.get() != null;
-                                       }
-
-                                       @Override
-                                       protected FcpKeyPair getResult() {
-                                               return keyPair.get();
-                                       }
-
-                                       @Override
-                                       protected void consumeSSKKeypair(SSKKeypair sskKeypair) {
-                                               keyPair.set(new FcpKeyPair(sskKeypair.getRequestURI(), sskKeypair.getInsertURI()));
-                                       }
-                               }.send(new GenerateSSK()).get();
-                       });
-               }
-
+               return new GenerateKeypairCommandImpl(threadPool, this::connect);
        }
 
        @Override
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java
new file mode 100644 (file)
index 0000000..6f5df33
--- /dev/null
@@ -0,0 +1,57 @@
+package net.pterodactylus.fcp.quelaton;
+
+import java.io.IOException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.atomic.AtomicReference;
+
+import net.pterodactylus.fcp.FcpKeyPair;
+import net.pterodactylus.fcp.GenerateSSK;
+import net.pterodactylus.fcp.SSKKeypair;
+
+/**
+ * Implementation of the {@link GenerateKeypairCommand}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+class GenerateKeypairCommandImpl implements GenerateKeypairCommand {
+
+       private final ExecutorService threadPool;
+       private final ConnectionSupplier connectionSupplier;
+
+       GenerateKeypairCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+               this.threadPool = threadPool;
+               this.connectionSupplier = connectionSupplier;
+       }
+
+       @Override
+       public Future<FcpKeyPair> execute() {
+               return threadPool.submit(() -> new FcpKeyPairReplySequence().send(new GenerateSSK()).get());
+       }
+
+       private class FcpKeyPairReplySequence extends FcpReplySequence<FcpKeyPair> {
+
+               private AtomicReference<FcpKeyPair> keyPair = new AtomicReference<>();
+
+               public FcpKeyPairReplySequence() throws IOException {
+                       super(GenerateKeypairCommandImpl.this.threadPool, GenerateKeypairCommandImpl.this.connectionSupplier.get());
+               }
+
+               @Override
+               protected boolean isFinished() {
+                       return keyPair.get() != null;
+               }
+
+               @Override
+               protected FcpKeyPair getResult() {
+                       return keyPair.get();
+               }
+
+               @Override
+               protected void consumeSSKKeypair(SSKKeypair sskKeypair) {
+                       keyPair.set(new FcpKeyPair(sskKeypair.getRequestURI(), sskKeypair.getInsertURI()));
+               }
+
+       }
+
+}