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;
27 public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
28 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
29 this.connectionSupplier = connectionSupplier;
33 public Future<Collection<Peer>> execute() {
34 String identifier = new RandomIdentifierGenerator().generate();
35 ListPeers listPeers = new ListPeers(identifier);
36 return threadPool.submit(() -> new ListPeersReplySequence().send(listPeers).get());
39 private class ListPeersReplySequence extends FcpReplySequence<Collection<Peer>> {
41 private final Collection<Peer> peers = new HashSet<>();
42 private final AtomicBoolean finished = new AtomicBoolean(false);
44 public ListPeersReplySequence() throws IOException {
45 super(threadPool, connectionSupplier.get());
49 protected boolean isFinished() {
50 return finished.get();
54 protected Collection<Peer> getResult() {
59 protected void consumePeer(Peer peer) {
64 protected void consumeEndListPeers(EndListPeers endListPeers) {