Refactor FCP dialog
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / RemovePeerCommandImpl.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 import java.util.function.Supplier;
8
9 import net.pterodactylus.fcp.PeerRemoved;
10 import net.pterodactylus.fcp.RemovePeer;
11 import net.pterodactylus.fcp.UnknownNodeIdentifier;
12
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import com.google.common.util.concurrent.MoreExecutors;
16
17 /**
18  * Default {@link RemovePeerCommand} implementation based on {@link FcpDialog}.
19  *
20  * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
21  */
22 public class RemovePeerCommandImpl implements RemovePeerCommand {
23
24         private final ListeningExecutorService threadPool;
25         private final ConnectionSupplier connectionSupplier;
26         private final Supplier<String> identifierGenerator;
27         private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
28
29         public RemovePeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
30                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
31                 this.connectionSupplier = connectionSupplier;
32                 this.identifierGenerator = identifierGenerator;
33         }
34
35         @Override
36         public Executable<Boolean> byName(String name) {
37                 nodeIdentifier.set(name);
38                 return this::execute;
39         }
40
41         @Override
42         public Executable<Boolean> byIdentity(String nodeIdentity) {
43                 nodeIdentifier.set(nodeIdentity);
44                 return this::execute;
45         }
46
47         @Override
48         public Executable<Boolean> byHostAndPort(String host, int port) {
49                 nodeIdentifier.set(String.format("%s:%d", host, port));
50                 return this::execute;
51         }
52
53         private ListenableFuture<Boolean> execute() {
54                 return threadPool.submit(this::executeDialog);
55         }
56
57         private boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
58                 RemovePeer removePeer = new RemovePeer(identifierGenerator.get(), nodeIdentifier.get());
59                 try (RemovePeerDialog removePeerDialog = new RemovePeerDialog()) {
60                         return removePeerDialog.send(removePeer).get();
61                 }
62         }
63
64         private class RemovePeerDialog extends FcpDialog<Boolean> {
65
66                 public RemovePeerDialog() throws IOException {
67                         super(threadPool, connectionSupplier.get(), false);
68                 }
69
70                 @Override
71                 protected void consumePeerRemoved(PeerRemoved peerRemoved) {
72                         setResult(true);
73                 }
74
75                 @Override
76                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
77                         finish();
78                 }
79
80         }
81
82 }