--- /dev/null
+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;
+
+}
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.
@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
--- /dev/null
+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()));
+ }
+
+ }
+
+}