1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.concurrent.ExecutionException;
8 import java.util.concurrent.ExecutorService;
9 import java.util.concurrent.atomic.AtomicBoolean;
10 import java.util.function.Supplier;
12 import net.pterodactylus.fcp.EndListPeers;
13 import net.pterodactylus.fcp.ListPeers;
14 import net.pterodactylus.fcp.Peer;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.ListeningExecutorService;
18 import com.google.common.util.concurrent.MoreExecutors;
21 * Default {@link ListPeersCommand} implementation based on {@link FcpDialog}.
23 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
25 public class ListPeersCommandImpl implements ListPeersCommand {
27 private final ListeningExecutorService threadPool;
28 private final ConnectionSupplier connectionSupplier;
29 private final Supplier<String> identifierGenerator;
30 private final AtomicBoolean includeMetadata = new AtomicBoolean(false);
31 private final AtomicBoolean includeVolatile = new AtomicBoolean(false);
33 public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
34 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
35 this.connectionSupplier = connectionSupplier;
36 this.identifierGenerator = identifierGenerator;
40 public ListPeersCommand includeMetadata() {
41 includeMetadata.set(true);
46 public ListPeersCommand includeVolatile() {
47 includeVolatile.set(true);
52 public ListenableFuture<Collection<Peer>> execute() {
53 return threadPool.submit(this::executeDialog);
56 private Collection<Peer> executeDialog() throws InterruptedException, ExecutionException, IOException {
57 ListPeers listPeers = new ListPeers(identifierGenerator.get(), includeMetadata.get(), includeVolatile.get());
58 try (ListPeersDialog listPeersDialog = new ListPeersDialog()) {
59 return listPeersDialog.send(listPeers).get();
63 private class ListPeersDialog extends FcpDialog<Collection<Peer>> {
65 private final Collection<Peer> peers = new HashSet<>();
67 public ListPeersDialog() throws IOException {
68 super(threadPool, connectionSupplier.get(), Collections.<Peer>emptyList());
72 protected void consumePeer(Peer peer) {
77 protected void consumeEndListPeers(EndListPeers endListPeers) {