1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.util.Optional;
5 import java.util.concurrent.ExecutionException;
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.atomic.AtomicBoolean;
8 import java.util.concurrent.atomic.AtomicReference;
9 import java.util.function.Supplier;
11 import net.pterodactylus.fcp.ListPeer;
12 import net.pterodactylus.fcp.Peer;
13 import net.pterodactylus.fcp.UnknownNodeIdentifier;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.ListeningExecutorService;
17 import com.google.common.util.concurrent.MoreExecutors;
20 * Default {@link ListPeerCommand} implementation based on {@link FcpDialog}.
22 * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
24 public class ListPeerCommandImpl implements ListPeerCommand {
26 private final ListeningExecutorService threadPool;
27 private final ConnectionSupplier connectionSupplier;
28 private final Supplier<String> identifierGenerator;
29 private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
31 public ListPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
32 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
33 this.connectionSupplier = connectionSupplier;
34 this.identifierGenerator = identifierGenerator;
38 public Executable<Optional<Peer>> byName(String name) {
39 nodeIdentifier.set(name);
44 public Executable<Optional<Peer>> byIdentity(String identity) {
45 nodeIdentifier.set(identity);
50 public Executable<Optional<Peer>> byHostAndPort(String host, int port) {
51 nodeIdentifier.set(String.format("%s:%d", host, port));
55 private ListenableFuture<Optional<Peer>> execute() {
56 return threadPool.submit(this::executeDialog);
59 private Optional<Peer> executeDialog() throws IOException, ExecutionException, InterruptedException {
60 ListPeer listPeer = new ListPeer(identifierGenerator.get(), nodeIdentifier.get());
61 try (ListPeerDialog listPeerDialog = new ListPeerDialog()) {
62 return Optional.ofNullable(listPeerDialog.send(listPeer).get());
66 private class ListPeerDialog extends FcpDialog<Peer> {
68 private final AtomicBoolean finished = new AtomicBoolean();
69 private final AtomicReference<Peer> peer = new AtomicReference<>();
71 public ListPeerDialog() throws IOException {
72 super(threadPool, connectionSupplier.get());
76 protected boolean isFinished() {
77 return finished.get();
81 protected Peer getResult() {
86 protected void consumePeer(Peer peer) {
92 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {