Add command that removes a peer from the node by name
[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         private ListenableFuture<Boolean> execute() {
40                 return threadPool.submit(this::executeDialog);
41         }
42
43         private boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
44                 RemovePeer removePeer = new RemovePeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
45                 try (RemovePeerDialog removePeerDialog = new RemovePeerDialog()) {
46                         return removePeerDialog.send(removePeer).get();
47                 }
48         }
49
50         private class RemovePeerDialog extends FcpDialog<Boolean> {
51
52                 private final AtomicBoolean finished = new AtomicBoolean();
53                 private final AtomicBoolean removed = new AtomicBoolean();
54
55                 public RemovePeerDialog() throws IOException {
56                         super(threadPool, connectionSupplier.get());
57                 }
58
59                 @Override
60                 protected boolean isFinished() {
61                         return finished.get() || removed.get();
62                 }
63
64                 @Override
65                 protected Boolean getResult() {
66                         return removed.get();
67                 }
68
69                 @Override
70                 protected void consumePeerRemoved(PeerRemoved peerRemoved) {
71                         removed.set(true);
72                 }
73
74                 @Override
75                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
76                         finished.set(true);
77                 }
78
79         }
80
81 }