From: David ‘Bombe’ Roden Date: Mon, 13 Jul 2015 17:34:26 +0000 (+0200) Subject: Add command to modify peer settings X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=60bf94d38d9c17dfba81e20564e98337714ae332;p=jFCPlib.git Add command to modify peer settings --- diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java index 09b154b..3dd0b76 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java @@ -1,12 +1,14 @@ 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.AtomicReference; import java.util.function.Supplier; import net.pterodactylus.fcp.FcpConnection; +import net.pterodactylus.fcp.Peer; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.MoreExecutors; @@ -85,6 +87,11 @@ public class DefaultFcpClient implements FcpClient { } @Override + public ModifyPeerCommand modifyPeer() { + return new ModifyPeerCommandImpl(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 6864e5d..5ae1f0c 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java @@ -15,6 +15,7 @@ public interface FcpClient { ListPeerCommand listPeer(); ListPeersCommand listPeers(); AddPeerCommand addPeer(); + ModifyPeerCommand modifyPeer(); ListPeerNotesCommand listPeerNotes(); } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommand.java new file mode 100644 index 0000000..a608fc2 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommand.java @@ -0,0 +1,17 @@ +package net.pterodactylus.fcp.quelaton; + +import java.util.Optional; + +import net.pterodactylus.fcp.Peer; + +/** + * Command that modifies certain settings of a peer. + * + * @author David ‘Bombe’ Roden + */ +public interface ModifyPeerCommand { + + ModifyPeerCommand enable(); + Executable> byName(String name); + +} diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommandImpl.java new file mode 100644 index 0000000..71fda3d --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ModifyPeerCommandImpl.java @@ -0,0 +1,91 @@ +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.ModifyPeer; +import net.pterodactylus.fcp.Peer; +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 ModifyPeerCommand} implementation based on {@link FcpDialog}. + * + * @author David ‘Bombe’ Roden + */ +public class ModifyPeerCommandImpl implements ModifyPeerCommand { + + private final ListeningExecutorService threadPool; + private final ConnectionSupplier connectionSupplier; + private final AtomicReference nodeIdentifier = new AtomicReference<>(); + private final AtomicReference enabled = new AtomicReference<>(); + + public ModifyPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) { + this.threadPool = MoreExecutors.listeningDecorator(threadPool); + this.connectionSupplier = connectionSupplier; + } + + @Override + public ModifyPeerCommand enable() { + enabled.set(true); + return this; + } + + @Override + public Executable> byName(String name) { + nodeIdentifier.set(name); + return this::execute; + } + + private ListenableFuture> execute() { + return threadPool.submit(this::executeSequence); + } + + private Optional executeSequence() throws IOException, ExecutionException, InterruptedException { + ModifyPeer modifyPeer = new ModifyPeer(new RandomIdentifierGenerator().generate(), nodeIdentifier.get()); + Optional.ofNullable(enabled.get()).ifPresent(enabled -> modifyPeer.setEnabled(enabled)); + try (ModifyPeerDialog modifyPeerDialog = new ModifyPeerDialog()) { + return modifyPeerDialog.send(modifyPeer).get(); + } + } + + private class ModifyPeerDialog extends FcpDialog> { + + private final AtomicBoolean finished = new AtomicBoolean(); + private final AtomicReference peer = new AtomicReference<>(); + + public ModifyPeerDialog() throws IOException { + super(threadPool, connectionSupplier.get()); + } + + @Override + protected boolean isFinished() { + return finished.get(); + } + + @Override + protected Optional getResult() { + return Optional.ofNullable(peer.get()); + } + + @Override + protected void consumePeer(Peer peer) { + this.peer.set(peer); + finished.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 9eef9ce..1c5b2d7 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -1242,4 +1242,27 @@ public class DefaultFcpClientTest { assertThat(peerNote.get().get().getPeerNoteType(), is(1)); } + @Test + public void defaultFcpClientCanEnablePeerByName() throws InterruptedException, ExecutionException, IOException { + Future> peer = fcpClient.modifyPeer().enable().byName("Friend1").execute(); + connectNode(); + List lines = fcpServer.collectUntil(is("EndMessage")); + String identifier = extractIdentifier(lines); + assertThat(lines, matchesFcpMessage( + "ModifyPeer", + "Identifier=" + identifier, + "NodeIdentifier=Friend1", + "IsDisabled=false", + "EndMessage" + )); + fcpServer.writeLine( + "Peer", + "Identifier=" + identifier, + "NodeIdentifier=Friend1", + "identity=id1", + "EndMessage" + ); + assertThat(peer.get().get().getIdentity(), is("id1")); + } + }