From 021111b5eb02f1071aaa306f172e833b73890011 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 11 Jul 2015 19:02:05 +0200 Subject: [PATCH] Add method to add peer from a URL --- src/main/java/net/pterodactylus/fcp/AddPeer.java | 5 ++++ .../pterodactylus/fcp/quelaton/AddPeerCommand.java | 3 +++ .../fcp/quelaton/AddPeerCommandImpl.java | 10 ++++++++ .../fcp/quelaton/DefaultFcpClientTest.java | 28 ++++++++++++++++++++++ 4 files changed, 46 insertions(+) diff --git a/src/main/java/net/pterodactylus/fcp/AddPeer.java b/src/main/java/net/pterodactylus/fcp/AddPeer.java index 0aa899e..f9e73ae 100644 --- a/src/main/java/net/pterodactylus/fcp/AddPeer.java +++ b/src/main/java/net/pterodactylus/fcp/AddPeer.java @@ -63,6 +63,11 @@ public class AddPeer extends FcpMessage { setField("URL", String.valueOf(url)); } + public AddPeer(String identifier, URL url) { + this(url); + setField("Identifier", identifier); + } + /** * Creates a new “AddPeer” request that adds the peer given by the noderef. * diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommand.java index 2434106..8b7a7bb 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommand.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommand.java @@ -1,5 +1,6 @@ package net.pterodactylus.fcp.quelaton; +import java.net.URL; import java.util.Optional; import net.pterodactylus.fcp.Peer; @@ -11,4 +12,6 @@ import net.pterodactylus.fcp.Peer; */ public interface AddPeerCommand extends WithFile>> { + Executable> fromURL(URL url); + } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java index 67aa563..fbfb314 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java @@ -2,6 +2,7 @@ package net.pterodactylus.fcp.quelaton; import java.io.File; import java.io.IOException; +import java.net.URL; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -26,6 +27,7 @@ public class AddPeerCommandImpl implements AddPeerCommand { private final ListeningExecutorService threadPool; private final ConnectionSupplier connectionSupplier; private final AtomicReference file = new AtomicReference<>(); + private final AtomicReference url = new AtomicReference<>(); public AddPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) { this.threadPool = MoreExecutors.listeningDecorator(threadPool); @@ -38,6 +40,12 @@ public class AddPeerCommandImpl implements AddPeerCommand { return this::execute; } + @Override + public Executable> fromURL(URL url) { + this.url.set(url); + return this::execute; + } + private ListenableFuture> execute() { return threadPool.submit(this::executeSequence); } @@ -46,6 +54,8 @@ public class AddPeerCommandImpl implements AddPeerCommand { AddPeer addPeer = null; if (file.get() != null) { addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), file.get().getPath()); + } else if (url.get() != null) { + addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), url.get()); } try (AddPeerSequence addPeerSequence = new AddPeerSequence()) { return addPeerSequence.send(addPeer).get(); diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index 06a8de0..574f4d7 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -9,6 +9,7 @@ import static org.hamcrest.Matchers.notNullValue; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; +import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.List; @@ -1025,4 +1026,31 @@ public class DefaultFcpClientTest { assertThat(peer.get().get().getIdentity().toString(), is("id1")); } + @Test + public void defaultFcpClientCanAddPeerFromURL() throws InterruptedException, ExecutionException, IOException { + Future> peer = fcpClient.addPeer().fromURL(new URL("http://node.ref/")).execute(); + connectNode(); + List lines = fcpServer.collectUntil(is("EndMessage")); + String identifier = extractIdentifier(lines); + assertThat(lines, matchesFcpMessage( + "AddPeer", + "Identifier=" + identifier, + "URL=http://node.ref/", + "EndMessage" + )); + fcpServer.writeLine( + "Peer", + "Identifier=" + identifier, + "identity=id1", + "opennet=false", + "ark.pubURI=SSK@3YEf.../ark", + "ark.number=78", + "auth.negTypes=2", + "version=Fred,0.7,1.0,1466", + "lastGoodVersion=Fred,0.7,1.0,1466", + "EndMessage" + ); + assertThat(peer.get().get().getIdentity().toString(), is("id1")); + } + } -- 2.7.4