Add ListPeerNotes command
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 13 Jul 2015 05:16:15 +0000 (07:16 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 13 Jul 2015 05:16:15 +0000 (07:16 +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/ListPeerNotesCommand.java [new file with mode: 0644]
src/main/java/net/pterodactylus/fcp/quelaton/ListPeerNotesCommandImpl.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index 574afc6..09b154b 100644 (file)
@@ -84,5 +84,10 @@ public class DefaultFcpClient implements FcpClient {
                return new AddPeerCommandImpl(threadPool, this::connect);
        }
 
+       @Override
+       public ListPeerNotesCommand listPeerNotes() {
+               return new ListPeerNotesCommandImpl(threadPool, this::connect);
+       }
+
 }
 
index 5b61979..6864e5d 100644 (file)
@@ -15,5 +15,6 @@ public interface FcpClient {
        ListPeerCommand listPeer();
        ListPeersCommand listPeers();
        AddPeerCommand addPeer();
+       ListPeerNotesCommand listPeerNotes();
 
 }
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeerNotesCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeerNotesCommand.java
new file mode 100644 (file)
index 0000000..ebe4a4d
--- /dev/null
@@ -0,0 +1,19 @@
+package net.pterodactylus.fcp.quelaton;
+
+import java.util.Optional;
+
+import net.pterodactylus.fcp.PeerNote;
+
+/**
+ * List the peer notes of a single peer by its name (darknet only), node identifier, or host name/IP address and port
+ * number.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public interface ListPeerNotesCommand {
+
+       Executable<Optional<PeerNote>> byName(String name);
+       Executable<Optional<PeerNote>> byIdentity(String identity);
+       Executable<Optional<PeerNote>> byHostAndPort(String host, int port);
+
+}
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeerNotesCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeerNotesCommandImpl.java
new file mode 100644 (file)
index 0000000..426917a
--- /dev/null
@@ -0,0 +1,101 @@
+package net.pterodactylus.fcp.quelaton;
+
+import java.io.IOException;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+
+import net.pterodactylus.fcp.EndListPeerNotes;
+import net.pterodactylus.fcp.ListPeerNotes;
+import net.pterodactylus.fcp.PeerNote;
+import net.pterodactylus.fcp.UnknownNodeIdentifier;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
+/**
+ * Default {@link ListPeerNotesCommand} implementation based on {@link FcpDialog}.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public class ListPeerNotesCommandImpl implements ListPeerNotesCommand {
+
+       private final ListeningExecutorService threadPool;
+       private final ConnectionSupplier connectionSupplier;
+       private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
+
+       public ListPeerNotesCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+               this.threadPool = MoreExecutors.listeningDecorator(threadPool);
+               this.connectionSupplier = connectionSupplier;
+       }
+
+       @Override
+       public Executable<Optional<PeerNote>> byName(String name) {
+               nodeIdentifier.set(name);
+               return this::execute;
+       }
+
+       @Override
+       public Executable<Optional<PeerNote>> byIdentity(String identity) {
+               nodeIdentifier.set(identity);
+               return this::execute;
+       }
+
+       @Override
+       public Executable<Optional<PeerNote>> byHostAndPort(String host, int port) {
+               nodeIdentifier.set(String.format("%s:%d", host, port));
+               return this::execute;
+       }
+
+       private ListenableFuture<Optional<PeerNote>> execute() {
+               return threadPool.submit(this::executeSequence);
+       }
+
+       private Optional<PeerNote> executeSequence() throws IOException, ExecutionException, InterruptedException {
+               ListPeerNotes listPeerNotes =
+                       new ListPeerNotes(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
+               try (ListPeerNotesDialog listPeerNotesDialog = new ListPeerNotesDialog()) {
+                       return listPeerNotesDialog.send(listPeerNotes).get();
+               }
+       }
+
+       private class ListPeerNotesDialog extends FcpDialog<Optional<PeerNote>> {
+
+               private final AtomicReference<PeerNote> peerNote = new AtomicReference<>();
+               private final AtomicBoolean finished = new AtomicBoolean();
+
+               public ListPeerNotesDialog() throws IOException {
+                       super(threadPool, connectionSupplier.get());
+               }
+
+               @Override
+               protected boolean isFinished() {
+                       return finished.get();
+               }
+
+               @Override
+               protected Optional<PeerNote> getResult() {
+                       return Optional.ofNullable(peerNote.get());
+               }
+
+               @Override
+               protected void consumePeerNote(PeerNote peerNote) {
+                       this.peerNote.set(peerNote);
+               }
+
+               @Override
+               protected void consumeEndListPeerNotes(EndListPeerNotes endListPeerNotes) {
+                       finished.set(true);
+               }
+
+               @Override
+               protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
+                       finished.set(true);
+               }
+
+       }
+
+}
index a8dda59..ed89328 100644 (file)
@@ -29,6 +29,7 @@ import net.pterodactylus.fcp.Key;
 import net.pterodactylus.fcp.NodeData;
 import net.pterodactylus.fcp.NodeRef;
 import net.pterodactylus.fcp.Peer;
+import net.pterodactylus.fcp.PeerNote;
 import net.pterodactylus.fcp.Priority;
 import net.pterodactylus.fcp.fake.FakeTcpServer;
 import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data;
@@ -1133,4 +1134,112 @@ public class DefaultFcpClientTest {
                assertThat(peer.get().get().getIdentity(), is("id1"));
        }
 
+       @Test
+       public void listPeerNotesCanGetPeerNotesByNodeName() throws InterruptedException, ExecutionException, IOException {
+               Future<Optional<PeerNote>> peerNote = fcpClient.listPeerNotes().byName("Friend1").execute();
+               connectNode();
+               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+               String identifier = extractIdentifier(lines);
+               assertThat(lines, matchesFcpMessage(
+                       "ListPeerNotes",
+                       "NodeIdentifier=Friend1",
+                       "EndMessage"
+               ));
+               fcpServer.writeLine(
+                       "PeerNote",
+                       "Identifier=" + identifier,
+                       "NodeIdentifier=Friend1",
+                       "NoteText=RXhhbXBsZSBUZXh0Lg==",
+                       "PeerNoteType=1",
+                       "EndMessage"
+               );
+               fcpServer.writeLine(
+                       "EndListPeerNotes",
+                       "Identifier=" + identifier,
+                       "EndMessage"
+               );
+               assertThat(peerNote.get().get().getNoteText(), is("RXhhbXBsZSBUZXh0Lg=="));
+               assertThat(peerNote.get().get().getPeerNoteType(), is(1));
+       }
+
+       @Test
+       public void listPeerNotesReturnsEmptyOptionalWhenNodeIdenfierUnknown()
+       throws InterruptedException, ExecutionException,
+       IOException {
+               Future<Optional<PeerNote>> peerNote = fcpClient.listPeerNotes().byName("Friend1").execute();
+               connectNode();
+               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+               String identifier = extractIdentifier(lines);
+               assertThat(lines, matchesFcpMessage(
+                       "ListPeerNotes",
+                       "NodeIdentifier=Friend1",
+                       "EndMessage"
+               ));
+               fcpServer.writeLine(
+                       "UnknownNodeIdentifier",
+                       "Identifier=" + identifier,
+                       "NodeIdentifier=Friend1",
+                       "EndMessage"
+               );
+               assertThat(peerNote.get().isPresent(), is(false));
+       }
+
+       @Test
+       public void listPeerNotesCanGetPeerNotesByNodeIdentifier()
+       throws InterruptedException, ExecutionException, IOException {
+               Future<Optional<PeerNote>> peerNote = fcpClient.listPeerNotes().byIdentity("id1").execute();
+               connectNode();
+               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+               String identifier = extractIdentifier(lines);
+               assertThat(lines, matchesFcpMessage(
+                       "ListPeerNotes",
+                       "NodeIdentifier=id1",
+                       "EndMessage"
+               ));
+               fcpServer.writeLine(
+                       "PeerNote",
+                       "Identifier=" + identifier,
+                       "NodeIdentifier=id1",
+                       "NoteText=RXhhbXBsZSBUZXh0Lg==",
+                       "PeerNoteType=1",
+                       "EndMessage"
+               );
+               fcpServer.writeLine(
+                       "EndListPeerNotes",
+                       "Identifier=" + identifier,
+                       "EndMessage"
+               );
+               assertThat(peerNote.get().get().getNoteText(), is("RXhhbXBsZSBUZXh0Lg=="));
+               assertThat(peerNote.get().get().getPeerNoteType(), is(1));
+       }
+
+       @Test
+       public void listPeerNotesCanGetPeerNotesByHostNameAndPortNumber()
+       throws InterruptedException, ExecutionException, IOException {
+               Future<Optional<PeerNote>> peerNote = fcpClient.listPeerNotes().byHostAndPort("1.2.3.4", 5678).execute();
+               connectNode();
+               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+               String identifier = extractIdentifier(lines);
+               assertThat(lines, matchesFcpMessage(
+                       "ListPeerNotes",
+                       "NodeIdentifier=1.2.3.4:5678",
+                       "EndMessage"
+               ));
+               fcpServer.writeLine(
+                       "PeerNote",
+                       "Identifier=" + identifier,
+                       "NodeIdentifier=id1",
+                       "NoteText=RXhhbXBsZSBUZXh0Lg==",
+                       "PeerNoteType=1",
+                       "EndMessage"
+               );
+               fcpServer.writeLine(
+                       "EndListPeerNotes",
+                       "Identifier=" + identifier,
+                       "EndMessage"
+               );
+               assertThat(peerNote.get().get().getNoteText(), is("RXhhbXBsZSBUZXh0Lg=="));
+               assertThat(peerNote.get().get().getPeerNoteType(), is(1));
+       }
+
 }