From 546860eb61ae85e546582747addb21e93ad33468 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 6 Sep 2024 14:57:03 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Deprecate=20and=20replace=20modifyPe?= =?utf8?q?er(),=20add=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/fcp/highlevel/FcpClient.java | 30 +++++++++++++++++++++- .../pterodactylus/fcp/highlevel/FcpClientTest.java | 16 ++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java index 1dd6e9f..1144ab3 100644 --- a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java @@ -76,6 +76,7 @@ import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; import static com.google.common.collect.FluentIterable.from; import static java.util.stream.Collectors.toList; @@ -660,8 +661,35 @@ public class FcpClient implements Closeable { * if an I/O error occurs * @throws FcpException * if an FCP error occurs + * @deprecated Use {@link #modifyPeer(Peer, Consumer)} */ + @Deprecated public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException { + modifyPeer(peer, modifyPeer -> { + if (allowLocalAddresses != null) { + modifyPeer.setAllowLocalAddresses(allowLocalAddresses); + } + if (disabled != null) { + modifyPeer.setEnabled(!disabled); + } + if (listenOnly != null) { + modifyPeer.setListenOnly(listenOnly); + } + }); + } + + /** + * Modifies the given peer. + * + * @param peer The peer to modify + * @param modifyPeerConsumer A lambda that modifies a {@link ModifyPeer} + * object to change the peer’s configuration + * @throws IOException if an I/O error occurs + * @throws FcpException if an FCP error occurs + */ + public void modifyPeer(Peer peer, Consumer modifyPeerConsumer) throws IOException, FcpException { + ModifyPeer modifyPeer = new ModifyPeer(createIdentifier("modify-peer"), peer.getIdentifier()); + modifyPeerConsumer.accept(modifyPeer); new ExtendedFcpAdapter() { /** @@ -670,7 +698,7 @@ public class FcpClient implements Closeable { @Override @SuppressWarnings("synthetic-access") public void run() throws IOException { - sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly)); + sendMessage(modifyPeer); } /** diff --git a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java index e455233..e86bb85 100644 --- a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java @@ -453,6 +453,22 @@ public class FcpClientTest { } @Test + public void modifyPeerSendsCorrectMessage() throws Exception { + List sentMessages = new ArrayList<>(); + FcpConnection fcpConnection = createFcpConnection(message -> { + if (message.getName().equals("ModifyPeer")) { + sentMessages.add(message); + return (listener, connection) -> listener.receivedPeer(connection, createPeer()); + } + return FcpClientTest::doNothing; + }); + try (FcpClient fcpClient = new FcpClient(fcpConnection)) { + fcpClient.modifyPeer(createPeer(), modifyPeer -> modifyPeer.setAllowLocalAddresses(true)); + assertThat(sentMessages, contains(hasField("AllowLocalAddresses", equalTo("true")))); + } + } + + @Test public void generatingKeyPairSendsCorrectMessage() throws IOException, FcpException { FcpConnection fcpConnection = createFcpConnection(message -> { if (message.getName().equals("GenerateSSK")) { -- 2.7.4