From: David ‘Bombe’ Roden Date: Mon, 13 Jul 2015 19:00:48 +0000 (+0200) Subject: Add command that removes a peer from the node by name X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=3eb8cc4d8104f1f1a704e2868ea659d2e455587d;p=jFCPlib.git Add command that removes a peer from the node by name --- diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java index 3dd0b76..0d180cb 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java @@ -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); } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java index 5ae1f0c..c2333da 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java @@ -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 index 0000000..02b0d44 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/RemovePeerCommand.java @@ -0,0 +1,12 @@ +package net.pterodactylus.fcp.quelaton; + +/** + * Command that removes a peer from the node. + * + * @author David ‘Bombe’ Roden + */ +public interface RemovePeerCommand { + + Executable 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 index 0000000..9578477 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/RemovePeerCommandImpl.java @@ -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 David ‘Bombe’ Roden + */ +public class RemovePeerCommandImpl implements RemovePeerCommand { + + private final ListeningExecutorService threadPool; + private final ConnectionSupplier connectionSupplier; + private final AtomicReference nodeIdentifier = new AtomicReference<>(); + + public RemovePeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) { + this.threadPool = MoreExecutors.listeningDecorator(threadPool); + this.connectionSupplier = connectionSupplier; + } + + @Override + public Executable byName(String name) { + nodeIdentifier.set(name); + return this::execute; + } + + private ListenableFuture 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 { + + 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); + } + + } + +} diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index 5979284..c1d09ea 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -1572,4 +1572,25 @@ public class DefaultFcpClientTest { assertThat(peer.get().get().getIdentity(), is("id1")); } + @Test + public void defaultFcpClientCanRemovePeerByName() throws InterruptedException, ExecutionException, IOException { + Future peer = fcpClient.removePeer().byName("Friend1").execute(); + connectNode(); + List 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)); + } + }