From e3f3a4323df8d53a0a59c2e4369abbcd8d60b940 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 10 Jul 2015 21:49:34 +0200 Subject: [PATCH] Add GetNode command implementation --- .../fcp/quelaton/DefaultFcpClient.java | 5 ++ .../net/pterodactylus/fcp/quelaton/FcpClient.java | 1 + .../pterodactylus/fcp/quelaton/GetNodeCommand.java | 12 +++++ .../fcp/quelaton/GetNodeCommandImpl.java | 60 ++++++++++++++++++++++ .../fcp/quelaton/DefaultFcpClientTest.java | 29 +++++++++++ 5 files changed, 107 insertions(+) create mode 100644 src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommand.java create mode 100644 src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java index 0693e1c..8d2afc0 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java @@ -50,6 +50,11 @@ public class DefaultFcpClient implements FcpClient { } @Override + public GetNodeCommand getNode() { + return new GetNodeCommandImpl(threadPool, this::connect); + } + + @Override public GenerateKeypairCommand generateKeypair() { return new GenerateKeypairCommandImpl(threadPool, this::connect); } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java index ad4501d..5cbec0f 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java @@ -7,6 +7,7 @@ package net.pterodactylus.fcp.quelaton; */ public interface FcpClient { + GetNodeCommand getNode(); GenerateKeypairCommand generateKeypair(); ClientGetCommand clientGet(); ClientPutCommand clientPut(); diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommand.java new file mode 100644 index 0000000..9d8a1a9 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommand.java @@ -0,0 +1,12 @@ +package net.pterodactylus.fcp.quelaton; + +import net.pterodactylus.fcp.NodeData; + +/** + * Returns information about the FCP server itself. + * + * @author David ‘Bombe’ Roden + */ +public interface GetNodeCommand extends Executable { + +} diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java new file mode 100644 index 0000000..7efffc0 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java @@ -0,0 +1,60 @@ +package net.pterodactylus.fcp.quelaton; + +import java.io.IOException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.atomic.AtomicReference; + +import net.pterodactylus.fcp.GetNode; +import net.pterodactylus.fcp.NodeData; + +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; + +/** + * Default {@link GetNodeCommandImpl} implementation based on {@link FcpReplySequence}. + * + * @author David ‘Bombe’ Roden + */ +public class GetNodeCommandImpl implements GetNodeCommand { + + private final ListeningExecutorService threadPool; + private final ConnectionSupplier connectionSupplier; + + public GetNodeCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) { + this.threadPool = MoreExecutors.listeningDecorator(threadPool); + this.connectionSupplier = connectionSupplier; + } + + @Override + public ListenableFuture execute() { + GetNode getNode = new GetNode(new RandomIdentifierGenerator().generate(), false, false, false); + return threadPool.submit(() -> new GetNodeReplySequence().send(getNode).get()); + } + + private class GetNodeReplySequence extends FcpReplySequence { + + private final AtomicReference nodeData = new AtomicReference<>(); + + public GetNodeReplySequence() throws IOException { + super(threadPool, connectionSupplier.get()); + } + + @Override + protected boolean isFinished() { + return nodeData.get() != null; + } + + @Override + protected NodeData getResult() { + return nodeData.get(); + } + + @Override + protected void consumeNodeData(NodeData nodeData) { + this.nodeData.set(nodeData); + } + + } + +} diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index 3c621ee..76e04fd 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -4,6 +4,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; import java.io.ByteArrayInputStream; import java.io.File; @@ -20,6 +21,7 @@ import java.util.stream.Collectors; import net.pterodactylus.fcp.FcpKeyPair; import net.pterodactylus.fcp.Key; +import net.pterodactylus.fcp.NodeData; import net.pterodactylus.fcp.Peer; import net.pterodactylus.fcp.Priority; import net.pterodactylus.fcp.fake.FakeTcpServer; @@ -747,4 +749,31 @@ public class DefaultFcpClientTest { containsInAnyOrder("bar1", "bar2")); } + @Test + public void defaultFcpClientCanGetNodeInformation() throws InterruptedException, ExecutionException, IOException { + Future nodeData = fcpClient.getNode().execute(); + connectNode(); + List lines = fcpServer.collectUntil(is("EndMessage")); + String identifier = extractIdentifier(lines); + assertThat(lines, matchesFcpMessage( + "GetNode", + "Identifier=" + identifier, + "GiveOpennetRef=false", + "WithPrivate=false", + "WithVolatile=false", + "EndMessage" + )); + fcpServer.writeLine( + "NodeData", + "Identifier=" + identifier, + "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(nodeData.get(), notNullValue()); + } + } -- 2.7.4