From: David ‘Bombe’ Roden Date: Fri, 10 Jul 2015 13:52:15 +0000 (+0200) Subject: Use execute() to trigger execution of commands X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=bbe77c263f9f37b2407a6b5ce2f5e68e11bd9e7c;p=jFCPlib.git Use execute() to trigger execution of commands --- diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommand.java index 7e039ba..7c5bf8d 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommand.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommand.java @@ -21,7 +21,7 @@ public interface ClientGetCommand { ClientGetCommand realTime(); ClientGetCommand global(); - ListenableFuture> uri(String uri); + Executable> uri(String uri); interface Data { diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java index 8e24cbf..5137c96 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java @@ -78,9 +78,9 @@ class ClientGetCommandImpl implements ClientGetCommand { } @Override - public ListenableFuture> uri(String uri) { + public Executable> uri(String uri) { ClientGet clientGet = createClientGetCommand(uri); - return threadPool.submit(() -> new ClientGetReplySequence().send(clientGet).get()); + return () -> threadPool.submit(() -> new ClientGetReplySequence().send(clientGet).get()); } private ClientGet createClientGetCommand(String uri) { diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommand.java index 53eccef..a8a571b 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommand.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommand.java @@ -14,8 +14,8 @@ import net.pterodactylus.fcp.Key; public interface ClientPutCommand { ClientPutCommand named(String targetFilename); - WithUri> redirectTo(String uri); - WithUri> from(File file); - WithLength>> from(InputStream inputStream); + WithUri>> redirectTo(String uri); + WithUri>> from(File file); + WithLength>>> from(InputStream inputStream); } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java index 0228cce..02d020c 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java @@ -54,32 +54,32 @@ class ClientPutCommandImpl implements ClientPutCommand { } @Override - public WithUri> redirectTo(String uri) { + public WithUri>> redirectTo(String uri) { this.redirectUri.set(Objects.requireNonNull(uri, "uri must not be null")); return this::key; } @Override - public WithUri> from(File file) { + public WithUri>> from(File file) { this.file.set(Objects.requireNonNull(file, "file must not be null")); return this::key; } @Override - public WithLength>> from(InputStream inputStream) { + public WithLength>>> from(InputStream inputStream) { payload.set(Objects.requireNonNull(inputStream, "inputStream must not be null")); return this::length; } - private WithUri> length(long length) { + private WithUri>> length(long length) { this.length.set(length); return this::key; } - private ListenableFuture> key(String uri) { + private Executable> key(String uri) { String identifier = new RandomIdentifierGenerator().generate(); ClientPut clientPut = createClientPutCommand(uri, identifier); - return threadPool.submit(() -> new ClientPutReplySequence().send(clientPut).get()); + return () -> threadPool.submit(() -> new ClientPutReplySequence().send(clientPut).get()); } private ClientPut createClientPutCommand(String uri, String identifier) { diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommand.java index b919537..147147e 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommand.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommand.java @@ -9,8 +9,6 @@ import java.util.concurrent.Future; * * @author David ‘Bombe’ Roden */ -public interface GenerateKeypairCommand { - - Future execute(); +public interface GenerateKeypairCommand extends Executable { } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java index 6f5df33..d5f30d6 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java @@ -9,6 +9,10 @@ import net.pterodactylus.fcp.FcpKeyPair; import net.pterodactylus.fcp.GenerateSSK; import net.pterodactylus.fcp.SSKKeypair; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; + /** * Implementation of the {@link GenerateKeypairCommand}. * @@ -16,16 +20,16 @@ import net.pterodactylus.fcp.SSKKeypair; */ class GenerateKeypairCommandImpl implements GenerateKeypairCommand { - private final ExecutorService threadPool; + private final ListeningExecutorService threadPool; private final ConnectionSupplier connectionSupplier; GenerateKeypairCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) { - this.threadPool = threadPool; + this.threadPool = MoreExecutors.listeningDecorator(threadPool); this.connectionSupplier = connectionSupplier; } @Override - public Future execute() { + public ListenableFuture execute() { return threadPool.submit(() -> new FcpKeyPairReplySequence().send(new GenerateSSK()).get()); } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommand.java index eb366d7..579487f 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommand.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommand.java @@ -10,11 +10,9 @@ import net.pterodactylus.fcp.Peer; * * @author David ‘Bombe’ Roden */ -public interface ListPeersCommand { +public interface ListPeersCommand extends Executable> { ListPeersCommand includeMetadata(); ListPeersCommand includeVolatile(); - Future> execute(); - } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java index 97f3842..c084cb5 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java @@ -11,6 +11,7 @@ import net.pterodactylus.fcp.EndListPeers; import net.pterodactylus.fcp.ListPeers; import net.pterodactylus.fcp.Peer; +import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.MoreExecutors; @@ -44,7 +45,7 @@ public class ListPeersCommandImpl implements ListPeersCommand { } @Override - public Future> execute() { + public ListenableFuture> execute() { String identifier = new RandomIdentifierGenerator().generate(); ListPeers listPeers = new ListPeers(identifier, includeMetadata.get(), includeVolatile.get()); return threadPool.submit(() -> new ListPeersReplySequence().send(listPeers).get()); diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/WithUri.java b/src/main/java/net/pterodactylus/fcp/quelaton/WithUri.java index 76ffeaf..0828b3e 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/WithUri.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/WithUri.java @@ -11,6 +11,6 @@ import com.google.common.util.concurrent.ListenableFuture; */ public interface WithUri { - ListenableFuture uri(String uri); + R uri(String uri); } diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index 9a3cd26..3c621ee 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -121,7 +121,7 @@ public class DefaultFcpClientTest { @Test public void clientGetCanDownloadData() throws InterruptedException, ExecutionException, IOException { - Future> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt"); + Future> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); assertThat(lines, matchesFcpMessage("ClientGet", "ReturnType=direct", "URI=KSK@foo.txt")); @@ -154,7 +154,7 @@ public class DefaultFcpClientTest { @Test public void clientGetDownloadsDataForCorrectIdentifier() throws InterruptedException, ExecutionException, IOException { - Future> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt"); + Future> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt")); @@ -188,7 +188,7 @@ public class DefaultFcpClientTest { @Test public void clientGetRecognizesGetFailed() throws InterruptedException, ExecutionException, IOException { - Future> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt"); + Future> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt")); @@ -206,7 +206,7 @@ public class DefaultFcpClientTest { @Test public void clientGetRecognizesGetFailedForCorrectIdentifier() throws InterruptedException, ExecutionException, IOException { - Future> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt"); + Future> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt")); @@ -229,7 +229,7 @@ public class DefaultFcpClientTest { @Test(expected = ExecutionException.class) public void clientGetRecognizesConnectionClosed() throws InterruptedException, ExecutionException, IOException { - Future> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt"); + Future> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt")); @@ -267,7 +267,7 @@ public class DefaultFcpClientTest { @Test public void clientGetWithIgnoreDataStoreSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException { - fcpClient.clientGet().ignoreDataStore().uri("KSK@foo.txt"); + fcpClient.clientGet().ignoreDataStore().uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "IgnoreDS=true")); @@ -276,7 +276,7 @@ public class DefaultFcpClientTest { @Test public void clientGetWithDataStoreOnlySettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException { - fcpClient.clientGet().dataStoreOnly().uri("KSK@foo.txt"); + fcpClient.clientGet().dataStoreOnly().uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "DSonly=true")); @@ -285,7 +285,7 @@ public class DefaultFcpClientTest { @Test public void clientGetWithMaxSizeSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException { - fcpClient.clientGet().maxSize(1048576).uri("KSK@foo.txt"); + fcpClient.clientGet().maxSize(1048576).uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "MaxSize=1048576")); @@ -294,7 +294,7 @@ public class DefaultFcpClientTest { @Test public void clientGetWithPrioritySettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException { - fcpClient.clientGet().priority(Priority.interactive).uri("KSK@foo.txt"); + fcpClient.clientGet().priority(Priority.interactive).uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "PriorityClass=1")); @@ -303,7 +303,7 @@ public class DefaultFcpClientTest { @Test public void clientGetWithRealTimeSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException { - fcpClient.clientGet().realTime().uri("KSK@foo.txt"); + fcpClient.clientGet().realTime().uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "RealTimeFlag=true")); @@ -312,7 +312,7 @@ public class DefaultFcpClientTest { @Test public void clientGetWithGlobalSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException { - fcpClient.clientGet().global().uri("KSK@foo.txt"); + fcpClient.clientGet().global().uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "Global=true")); @@ -349,7 +349,8 @@ public class DefaultFcpClientTest { fcpClient.clientPut() .from(new ByteArrayInputStream("Hello\n".getBytes())) .length(6) - .uri("KSK@foo.txt"); + .uri("KSK@foo.txt") + .execute(); connectNode(); List lines = fcpServer.collectUntil(is("Hello")); assertThat(lines, matchesFcpMessage("ClientPut", "UploadFrom=direct", "DataLength=6", "URI=KSK@foo.txt")); @@ -361,7 +362,8 @@ public class DefaultFcpClientTest { Future> key = fcpClient.clientPut() .from(new ByteArrayInputStream("Hello\n".getBytes())) .length(6) - .uri("KSK@foo.txt"); + .uri("KSK@foo.txt") + .execute(); connectNode(); List lines = fcpServer.collectUntil(is("Hello")); String identifier = extractIdentifier(lines); @@ -385,7 +387,8 @@ public class DefaultFcpClientTest { Future> key = fcpClient.clientPut() .from(new ByteArrayInputStream("Hello\n".getBytes())) .length(6) - .uri("KSK@foo.txt"); + .uri("KSK@foo.txt") + .execute(); connectNode(); List lines = fcpServer.collectUntil(is("Hello")); String identifier = extractIdentifier(lines); @@ -410,7 +413,8 @@ public class DefaultFcpClientTest { .named("otherName.txt") .from(new ByteArrayInputStream("Hello\n".getBytes())) .length(6) - .uri("KSK@foo.txt"); + .uri("KSK@foo.txt") + .execute(); connectNode(); List lines = fcpServer.collectUntil(is("Hello")); assertThat(lines, matchesFcpMessage("ClientPut", "TargetFilename=otherName.txt", "UploadFrom=direct", @@ -420,7 +424,7 @@ public class DefaultFcpClientTest { @Test public void clientPutWithRedirectSendsCorrectCommand() throws IOException, ExecutionException, InterruptedException { - fcpClient.clientPut().redirectTo("KSK@bar.txt").uri("KSK@foo.txt"); + fcpClient.clientPut().redirectTo("KSK@bar.txt").uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); assertThat(lines, @@ -429,7 +433,7 @@ public class DefaultFcpClientTest { @Test public void clientPutWithFileSendsCorrectCommand() throws InterruptedException, ExecutionException, IOException { - fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt"); + fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); assertThat(lines, @@ -440,7 +444,7 @@ public class DefaultFcpClientTest { public void clientPutWithFileCanCompleteTestDdaSequence() throws IOException, ExecutionException, InterruptedException { File tempFile = createTempFile(); - fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt"); + fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); String identifier = extractIdentifier(lines); @@ -493,7 +497,7 @@ public class DefaultFcpClientTest { @Test public void clientPutDoesNotReactToProtocolErrorForDifferentIdentifier() throws InterruptedException, ExecutionException, IOException { - Future> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt"); + Future> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); String identifier = extractIdentifier(lines); @@ -515,7 +519,7 @@ public class DefaultFcpClientTest { @Test public void clientPutAbortsOnProtocolErrorOtherThan25() throws InterruptedException, ExecutionException, IOException { - Future> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt"); + Future> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); String identifier = extractIdentifier(lines); @@ -532,7 +536,7 @@ public class DefaultFcpClientTest { public void clientPutDoesNotReplyToWrongTestDdaReply() throws IOException, ExecutionException, InterruptedException { File tempFile = createTempFile(); - fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt"); + fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); String identifier = extractIdentifier(lines); @@ -575,7 +579,7 @@ public class DefaultFcpClientTest { public void clientPutSendsResponseEvenIfFileCanNotBeRead() throws IOException, ExecutionException, InterruptedException { File tempFile = createTempFile(); - fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt"); + fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); String identifier = extractIdentifier(lines); @@ -612,7 +616,7 @@ public class DefaultFcpClientTest { public void clientPutDoesNotResendOriginalClientPutOnTestDDACompleteWithWrongDirectory() throws IOException, ExecutionException, InterruptedException { File tempFile = createTempFile(); - fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt"); + fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute(); connectNode(); List lines = fcpServer.collectUntil(is("EndMessage")); String identifier = extractIdentifier(lines);