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.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import com.google.common.util.concurrent.MoreExecutors;
19 * Default {@link ListPeersCommand} implementation based on {@link FcpReplySequence}.
21 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
23 public class ListPeersCommandImpl implements ListPeersCommand {
25 private final ListeningExecutorService threadPool;
26 private final ConnectionSupplier connectionSupplier;
27 private final AtomicBoolean includeMetadata = new AtomicBoolean(false);
28 private final AtomicBoolean includeVolatile = new AtomicBoolean(false);
30 public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
31 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
32 this.connectionSupplier = connectionSupplier;
36 public ListPeersCommand includeMetadata() {
37 includeMetadata.set(true);
42 public ListPeersCommand includeVolatile() {
43 includeVolatile.set(true);
48 public ListenableFuture<Collection<Peer>> execute() {
49 String identifier = new RandomIdentifierGenerator().generate();
50 ListPeers listPeers = new ListPeers(identifier, includeMetadata.get(), includeVolatile.get());
51 return threadPool.submit(() -> new ListPeersReplySequence().send(listPeers).get());
54 private class ListPeersReplySequence extends FcpReplySequence<Collection<Peer>> {
56 private final Collection<Peer> peers = new HashSet<>();
57 private final AtomicBoolean finished = new AtomicBoolean(false);
59 public ListPeersReplySequence() throws IOException {
60 super(threadPool, connectionSupplier.get());
64 protected boolean isFinished() {
65 return finished.get();
69 protected Collection<Peer> getResult() {
74 protected void consumePeer(Peer peer) {
79 protected void consumeEndListPeers(EndListPeers endListPeers) {