Add event for URIGenerated messages on ClientPut command
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 12 Jul 2015 11:09:47 +0000 (13:09 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 12 Jul 2015 11:09:47 +0000 (13:09 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommand.java
src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index a8a571b..41c2377 100644 (file)
@@ -3,6 +3,7 @@ package net.pterodactylus.fcp.quelaton;
 import java.io.File;
 import java.io.InputStream;
 import java.util.Optional;
+import java.util.function.Consumer;
 
 import net.pterodactylus.fcp.Key;
 
@@ -13,6 +14,7 @@ import net.pterodactylus.fcp.Key;
  */
 public interface ClientPutCommand {
 
+       ClientPutCommand onKeyGenerated(Consumer<String> keyGenerated);
        ClientPutCommand named(String targetFilename);
        WithUri<Executable<Optional<Key>>> redirectTo(String uri);
        WithUri<Executable<Optional<Key>>> from(File file);
index 4f8ddde..1b322f3 100644 (file)
@@ -4,13 +4,16 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.file.Files;
+import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
+import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Consumer;
 
 import net.pterodactylus.fcp.ClientPut;
 import net.pterodactylus.fcp.FcpMessage;
@@ -22,6 +25,7 @@ import net.pterodactylus.fcp.TestDDAComplete;
 import net.pterodactylus.fcp.TestDDAReply;
 import net.pterodactylus.fcp.TestDDARequest;
 import net.pterodactylus.fcp.TestDDAResponse;
+import net.pterodactylus.fcp.URIGenerated;
 import net.pterodactylus.fcp.UploadFrom;
 
 import com.google.common.util.concurrent.ListenableFuture;
@@ -42,6 +46,7 @@ class ClientPutCommandImpl implements ClientPutCommand {
        private final AtomicReference<InputStream> payload = new AtomicReference<>();
        private final AtomicLong length = new AtomicLong();
        private final AtomicReference<String> targetFilename = new AtomicReference<>();
+       private final List<Consumer<String>> keyGenerateds = new CopyOnWriteArrayList<>();
 
        public ClientPutCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
                this.threadPool = MoreExecutors.listeningDecorator(threadPool);
@@ -49,6 +54,12 @@ class ClientPutCommandImpl implements ClientPutCommand {
        }
 
        @Override
+       public ClientPutCommand onKeyGenerated(Consumer<String> keyGenerated) {
+               keyGenerateds.add(keyGenerated);
+               return this;
+       }
+
+       @Override
        public ClientPutCommand named(String targetFilename) {
                this.targetFilename.set(targetFilename);
                return this;
@@ -155,6 +166,13 @@ class ClientPutCommandImpl implements ClientPutCommand {
                }
 
                @Override
+               protected void consumeURIGenerated(URIGenerated uriGenerated) {
+                       for (Consumer<String> keyGenerated : keyGenerateds) {
+                               keyGenerated.accept(uriGenerated.getURI());
+                       }
+               }
+
+               @Override
                protected void consumePutSuccessful(PutSuccessful putSuccessful) {
                        finalKey.set(new Key(putSuccessful.getURI()));
                        putFinished.set(true);
index 450345b..a8dda59 100644 (file)
@@ -1,6 +1,7 @@
 package net.pterodactylus.fcp.quelaton;
 
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.containsInAnyOrder;
 import static org.hamcrest.Matchers.hasSize;
 import static org.hamcrest.Matchers.is;
@@ -14,6 +15,7 @@ import java.nio.charset.StandardCharsets;
 import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
+import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -675,6 +677,35 @@ public class DefaultFcpClientTest {
        }
 
        @Test
+       public void clientPutSendsNotificationsForGeneratedKeys()
+       throws InterruptedException, ExecutionException, IOException {
+               List<String> generatedKeys = new CopyOnWriteArrayList<>();
+               Future<Optional<Key>> key = fcpClient.clientPut()
+                       .onKeyGenerated(generatedKeys::add)
+                       .from(new ByteArrayInputStream("Hello\n".getBytes()))
+                       .length(6)
+                       .uri("KSK@foo.txt")
+                       .execute();
+               connectNode();
+               List<String> lines = fcpServer.collectUntil(is("Hello"));
+               String identifier = extractIdentifier(lines);
+               fcpServer.writeLine(
+                       "URIGenerated",
+                       "Identifier="+identifier,
+                       "URI=KSK@foo.txt",
+                       "EndMessage"
+               );
+               fcpServer.writeLine(
+                       "PutSuccessful",
+                       "URI=KSK@foo.txt",
+                       "Identifier=" + identifier,
+                       "EndMessage"
+               );
+               assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
+               assertThat(generatedKeys, contains("KSK@foo.txt"));
+       }
+
+       @Test
        public void clientCanListPeers() throws IOException, ExecutionException, InterruptedException {
                Future<Collection<Peer>> peers = fcpClient.listPeers().execute();
                connectNode();