Add “with metadata” and “with volatile” flags to ListPeer command
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / ListPeersCommandImpl.java
1 package net.pterodactylus.fcp.quelaton;
2
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;
9
10 import net.pterodactylus.fcp.EndListPeers;
11 import net.pterodactylus.fcp.ListPeers;
12 import net.pterodactylus.fcp.Peer;
13
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import com.google.common.util.concurrent.MoreExecutors;
16
17 /**
18  * Default {@link ListPeersCommand} implementation based on {@link FcpReplySequence}.
19  *
20  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
21  */
22 public class ListPeersCommandImpl implements ListPeersCommand {
23
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);
28
29         public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
30                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
31                 this.connectionSupplier = connectionSupplier;
32         }
33
34         @Override
35         public ListPeersCommand includeMetadata() {
36                 includeMetadata.set(true);
37                 return this;
38         }
39
40         @Override
41         public ListPeersCommand includeVolatile() {
42                 includeVolatile.set(true);
43                 return this;
44         }
45
46         @Override
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());
51         }
52
53         private class ListPeersReplySequence extends FcpReplySequence<Collection<Peer>> {
54
55                 private final Collection<Peer> peers = new HashSet<>();
56                 private final AtomicBoolean finished = new AtomicBoolean(false);
57
58                 public ListPeersReplySequence() throws IOException {
59                         super(threadPool, connectionSupplier.get());
60                 }
61
62                 @Override
63                 protected boolean isFinished() {
64                         return finished.get();
65                 }
66
67                 @Override
68                 protected Collection<Peer> getResult() {
69                         return peers;
70                 }
71
72                 @Override
73                 protected void consumePeer(Peer peer) {
74                         peers.add(peer);
75                 }
76
77                 @Override
78                 protected void consumeEndListPeers(EndListPeers endListPeers) {
79                         finished.set(true);
80                 }
81
82         }
83
84 }