1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.util.concurrent.ExecutionException;
5 import java.util.concurrent.ExecutorService;
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(this::executeSequence);
36 private FcpKeyPair executeSequence() throws InterruptedException, ExecutionException, IOException {
37 try (FcpKeyPairReplySequence fcpKeyPairReplySequence = new FcpKeyPairReplySequence()) {
38 return fcpKeyPairReplySequence.send(new GenerateSSK()).get();
42 private class FcpKeyPairReplySequence extends FcpReplySequence<FcpKeyPair> {
44 private AtomicReference<FcpKeyPair> keyPair = new AtomicReference<>();
46 public FcpKeyPairReplySequence() throws IOException {
47 super(GenerateKeypairCommandImpl.this.threadPool, GenerateKeypairCommandImpl.this.connectionSupplier.get());
51 protected boolean isFinished() {
52 return keyPair.get() != null;
56 protected FcpKeyPair getResult() {
61 protected void consumeSSKKeypair(SSKKeypair sskKeypair) {
62 keyPair.set(new FcpKeyPair(sskKeypair.getRequestURI(), sskKeypair.getInsertURI()));