From: David ‘Bombe’ Roden Date: Mon, 6 Jul 2015 04:47:01 +0000 (+0200) Subject: Move key pair generation command to its own class X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=7980851d84be2f31d0db3837619eb1548c3847c7;p=jFCPlib.git Move key pair generation command to its own class --- 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())); + } + + } + +}