1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.util.concurrent.ExecutorService;
5 import java.util.concurrent.Future;
6 import java.util.concurrent.atomic.AtomicReference;
8 import net.pterodactylus.fcp.FcpKeyPair;
9 import net.pterodactylus.fcp.GenerateSSK;
10 import net.pterodactylus.fcp.SSKKeypair;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import com.google.common.util.concurrent.MoreExecutors;
17 * Implementation of the {@link GenerateKeypairCommand}.
19 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
21 class GenerateKeypairCommandImpl implements GenerateKeypairCommand {
23 private final ListeningExecutorService threadPool;
24 private final ConnectionSupplier connectionSupplier;
26 GenerateKeypairCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
27 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
28 this.connectionSupplier = connectionSupplier;
32 public ListenableFuture<FcpKeyPair> execute() {
33 return threadPool.submit(() -> new FcpKeyPairReplySequence().send(new GenerateSSK()).get());
36 private class FcpKeyPairReplySequence extends FcpReplySequence<FcpKeyPair> {
38 private AtomicReference<FcpKeyPair> keyPair = new AtomicReference<>();
40 public FcpKeyPairReplySequence() throws IOException {
41 super(GenerateKeypairCommandImpl.this.threadPool, GenerateKeypairCommandImpl.this.connectionSupplier.get());
45 protected boolean isFinished() {
46 return keyPair.get() != null;
50 protected FcpKeyPair getResult() {
55 protected void consumeSSKKeypair(SSKKeypair sskKeypair) {
56 keyPair.set(new FcpKeyPair(sskKeypair.getRequestURI(), sskKeypair.getInsertURI()));