From: David ‘Bombe’ Roden Date: Sat, 11 Jul 2015 17:26:09 +0000 (+0200) Subject: Add method to add peer by noderef X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=3e3e6edfb7e526a93bd27d32786835993f348cd5;p=jFCPlib.git Add method to add peer by noderef --- diff --git a/src/main/java/net/pterodactylus/fcp/AddPeer.java b/src/main/java/net/pterodactylus/fcp/AddPeer.java index f9e73ae..696a017 100644 --- a/src/main/java/net/pterodactylus/fcp/AddPeer.java +++ b/src/main/java/net/pterodactylus/fcp/AddPeer.java @@ -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()); diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommand.java index 5997af6..1ae1b6f 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommand.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommand.java @@ -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> fromFile(File file); Executable> fromURL(URL url); + Executable> fromNodeRef(NodeRef nodeRef); } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java index a4f4452..5dfef6f 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/AddPeerCommandImpl.java @@ -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 = new AtomicReference<>(); private final AtomicReference url = new AtomicReference<>(); + private final AtomicReference 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> fromNodeRef(NodeRef nodeRef) { + this.nodeRef.set(nodeRef); + return this::execute; + } + private ListenableFuture> 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(); diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index 840e601..4601acd 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -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> peer = fcpClient.addPeer().fromNodeRef(nodeRef).execute(); + connectNode(); + List 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")); + } + }