Add method to add peer by noderef
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sat, 11 Jul 2015 17:26:09 +0000 (19:26 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sat, 11 Jul 2015 17:26:09 +0000 (19:26 +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 f9e73ae..696a017 100644 (file)
@@ -79,6 +79,11 @@ public class AddPeer extends FcpMessage {
                setNodeRef(nodeRef);
        }
 
+       public AddPeer(String identifier, NodeRef nodeRef) {
+               this(nodeRef);
+               setField("Identifier", identifier);
+       }
+
        //
        // PRIVATE METHODS
        //
@@ -90,7 +95,7 @@ public class AddPeer extends FcpMessage {
         *            The noderef of the peer
         */
        private void setNodeRef(NodeRef nodeRef) {
-               setField("lastGoodVersion", nodeRef.getLastGoodVersion().toString());
+               setField("lastGoodVersion", String.valueOf(nodeRef.getLastGoodVersion()));
                setField("opennet", String.valueOf(nodeRef.isOpennet()));
                setField("identity", nodeRef.getIdentity());
                setField("myName", nodeRef.getMyName());
index 5997af6..1ae1b6f 100644 (file)
@@ -4,6 +4,7 @@ import java.io.File;
 import java.net.URL;
 import java.util.Optional;
 
+import net.pterodactylus.fcp.NodeRef;
 import net.pterodactylus.fcp.Peer;
 
 /**
@@ -15,5 +16,6 @@ public interface AddPeerCommand {
 
        Executable<Optional<Peer>> fromFile(File file);
        Executable<Optional<Peer>> fromURL(URL url);
+       Executable<Optional<Peer>> fromNodeRef(NodeRef nodeRef);
 
 }
index a4f4452..5dfef6f 100644 (file)
@@ -10,6 +10,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 
 import net.pterodactylus.fcp.AddPeer;
+import net.pterodactylus.fcp.NodeRef;
 import net.pterodactylus.fcp.Peer;
 import net.pterodactylus.fcp.ProtocolError;
 
@@ -28,6 +29,7 @@ public class AddPeerCommandImpl implements AddPeerCommand {
        private final ConnectionSupplier connectionSupplier;
        private final AtomicReference<File> file = new AtomicReference<>();
        private final AtomicReference<URL> url = new AtomicReference<>();
+       private final AtomicReference<NodeRef> nodeRef = new AtomicReference<>();
 
        public AddPeerCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
                this.threadPool = MoreExecutors.listeningDecorator(threadPool);
@@ -46,6 +48,12 @@ public class AddPeerCommandImpl implements AddPeerCommand {
                return this::execute;
        }
 
+       @Override
+       public Executable<Optional<Peer>> fromNodeRef(NodeRef nodeRef) {
+               this.nodeRef.set(nodeRef);
+               return this::execute;
+       }
+
        private ListenableFuture<Optional<Peer>> execute() {
                return threadPool.submit(this::executeSequence);
        }
@@ -56,6 +64,8 @@ public class AddPeerCommandImpl implements AddPeerCommand {
                        addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), file.get().getPath());
                } else if (url.get() != null) {
                        addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), url.get());
+               } else {
+                       addPeer = new AddPeer(new RandomIdentifierGenerator().generate(), nodeRef.get());
                }
                try (AddPeerSequence addPeerSequence = new AddPeerSequence()) {
                        return addPeerSequence.send(addPeer).get();
index 840e601..4601acd 100644 (file)
@@ -20,9 +20,12 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.stream.Collectors;
 
+import net.pterodactylus.fcp.ARK;
+import net.pterodactylus.fcp.DSAGroup;
 import net.pterodactylus.fcp.FcpKeyPair;
 import net.pterodactylus.fcp.Key;
 import net.pterodactylus.fcp.NodeData;
+import net.pterodactylus.fcp.NodeRef;
 import net.pterodactylus.fcp.Peer;
 import net.pterodactylus.fcp.Priority;
 import net.pterodactylus.fcp.fake.FakeTcpServer;
@@ -1053,4 +1056,50 @@ public class DefaultFcpClientTest {
                assertThat(peer.get().get().getIdentity().toString(), is("id1"));
        }
 
+       @Test
+       public void defaultFcpClientCanAddPeerFromNodeRef() throws InterruptedException, ExecutionException, IOException {
+               NodeRef nodeRef = new NodeRef();
+               nodeRef.setIdentity("id1");
+               nodeRef.setName("name");
+               nodeRef.setARK(new ARK("public", "1"));
+               nodeRef.setDSAGroup(new DSAGroup("base", "prime", "subprime"));
+               nodeRef.setNegotiationTypes(new int[] { 3, 5 });
+               nodeRef.setPhysicalUDP("1.2.3.4:5678");
+               nodeRef.setDSAPublicKey("dsa-public");
+               nodeRef.setSignature("sig");
+               Future<Optional<Peer>> peer = fcpClient.addPeer().fromNodeRef(nodeRef).execute();
+               connectNode();
+               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+               String identifier = extractIdentifier(lines);
+               assertThat(lines, matchesFcpMessage(
+                       "AddPeer",
+                       "Identifier=" + identifier,
+                       "identity=id1",
+                       "myName=name",
+                       "ark.pubURI=public",
+                       "ark.number=1",
+                       "dsaGroup.g=base",
+                       "dsaGroup.p=prime",
+                       "dsaGroup.q=subprime",
+                       "dsaPubKey.y=dsa-public",
+                       "physical.udp=1.2.3.4:5678",
+                       "auth.negTypes=3;5",
+                       "sig=sig",
+                       "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"));
+       }
+
 }