From 7980851d84be2f31d0db3837619eb1548c3847c7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 6 Jul 2015 06:47:01 +0200 Subject: [PATCH] Move key pair generation command to its own class --- .../fcp/quelaton/ConnectionSupplier.java | 18 +++++++ .../fcp/quelaton/DefaultFcpClient.java | 34 +------------ .../fcp/quelaton/GenerateKeypairCommandImpl.java | 57 ++++++++++++++++++++++ 3 files changed, 76 insertions(+), 33 deletions(-) create mode 100644 src/main/java/net/pterodactylus/fcp/quelaton/ConnectionSupplier.java create mode 100644 src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java 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 index 0000000..2be80cc --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ConnectionSupplier.java @@ -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 David ‘Bombe’ Roden + */ +@FunctionalInterface +interface ConnectionSupplier { + + FcpConnection get() throws IOException; + +} diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java index 6edce63..79c7f44 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java @@ -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 execute() { - return threadPool.submit(() -> { - connect(); - return new FcpReplySequence(threadPool, connect()) { - private AtomicReference 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 index 0000000..6f5df33 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java @@ -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 David ‘Bombe’ Roden + */ +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 execute() { + return threadPool.submit(() -> new FcpKeyPairReplySequence().send(new GenerateSSK()).get()); + } + + private class FcpKeyPairReplySequence extends FcpReplySequence { + + private AtomicReference 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())); + } + + } + +} -- 2.7.4