Move key pair generation command to its own class
[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.ExecutorService;
5 import java.util.concurrent.Future;
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 /**
13  * Implementation of the {@link GenerateKeypairCommand}.
14  *
15  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
16  */
17 class GenerateKeypairCommandImpl implements GenerateKeypairCommand {
18
19         private final ExecutorService threadPool;
20         private final ConnectionSupplier connectionSupplier;
21
22         GenerateKeypairCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
23                 this.threadPool = threadPool;
24                 this.connectionSupplier = connectionSupplier;
25         }
26
27         @Override
28         public Future<FcpKeyPair> execute() {
29                 return threadPool.submit(() -> new FcpKeyPairReplySequence().send(new GenerateSSK()).get());
30         }
31
32         private class FcpKeyPairReplySequence extends FcpReplySequence<FcpKeyPair> {
33
34                 private AtomicReference<FcpKeyPair> keyPair = new AtomicReference<>();
35
36                 public FcpKeyPairReplySequence() throws IOException {
37                         super(GenerateKeypairCommandImpl.this.threadPool, GenerateKeypairCommandImpl.this.connectionSupplier.get());
38                 }
39
40                 @Override
41                 protected boolean isFinished() {
42                         return keyPair.get() != null;
43                 }
44
45                 @Override
46                 protected FcpKeyPair getResult() {
47                         return keyPair.get();
48                 }
49
50                 @Override
51                 protected void consumeSSKKeypair(SSKKeypair sskKeypair) {
52                         keyPair.set(new FcpKeyPair(sskKeypair.getRequestURI(), sskKeypair.getInsertURI()));
53                 }
54
55         }
56
57 }