Use execute() to trigger execution of commands
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 10 Jul 2015 13:52:15 +0000 (15:52 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 10 Jul 2015 14:00:13 +0000 (16:00 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommand.java
src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommand.java
src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommand.java
src/main/java/net/pterodactylus/fcp/quelaton/GenerateKeypairCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommand.java
src/main/java/net/pterodactylus/fcp/quelaton/ListPeersCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/WithUri.java
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index 7e039ba..7c5bf8d 100644 (file)
@@ -21,7 +21,7 @@ public interface ClientGetCommand {
        ClientGetCommand realTime();
        ClientGetCommand global();
 
-       ListenableFuture<Optional<Data>> uri(String uri);
+       Executable<Optional<Data>> uri(String uri);
 
        interface Data {
 
index 8e24cbf..5137c96 100644 (file)
@@ -78,9 +78,9 @@ class ClientGetCommandImpl implements ClientGetCommand {
        }
 
        @Override
-       public ListenableFuture<Optional<Data>> uri(String uri) {
+       public Executable<Optional<Data>> 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) {
index 53eccef..a8a571b 100644 (file)
@@ -14,8 +14,8 @@ import net.pterodactylus.fcp.Key;
 public interface ClientPutCommand {
 
        ClientPutCommand named(String targetFilename);
-       WithUri<Optional<Key>> redirectTo(String uri);
-       WithUri<Optional<Key>> from(File file);
-       WithLength<WithUri<Optional<Key>>> from(InputStream inputStream);
+       WithUri<Executable<Optional<Key>>> redirectTo(String uri);
+       WithUri<Executable<Optional<Key>>> from(File file);
+       WithLength<WithUri<Executable<Optional<Key>>>> from(InputStream inputStream);
 
 }
index 0228cce..02d020c 100644 (file)
@@ -54,32 +54,32 @@ class ClientPutCommandImpl implements ClientPutCommand {
        }
 
        @Override
-       public WithUri<Optional<Key>> redirectTo(String uri) {
+       public WithUri<Executable<Optional<Key>>> redirectTo(String uri) {
                this.redirectUri.set(Objects.requireNonNull(uri, "uri must not be null"));
                return this::key;
        }
 
        @Override
-       public WithUri<Optional<Key>> from(File file) {
+       public WithUri<Executable<Optional<Key>>> from(File file) {
                this.file.set(Objects.requireNonNull(file, "file must not be null"));
                return this::key;
        }
 
        @Override
-       public WithLength<WithUri<Optional<Key>>> from(InputStream inputStream) {
+       public WithLength<WithUri<Executable<Optional<Key>>>> from(InputStream inputStream) {
                payload.set(Objects.requireNonNull(inputStream, "inputStream must not be null"));
                return this::length;
        }
 
-       private WithUri<Optional<Key>> length(long length) {
+       private WithUri<Executable<Optional<Key>>> length(long length) {
                this.length.set(length);
                return this::key;
        }
 
-       private ListenableFuture<Optional<Key>> key(String uri) {
+       private Executable<Optional<Key>> 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) {
index b919537..147147e 100644 (file)
@@ -9,8 +9,6 @@ import java.util.concurrent.Future;
  *
  * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
  */
-public interface GenerateKeypairCommand {
-
-       Future<FcpKeyPair> execute();
+public interface GenerateKeypairCommand extends Executable<FcpKeyPair> {
 
 }
index 6f5df33..d5f30d6 100644 (file)
@@ -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<FcpKeyPair> execute() {
+       public ListenableFuture<FcpKeyPair> execute() {
                return threadPool.submit(() -> new FcpKeyPairReplySequence().send(new GenerateSSK()).get());
        }
 
index eb366d7..579487f 100644 (file)
@@ -10,11 +10,9 @@ import net.pterodactylus.fcp.Peer;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public interface ListPeersCommand {
+public interface ListPeersCommand extends Executable<Collection<Peer>> {
 
        ListPeersCommand includeMetadata();
        ListPeersCommand includeVolatile();
 
-       Future<Collection<Peer>> execute();
-
 }
index 97f3842..c084cb5 100644 (file)
@@ -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<Collection<Peer>> execute() {
+       public ListenableFuture<Collection<Peer>> execute() {
                String identifier = new RandomIdentifierGenerator().generate();
                ListPeers listPeers = new ListPeers(identifier, includeMetadata.get(), includeVolatile.get());
                return threadPool.submit(() -> new ListPeersReplySequence().send(listPeers).get());
index 76ffeaf..0828b3e 100644 (file)
@@ -11,6 +11,6 @@ import com.google.common.util.concurrent.ListenableFuture;
  */
 public interface WithUri<R> {
 
-       ListenableFuture<R> uri(String uri);
+       R uri(String uri);
 
 }
index 9a3cd26..3c621ee 100644 (file)
@@ -121,7 +121,7 @@ public class DefaultFcpClientTest {
 
        @Test
        public void clientGetCanDownloadData() throws InterruptedException, ExecutionException, IOException {
-               Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt");
+               Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
                connectNode();
                List<String> 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<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt");
+               Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
                connectNode();
                List<String> 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<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt");
+               Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
                connectNode();
                List<String> 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<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt");
+               Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
                connectNode();
                List<String> 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<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt");
+               Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
                connectNode();
                List<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<Optional<Key>> key = fcpClient.clientPut()
                        .from(new ByteArrayInputStream("Hello\n".getBytes()))
                        .length(6)
-                       .uri("KSK@foo.txt");
+                       .uri("KSK@foo.txt")
+                       .execute();
                connectNode();
                List<String> lines = fcpServer.collectUntil(is("Hello"));
                String identifier = extractIdentifier(lines);
@@ -385,7 +387,8 @@ public class DefaultFcpClientTest {
                Future<Optional<Key>> key = fcpClient.clientPut()
                        .from(new ByteArrayInputStream("Hello\n".getBytes()))
                        .length(6)
-                       .uri("KSK@foo.txt");
+                       .uri("KSK@foo.txt")
+                       .execute();
                connectNode();
                List<String> 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<String> 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<String> 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<String> 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<String> 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<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt");
+               Future<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
                connectNode();
                List<String> 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<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt");
+               Future<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
                connectNode();
                List<String> 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<String> 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<String> 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<String> lines = fcpServer.collectUntil(is("EndMessage"));
                String identifier = extractIdentifier(lines);