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.atomic.AtomicBoolean;
7 import java.util.concurrent.atomic.AtomicReference;
9 import net.pterodactylus.fcp.ListPeer;
10 import net.pterodactylus.fcp.Peer;
11 import net.pterodactylus.fcp.UnknownNodeIdentifier;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
17 * Default {@link ListPeerCommand} implementation based on {@link FcpReplySequence}.
19 * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
21 public class ListPeerCommandImpl implements ListPeerCommand {
23 private final ListeningExecutorService threadPool;
24 private final ConnectionSupplier connectionSupplier;
25 private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
27 public ListPeerCommandImpl(ListeningExecutorService threadPool, ConnectionSupplier connectionSupplier) {
28 this.threadPool = threadPool;
29 this.connectionSupplier = connectionSupplier;
33 public Executable<Optional<Peer>> byName(String name) {
34 nodeIdentifier.set(name);
39 public Executable<Optional<Peer>> byIdentity(String identity) {
40 nodeIdentifier.set(identity);
45 public Executable<Optional<Peer>> byHostAndPort(String host, int port) {
46 nodeIdentifier.set(String.format("%s:%d", host, port));
50 private ListenableFuture<Optional<Peer>> execute() {
51 return threadPool.submit(this::executeSequence);
54 private Optional<Peer> executeSequence() throws IOException, ExecutionException, InterruptedException {
55 ListPeer listPeer = new ListPeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
56 try (ListPeerSequence listPeerSequence = new ListPeerSequence()) {
57 return Optional.ofNullable(listPeerSequence.send(listPeer).get());
61 private class ListPeerSequence extends FcpReplySequence<Peer> {
63 private final AtomicBoolean finished = new AtomicBoolean();
64 private final AtomicReference<Peer> peer = new AtomicReference<>();
66 public ListPeerSequence() throws IOException {
67 super(threadPool, connectionSupplier.get());
71 protected boolean isFinished() {
72 return finished.get();
76 protected Peer getResult() {
81 protected void consumePeer(Peer peer) {
87 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {