b12e225ff42bfd7ad50f68305c1e075c2814eda9
[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         @Override
46         public Executable<Boolean> byHostAndPort(String host, int port) {
47                 nodeIdentifier.set(String.format("%s:%d", host, port));
48                 return this::execute;
49         }
50
51         private ListenableFuture<Boolean> execute() {
52                 return threadPool.submit(this::executeDialog);
53         }
54
55         private boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
56                 RemovePeer removePeer = new RemovePeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
57                 try (RemovePeerDialog removePeerDialog = new RemovePeerDialog()) {
58                         return removePeerDialog.send(removePeer).get();
59                 }
60         }
61
62         private class RemovePeerDialog extends FcpDialog<Boolean> {
63
64                 private final AtomicBoolean finished = new AtomicBoolean();
65                 private final AtomicBoolean removed = new AtomicBoolean();
66
67                 public RemovePeerDialog() throws IOException {
68                         super(threadPool, connectionSupplier.get());
69                 }
70
71                 @Override
72                 protected boolean isFinished() {
73                         return finished.get() || removed.get();
74                 }
75
76                 @Override
77                 protected Boolean getResult() {
78                         return removed.get();
79                 }
80
81                 @Override
82                 protected void consumePeerRemoved(PeerRemoved peerRemoved) {
83                         removed.set(true);
84                 }
85
86                 @Override
87                 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
88                         finished.set(true);
89                 }
90
91         }
92
93 }