Add basic ClientPutDiskDir command implementation
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Thu, 15 Oct 2015 20:07:08 +0000 (22:07 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Thu, 15 Oct 2015 21:23:52 +0000 (23:23 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommand.java [new file with mode: 0644]
src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommandImpl.java [new file with mode: 0644]
src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java
src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommand.java
new file mode 100644 (file)
index 0000000..e3c5344
--- /dev/null
@@ -0,0 +1,23 @@
+package net.pterodactylus.fcp.quelaton;
+
+import java.io.File;
+import java.util.Optional;
+
+import net.pterodactylus.fcp.Key;
+
+/**
+ * FCP command that inserts a directory from the filesystem local to the node into Freenet.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public interface ClientPutDiskDirCommand {
+
+       WithUri fromDirectory(File directory);
+
+       interface WithUri {
+
+               Executable<Optional<Key>> uri(String uri);
+
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommandImpl.java
new file mode 100644 (file)
index 0000000..861522c
--- /dev/null
@@ -0,0 +1,69 @@
+package net.pterodactylus.fcp.quelaton;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Supplier;
+
+import net.pterodactylus.fcp.ClientPutDiskDir;
+import net.pterodactylus.fcp.Key;
+import net.pterodactylus.fcp.PutSuccessful;
+
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
+/**
+ * Default {@link ClientPutDiskDirCommand} implemented based on {@link FcpDialog}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ClientPutDiskDirCommandImpl implements ClientPutDiskDirCommand {
+
+       private final ListeningExecutorService threadPool;
+       private final ConnectionSupplier connectionSupplier;
+       private final Supplier<String> identifierGenerator;
+       private final AtomicReference<String> directory = new AtomicReference<>();
+       private final AtomicReference<String> uri = new AtomicReference<>();
+
+       public ClientPutDiskDirCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
+               this.threadPool = MoreExecutors.listeningDecorator(threadPool);
+               this.connectionSupplier = connectionSupplier;
+               this.identifierGenerator = identifierGenerator;
+       }
+
+       @Override
+       public WithUri fromDirectory(File directory) {
+               this.directory.set(Objects.requireNonNull(directory).getPath());
+               return this::uri;
+       }
+
+       public Executable<Optional<Key>> uri(String uri) {
+               this.uri.set(Objects.requireNonNull(uri));
+               return () -> threadPool.submit(this::execute);
+       }
+
+       private Optional<Key> execute() throws IOException, ExecutionException, InterruptedException {
+               ClientPutDiskDir clientPutDiskDir = new ClientPutDiskDir(uri.get(), identifierGenerator.get(), directory.get());
+               try (ClientPutDiskDirDialog clientPutDiskDirDialog = new ClientPutDiskDirDialog()) {
+                       return clientPutDiskDirDialog.send(clientPutDiskDir).get();
+               }
+       }
+
+       private class ClientPutDiskDirDialog extends FcpDialog<Optional<Key>> {
+
+               public ClientPutDiskDirDialog() throws IOException {
+                       super(threadPool, connectionSupplier.get(), Optional.<Key>empty());
+               }
+
+               @Override
+               protected void consumePutSuccessful(PutSuccessful putSuccessful) {
+                       setResult(Optional.of(new Key(putSuccessful.getURI())));
+               }
+
+       }
+
+}
index dc6d950..86a7d7f 100644 (file)
@@ -87,6 +87,11 @@ public class DefaultFcpClient implements FcpClient {
        }
 
        @Override
+       public ClientPutDiskDirCommand clientPutDiskDir() {
+               return new ClientPutDiskDirCommandImpl(threadPool, this::connect, randomIdentifierGenerator::generate);
+       }
+
+       @Override
        public ListPeerCommand listPeer() {
                return new ListPeerCommandImpl(threadPool, this::connect, randomIdentifierGenerator::generate);
        }
index 3a1043a..aa287df 100644 (file)
@@ -14,6 +14,7 @@ public interface FcpClient {
        GenerateKeypairCommand generateKeypair();
        ClientGetCommand clientGet();
        ClientPutCommand clientPut();
+       ClientPutDiskDirCommand clientPutDiskDir();
 
        ListPeerCommand listPeer();
        ListPeersCommand listPeers();
index b21e15e..0adb977 100644 (file)
@@ -55,7 +55,10 @@ import org.hamcrest.Matchers;
 import org.hamcrest.TypeSafeDiagnosingMatcher;
 import org.junit.After;
 import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 import org.junit.runner.RunWith;
 
 /**
@@ -1618,6 +1621,39 @@ public class DefaultFcpClientTest {
 
        }
 
+       public class ClientPutDiskDir {
+
+               private final TemporaryFolder folder = new TemporaryFolder();
+
+               @Before
+               public void setup() throws IOException {
+                       folder.create();
+                       Files.write("file1\n", folder.newFile("file1.txt"), StandardCharsets.UTF_8);
+                       Files.write("file2\n", folder.newFile("file2.txt"), StandardCharsets.UTF_8);
+                       File directory = folder.newFolder("dir");
+                       Files.write("file3\n", new File(directory, "file3.txt"), StandardCharsets.UTF_8);
+               }
+
+               @After
+               public void removeFolder() {
+                   folder.delete();
+               }
+
+               @Test
+               public void commandIsSentCorrectly() throws InterruptedException, ExecutionException, IOException {
+                       Future<Optional<Key>> key = fcpClient.clientPutDiskDir().fromDirectory(folder.getRoot()).uri("CHK@").execute();
+                       connectAndAssert(() -> matchesFcpMessage(
+                               "ClientPutDiskDir",
+                               "Identifier=" + identifier,
+                               "URI=CHK@",
+                               "Filename=" + folder.getRoot().getPath()
+                       ));
+                       fcpServer.writeLine("PutSuccessful", "Identifier=" + identifier, "URI=CHK@abc", "EndMessage");
+                       assertThat(key.get().get().getKey(), is("CHK@abc"));
+               }
+
+       }
+
        public class ConfigCommand {
 
                public class GetConfig {