Rename all remaining occurences of “sequence” to “dialog”
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / GenerateKeypairCommandImpl.java
1 package net.pterodactylus.fcp.quelaton;
2
3 import java.io.IOException;
4 import java.util.concurrent.ExecutionException;
5 import java.util.concurrent.ExecutorService;
6 import java.util.concurrent.atomic.AtomicReference;
7
8 import net.pterodactylus.fcp.FcpKeyPair;
9 import net.pterodactylus.fcp.GenerateSSK;
10 import net.pterodactylus.fcp.SSKKeypair;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import com.google.common.util.concurrent.MoreExecutors;
15
16 /**
17  * Implementation of the {@link GenerateKeypairCommand}.
18  *
19  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
20  */
21 class GenerateKeypairCommandImpl implements GenerateKeypairCommand {
22
23         private final ListeningExecutorService threadPool;
24         private final ConnectionSupplier connectionSupplier;
25
26         GenerateKeypairCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
27                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
28                 this.connectionSupplier = connectionSupplier;
29         }
30
31         @Override
32         public ListenableFuture<FcpKeyPair> execute() {
33                 return threadPool.submit(this::executeDialog);
34         }
35
36         private FcpKeyPair executeDialog() throws InterruptedException, ExecutionException, IOException {
37                 try (FcpKeyPairDialog fcpKeyPairDialog = new FcpKeyPairDialog()) {
38                         return fcpKeyPairDialog.send(new GenerateSSK()).get();
39                 }
40         }
41
42         private class FcpKeyPairDialog extends FcpDialog<FcpKeyPair> {
43
44                 private AtomicReference<FcpKeyPair> keyPair = new AtomicReference<>();
45
46                 public FcpKeyPairDialog() throws IOException {
47                         super(GenerateKeypairCommandImpl.this.threadPool, GenerateKeypairCommandImpl.this.connectionSupplier.get());
48                 }
49
50                 @Override
51                 protected boolean isFinished() {
52                         return keyPair.get() != null;
53                 }
54
55                 @Override
56                 protected FcpKeyPair getResult() {
57                         return keyPair.get();
58                 }
59
60                 @Override
61                 protected void consumeSSKKeypair(SSKKeypair sskKeypair) {
62                         keyPair.set(new FcpKeyPair(sskKeypair.getRequestURI(), sskKeypair.getInsertURI()));
63                 }
64
65         }
66
67 }