Add command that removes a peer from the node by name
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 13 Jul 2015 19:00:48 +0000 (21:00 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 13 Jul 2015 19:00:48 +0000 (21:00 +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/RemovePeerCommand.java [new file with mode: 0644]
src/main/java/net/pterodactylus/fcp/quelaton/RemovePeerCommandImpl.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index 3dd0b76..0d180cb 100644 (file)
@@ -92,6 +92,11 @@ public class DefaultFcpClient implements FcpClient {
        }
 
        @Override
+       public RemovePeerCommand removePeer() {
+               return new RemovePeerCommandImpl(threadPool, this::connect);
+       }
+
+       @Override
        public ListPeerNotesCommand listPeerNotes() {
                return new ListPeerNotesCommandImpl(threadPool, this::connect);
        }
index 5ae1f0c..c2333da 100644 (file)
@@ -16,6 +16,8 @@ public interface FcpClient {
        ListPeersCommand listPeers();
        AddPeerCommand addPeer();
        ModifyPeerCommand modifyPeer();
+       RemovePeerCommand removePeer();
+
        ListPeerNotesCommand listPeerNotes();
 
 }
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/RemovePeerCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/RemovePeerCommand.java
new file mode 100644 (file)
index 0000000..02b0d44
--- /dev/null
@@ -0,0 +1,12 @@
+package net.pterodactylus.fcp.quelaton;
+
+/**
+ * Command that removes a peer from the node.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public interface RemovePeerCommand {
+
+       Executable<Boolean> byName(String name);
+
+}
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/RemovePeerCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/RemovePeerCommandImpl.java
new file mode 100644 (file)
index 0000000..9578477
--- /dev/null
@@ -0,0 +1,81 @@
+package net.pterodactylus.fcp.quelaton;
+
+import java.io.IOException;
+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.PeerRemoved;
+import net.pterodactylus.fcp.RemovePeer;
+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 RemovePeerCommand} implementation based on {@link FcpDialog}.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public class RemovePeerCommandImpl implements RemovePeerCommand {
+
+       private final ListeningExecutorService threadPool;
+       private final ConnectionSupplier connectionSupplier;
+       private final AtomicReference<String> nodeIdentifier = new AtomicReference<>();
+
+       public RemovePeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+               this.threadPool = MoreExecutors.listeningDecorator(threadPool);
+               this.connectionSupplier = connectionSupplier;
+       }
+
+       @Override
+       public Executable<Boolean> byName(String name) {
+               nodeIdentifier.set(name);
+               return this::execute;
+       }
+
+       private ListenableFuture<Boolean> execute() {
+               return threadPool.submit(this::executeDialog);
+       }
+
+       private boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
+               RemovePeer removePeer = new RemovePeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get());
+               try (RemovePeerDialog removePeerDialog = new RemovePeerDialog()) {
+                       return removePeerDialog.send(removePeer).get();
+               }
+       }
+
+       private class RemovePeerDialog extends FcpDialog<Boolean> {
+
+               private final AtomicBoolean finished = new AtomicBoolean();
+               private final AtomicBoolean removed = new AtomicBoolean();
+
+               public RemovePeerDialog() throws IOException {
+                       super(threadPool, connectionSupplier.get());
+               }
+
+               @Override
+               protected boolean isFinished() {
+                       return finished.get() || removed.get();
+               }
+
+               @Override
+               protected Boolean getResult() {
+                       return removed.get();
+               }
+
+               @Override
+               protected void consumePeerRemoved(PeerRemoved peerRemoved) {
+                       removed.set(true);
+               }
+
+               @Override
+               protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
+                       finished.set(true);
+               }
+
+       }
+
+}
index 5979284..c1d09ea 100644 (file)
@@ -1572,4 +1572,25 @@ public class DefaultFcpClientTest {
                assertThat(peer.get().get().getIdentity(), is("id1"));
        }
 
+       @Test
+       public void defaultFcpClientCanRemovePeerByName() throws InterruptedException, ExecutionException, IOException {
+               Future<Boolean> peer = fcpClient.removePeer().byName("Friend1").execute();
+               connectNode();
+               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+               String identifier = extractIdentifier(lines);
+               assertThat(lines, matchesFcpMessage(
+                       "RemovePeer",
+                       "Identifier=" + identifier,
+                       "NodeIdentifier=Friend1",
+                       "EndMessage"
+               ));
+               fcpServer.writeLine(
+                       "PeerRemoved",
+                       "Identifier=" + identifier,
+                       "NodeIdentifier=Friend1",
+                       "EndMessage"
+               );
+               assertThat(peer.get(), is(true));
+       }
+
 }