return new ClientPutCommandImpl(threadPool, this::connect);
}
+ @Override
+ public ListPeersCommand listPeers() {
+ return new ListPeersCommandImpl(threadPool, this::connect);
+ }
+
private class ClientHelloReplySequence extends FcpReplySequence<Void> {
private final AtomicReference<NodeHello> receivedNodeHello;
ClientGetCommand clientGet();
ClientPutCommand clientPut();
+ ListPeersCommand listPeers();
+
}
--- /dev/null
+package net.pterodactylus.fcp.quelaton;
+
+import java.util.Collection;
+import java.util.concurrent.Future;
+
+import net.pterodactylus.fcp.Peer;
+
+/**
+ * Retrieves the list of all peers from the FCP server.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public interface ListPeersCommand {
+
+ Future<Collection<Peer>> execute();
+
+}
--- /dev/null
+package net.pterodactylus.fcp.quelaton;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import net.pterodactylus.fcp.EndListPeers;
+import net.pterodactylus.fcp.ListPeers;
+import net.pterodactylus.fcp.Peer;
+
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
+/**
+ * Default {@link ListPeersCommand} implementation based on {@link FcpReplySequence}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ListPeersCommandImpl implements ListPeersCommand {
+
+ private final ListeningExecutorService threadPool;
+ private final ConnectionSupplier connectionSupplier;
+
+ public ListPeersCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+ this.threadPool = MoreExecutors.listeningDecorator(threadPool);
+ this.connectionSupplier = connectionSupplier;
+ }
+
+ @Override
+ public Future<Collection<Peer>> execute() {
+ String identifier = new RandomIdentifierGenerator().generate();
+ ListPeers listPeers = new ListPeers(identifier);
+ return threadPool.submit(() -> new ListPeersReplySequence().send(listPeers).get());
+ }
+
+ private class ListPeersReplySequence extends FcpReplySequence<Collection<Peer>> {
+
+ private final Collection<Peer> peers = new HashSet<>();
+ private final AtomicBoolean finished = new AtomicBoolean(false);
+
+ public ListPeersReplySequence() throws IOException {
+ super(threadPool, connectionSupplier.get());
+ }
+
+ @Override
+ protected boolean isFinished() {
+ return finished.get();
+ }
+
+ @Override
+ protected Collection<Peer> getResult() {
+ return peers;
+ }
+
+ @Override
+ protected void consumePeer(Peer peer) {
+ peers.add(peer);
+ }
+
+ @Override
+ protected void consumeEndListPeers(EndListPeers endListPeers) {
+ finished.set(true);
+ }
+
+ }
+
+}
package net.pterodactylus.fcp.quelaton;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
+import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
+import java.util.stream.Collectors;
import net.pterodactylus.fcp.FcpKeyPair;
import net.pterodactylus.fcp.Key;
+import net.pterodactylus.fcp.Peer;
import net.pterodactylus.fcp.Priority;
import net.pterodactylus.fcp.fake.FakeTcpServer;
import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data;
));
}
+ @Test
+ public void clientCanListPeers() throws IOException, ExecutionException, InterruptedException {
+ Future<Collection<Peer>> peers = fcpClient.listPeers().execute();
+ connectNode();
+ List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+ assertThat(lines, matchesFcpMessage(
+ "ListPeers",
+ "WithVolatile=false",
+ "WithMetadata=false",
+ "EndMessage"
+ ));
+ String identifier = extractIdentifier(lines);
+ fcpServer.writeLine(
+ "Peer",
+ "Identifier=" + identifier,
+ "identity=id1",
+ "EndMessage"
+ );
+ fcpServer.writeLine(
+ "Peer",
+ "Identifier=" + identifier,
+ "identity=id2",
+ "EndMessage"
+ );
+ fcpServer.writeLine(
+ "EndListPeers",
+ "Identifier=" + identifier,
+ "EndMessage"
+ );
+ assertThat(peers.get(), hasSize(2));
+ assertThat(peers.get().stream().map(Peer::getIdentity).collect(Collectors.toList()),
+ containsInAnyOrder("id1", "id2"));
+ }
+
}