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.ExecutorService;
7 import java.util.concurrent.Future;
8 import java.util.concurrent.atomic.AtomicBoolean;
10 import net.pterodactylus.fcp.EndListPeers;
11 import net.pterodactylus.fcp.ListPeers;
12 import net.pterodactylus.fcp.Peer;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import com.google.common.util.concurrent.MoreExecutors;
18 * Default {@link ListPeersCommand} implementation based on {@link FcpReplySequence}.
20 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
22 public class ListPeersCommandImpl implements ListPeersCommand {
24 private final ListeningExecutorService threadPool;
25 private final ConnectionSupplier connectionSupplier;
26 private final AtomicBoolean includeMetadata = new AtomicBoolean(false);
27 private final AtomicBoolean includeVolatile = new AtomicBoolean(false);
29 public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
30 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
31 this.connectionSupplier = connectionSupplier;
35 public ListPeersCommand includeMetadata() {
36 includeMetadata.set(true);
41 public ListPeersCommand includeVolatile() {
42 includeVolatile.set(true);
47 public Future<Collection<Peer>> execute() {
48 String identifier = new RandomIdentifierGenerator().generate();
49 ListPeers listPeers = new ListPeers(identifier, includeMetadata.get(), includeVolatile.get());
50 return threadPool.submit(() -> new ListPeersReplySequence().send(listPeers).get());
53 private class ListPeersReplySequence extends FcpReplySequence<Collection<Peer>> {
55 private final Collection<Peer> peers = new HashSet<>();
56 private final AtomicBoolean finished = new AtomicBoolean(false);
58 public ListPeersReplySequence() throws IOException {
59 super(threadPool, connectionSupplier.get());
63 protected boolean isFinished() {
64 return finished.get();
68 protected Collection<Peer> getResult() {
73 protected void consumePeer(Peer peer) {
78 protected void consumeEndListPeers(EndListPeers endListPeers) {