From 8ff9728f5a6fd4dd1f541c0a8caa7f8fc7401b76 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 15 Oct 2015 22:07:08 +0200 Subject: [PATCH] Add basic ClientPutDiskDir command implementation --- .../fcp/quelaton/ClientPutDiskDirCommand.java | 23 ++++++++ .../fcp/quelaton/ClientPutDiskDirCommandImpl.java | 69 ++++++++++++++++++++++ .../fcp/quelaton/DefaultFcpClient.java | 5 ++ .../net/pterodactylus/fcp/quelaton/FcpClient.java | 1 + .../fcp/quelaton/DefaultFcpClientTest.java | 36 +++++++++++ 5 files changed, 134 insertions(+) create mode 100644 src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommand.java create mode 100644 src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommandImpl.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 index 0000000..e3c5344 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommand.java @@ -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 David ‘Bombe’ Roden + */ +public interface ClientPutDiskDirCommand { + + WithUri fromDirectory(File directory); + + interface WithUri { + + Executable> 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 index 0000000..861522c --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommandImpl.java @@ -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 David ‘Bombe’ Roden + */ +public class ClientPutDiskDirCommandImpl implements ClientPutDiskDirCommand { + + private final ListeningExecutorService threadPool; + private final ConnectionSupplier connectionSupplier; + private final Supplier identifierGenerator; + private final AtomicReference directory = new AtomicReference<>(); + private final AtomicReference uri = new AtomicReference<>(); + + public ClientPutDiskDirCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier 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> uri(String uri) { + this.uri.set(Objects.requireNonNull(uri)); + return () -> threadPool.submit(this::execute); + } + + private Optional 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> { + + public ClientPutDiskDirDialog() throws IOException { + super(threadPool, connectionSupplier.get(), Optional.empty()); + } + + @Override + protected void consumePutSuccessful(PutSuccessful putSuccessful) { + setResult(Optional.of(new Key(putSuccessful.getURI()))); + } + + } + +} diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java index dc6d950..86a7d7f 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java @@ -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); } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java index 3a1043a..aa287df 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java @@ -14,6 +14,7 @@ public interface FcpClient { GenerateKeypairCommand generateKeypair(); ClientGetCommand clientGet(); ClientPutCommand clientPut(); + ClientPutDiskDirCommand clientPutDiskDir(); ListPeerCommand listPeer(); ListPeersCommand listPeers(); diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index b21e15e..0adb977 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -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> 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 { -- 2.7.4