Add GetNode command implementation
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 10 Jul 2015 19:49:34 +0000 (21:49 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 10 Jul 2015 19:56:28 +0000 (21:56 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java
src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java
src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommand.java [new file with mode: 0644]
src/main/java/net/pterodactylus/fcp/quelaton/GetNodeCommandImpl.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index 0693e1c..8d2afc0 100644 (file)
@@ -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);
        }
index ad4501d..5cbec0f 100644 (file)
@@ -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 (file)
index 0000000..9d8a1a9
--- /dev/null
@@ -0,0 +1,12 @@
+package net.pterodactylus.fcp.quelaton;
+
+import net.pterodactylus.fcp.NodeData;
+
+/**
+ * Returns information about the FCP server itself.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public interface GetNodeCommand extends Executable<NodeData> {
+
+}
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 (file)
index 0000000..7efffc0
--- /dev/null
@@ -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 <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+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<NodeData> execute() {
+               GetNode getNode = new GetNode(new RandomIdentifierGenerator().generate(), false, false, false);
+               return threadPool.submit(() -> new GetNodeReplySequence().send(getNode).get());
+       }
+
+       private class GetNodeReplySequence extends FcpReplySequence<NodeData> {
+
+               private final AtomicReference<NodeData> 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);
+               }
+
+       }
+
+}
index 3c621ee..76e04fd 100644 (file)
@@ -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> nodeData = fcpClient.getNode().execute();
+               connectNode();
+               List<String> 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());
+       }
+
 }