6b791e8ffddd97772e6dc697b785828022ed064b
[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.AtomicBoolean;
7 import java.util.concurrent.atomic.AtomicReference;
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 AtomicReference<String> nodeIdentifier = new AtomicReference<>();
27
28         public RemovePeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
29                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
30                 this.connectionSupplier = connectionSupplier;
31         }
32
33         @Override
34         public Executable<Boolean> byName(String name) {
35                 nodeIdentifier.set(name);
36                 return this::execute;
37         }
38
39         @Override
40         public Executable<Boolean> byIdentity(String nodeIdentity) {
41                 nodeIdentifier.set(nodeIdentity);
42                 return this::execute;
43         }
44
45         private ListenableFuture<Boolean> execute() {
46                 return threadPool.submit(this::executeDialog);
47         }
48
49         private boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
50                 RemovePeer removePeer = new RemovePeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
51                 try (RemovePeerDialog removePeerDialog = new RemovePeerDialog()) {
52                         return removePeerDialog.send(removePeer).get();
53                 }
54         }
55
56         private class RemovePeerDialog extends FcpDialog<Boolean> {
57
58                 private final AtomicBoolean finished = new AtomicBoolean();
59                 private final AtomicBoolean removed = new AtomicBoolean();
60
61                 public RemovePeerDialog() throws IOException {
62                         super(threadPool, connectionSupplier.get());
63                 }
64
65                 @Override
66                 protected boolean isFinished() {
67                         return finished.get() || removed.get();
68                 }
69
70                 @Override
71                 protected Boolean getResult() {
72                         return removed.get();
73                 }
74
75                 @Override
76                 protected void consumePeerRemoved(PeerRemoved peerRemoved) {
77                         removed.set(true);
78                 }
79
80                 @Override
81                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
82                         finished.set(true);
83                 }
84
85         }
86
87 }