Add method to add peer from a URL
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sat, 11 Jul 2015 17:02:05 +0000 (19:02 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sat, 11 Jul 2015 17:02:05 +0000 (19:02 +0200)
src/main/java/net/pterodactylus/fcp/AddPeer.java
src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommand.java
src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index 0aa899e..f9e73ae 100644 (file)
@@ -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.
         *
index 2434106..8b7a7bb 100644 (file)
@@ -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<Optional<Peer>>> {
 
+       Executable<Optional<Peer>> fromURL(URL url);
+
 }
index 67aa563..fbfb314 100644 (file)
@@ -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> file = new AtomicReference<>();
+       private final AtomicReference<URL> 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<Optional<Peer>> fromURL(URL url) {
+               this.url.set(url);
+               return this::execute;
+       }
+
        private ListenableFuture<Optional<Peer>> 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();
index 06a8de0..574f4d7 100644 (file)
@@ -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<Optional<Peer>> peer = fcpClient.addPeer().fromURL(new URL("http://node.ref/")).execute();
+               connectNode();
+               List<String> 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"));
+       }
+
 }