--- /dev/null
+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())));
+ }
+
+ }
+
+}
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;
/**
}
+ 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 {