1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.concurrent.ExecutionException;
7 import java.util.concurrent.ExecutorService;
8 import java.util.concurrent.atomic.AtomicBoolean;
9 import java.util.function.Supplier;
11 import net.pterodactylus.fcp.EndListPeers;
12 import net.pterodactylus.fcp.ListPeers;
13 import net.pterodactylus.fcp.Peer;
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 ListPeersCommand} implementation based on {@link FcpDialog}.
22 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
24 public class ListPeersCommandImpl implements ListPeersCommand {
26 private final ListeningExecutorService threadPool;
27 private final ConnectionSupplier connectionSupplier;
28 private final Supplier<String> identifierGenerator;
29 private final AtomicBoolean includeMetadata = new AtomicBoolean(false);
30 private final AtomicBoolean includeVolatile = new AtomicBoolean(false);
32 public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
33 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
34 this.connectionSupplier = connectionSupplier;
35 this.identifierGenerator = identifierGenerator;
39 public ListPeersCommand includeMetadata() {
40 includeMetadata.set(true);
45 public ListPeersCommand includeVolatile() {
46 includeVolatile.set(true);
51 public ListenableFuture<Collection<Peer>> execute() {
52 return threadPool.submit(this::executeDialog);
55 private Collection<Peer> executeDialog() throws InterruptedException, ExecutionException, IOException {
56 ListPeers listPeers = new ListPeers(identifierGenerator.get(), includeMetadata.get(), includeVolatile.get());
57 try (ListPeersDialog listPeersDialog = new ListPeersDialog()) {
58 return listPeersDialog.send(listPeers).get();
62 private class ListPeersDialog extends FcpDialog<Collection<Peer>> {
64 private final Collection<Peer> peers = new HashSet<>();
65 private final AtomicBoolean finished = new AtomicBoolean(false);
67 public ListPeersDialog() throws IOException {
68 super(threadPool, connectionSupplier.get());
72 protected boolean isFinished() {
73 return finished.get();
77 protected Collection<Peer> getResult() {
82 protected void consumePeer(Peer peer) {
87 protected void consumeEndListPeers(EndListPeers endListPeers) {