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