Add ListPeers command
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 10 Jul 2015 11:14:02 +0000 (13:14 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 10 Jul 2015 11:14:13 +0000 (13:14 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java
src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java
src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommand.java [new file with mode: 0644]
src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index 1f31d91..1d7c02c 100644 (file)
@@ -74,6 +74,11 @@ public class DefaultFcpClient implements FcpClient {
                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;
index 6e5ab0a..ad4501d 100644 (file)
@@ -11,4 +11,6 @@ public interface FcpClient {
        ClientGetCommand clientGet();
        ClientPutCommand clientPut();
 
+       ListPeersCommand listPeers();
+
 }
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommand.java
new file mode 100644 (file)
index 0000000..56a28c2
--- /dev/null
@@ -0,0 +1,17 @@
+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();
+
+}
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java
new file mode 100644 (file)
index 0000000..5638b98
--- /dev/null
@@ -0,0 +1,70 @@
+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);
+               }
+
+       }
+
+}
index 7a43771..4c49f9f 100644 (file)
@@ -1,21 +1,26 @@
 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;
@@ -583,4 +588,38 @@ public class DefaultFcpClientTest {
                ));
        }
 
+       @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"));
+       }
+
 }