1 package net.pterodactylus.fcp.quelaton;
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;
9 import net.pterodactylus.fcp.PeerRemoved;
10 import net.pterodactylus.fcp.RemovePeer;
11 import net.pterodactylus.fcp.UnknownNodeIdentifier;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import com.google.common.util.concurrent.MoreExecutors;
18 * Default {@link RemovePeerCommand} implementation based on {@link FcpDialog}.
20 * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
22 public class RemovePeerCommandImpl implements RemovePeerCommand {
24 private final ListeningExecutorService threadPool;
25 private final ConnectionSupplier connectionSupplier;
26 private final Supplier<String> identifierGenerator;
27 private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
29 public RemovePeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
30 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
31 this.connectionSupplier = connectionSupplier;
32 this.identifierGenerator = identifierGenerator;
36 public Executable<Boolean> byName(String name) {
37 nodeIdentifier.set(name);
42 public Executable<Boolean> byIdentity(String nodeIdentity) {
43 nodeIdentifier.set(nodeIdentity);
48 public Executable<Boolean> byHostAndPort(String host, int port) {
49 nodeIdentifier.set(String.format("%s:%d", host, port));
53 private ListenableFuture<Boolean> execute() {
54 return threadPool.submit(this::executeDialog);
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();
64 private class RemovePeerDialog extends FcpDialog<Boolean> {
66 public RemovePeerDialog() throws IOException {
67 super(threadPool, connectionSupplier.get(), false);
71 protected void consumePeerRemoved(PeerRemoved peerRemoved) {
76 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {