From: David ‘Bombe’ Roden Date: Sun, 12 Jul 2015 11:09:47 +0000 (+0200) Subject: Add event for URIGenerated messages on ClientPut command X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=3351e5f36c74fc270152688c251ea32365808222;p=jFCPlib.git Add event for URIGenerated messages on ClientPut command --- diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommand.java index a8a571b..41c2377 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommand.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommand.java @@ -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 keyGenerated); ClientPutCommand named(String targetFilename); WithUri>> redirectTo(String uri); WithUri>> from(File file); diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java index 4f8ddde..1b322f3 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java @@ -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 payload = new AtomicReference<>(); private final AtomicLong length = new AtomicLong(); private final AtomicReference targetFilename = new AtomicReference<>(); + private final List> 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 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 keyGenerated : keyGenerateds) { + keyGenerated.accept(uriGenerated.getURI()); + } + } + + @Override protected void consumePutSuccessful(PutSuccessful putSuccessful) { finalKey.set(new Key(putSuccessful.getURI())); putFinished.set(true); diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index 450345b..a8dda59 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -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 generatedKeys = new CopyOnWriteArrayList<>(); + Future> key = fcpClient.clientPut() + .onKeyGenerated(generatedKeys::add) + .from(new ByteArrayInputStream("Hello\n".getBytes())) + .length(6) + .uri("KSK@foo.txt") + .execute(); + connectNode(); + List 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> peers = fcpClient.listPeers().execute(); connectNode();