X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Ffcp%2Fquelaton%2FClientGetCommandImpl.java;h=0c93fc32772a6112d7f12540ad6bda89fa946c19;hb=1d2048edc0856425c91337bb103ee526de663e9d;hp=575d94b353b885b8929e1671d5a907f8bc214d28;hpb=f5ba10406d666197d448956e4340254c4f5593b6;p=jFCPlib.git diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java index 575d94b..0c93fc3 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java @@ -3,19 +3,17 @@ package net.pterodactylus.fcp.quelaton; import java.io.IOException; import java.io.InputStream; import java.util.Optional; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; import net.pterodactylus.fcp.AllData; import net.pterodactylus.fcp.ClientGet; -import net.pterodactylus.fcp.FcpMessage; import net.pterodactylus.fcp.FcpUtils.TempInputStream; import net.pterodactylus.fcp.GetFailed; import net.pterodactylus.fcp.Priority; import net.pterodactylus.fcp.ReturnType; -import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.MoreExecutors; @@ -78,9 +76,15 @@ class ClientGetCommandImpl implements ClientGetCommand { } @Override - public ListenableFuture> uri(String uri) { + public Executable> uri(String uri) { + return () -> threadPool.submit(() -> execute(uri)); + } + + private Optional execute(String uri) throws InterruptedException, ExecutionException, IOException { ClientGet clientGet = createClientGetCommand(uri); - return threadPool.submit(() -> new ClientGetReplySequence().send(clientGet).get()); + try (ClientGetDialog clientGetDialog = new ClientGetDialog()) { + return clientGetDialog.send(clientGet).get(); + } } private ClientGet createClientGetCommand(String uri) { @@ -107,9 +111,8 @@ class ClientGetCommandImpl implements ClientGetCommand { return clientGet; } - private class ClientGetReplySequence extends FcpReplySequence> { + private class ClientGetDialog extends FcpDialog> { - private final AtomicReference identifier = new AtomicReference<>(); private final AtomicBoolean finished = new AtomicBoolean(); private final AtomicBoolean failed = new AtomicBoolean(); @@ -117,7 +120,7 @@ class ClientGetCommandImpl implements ClientGetCommand { private long dataLength; private InputStream payload; - public ClientGetReplySequence() throws IOException { + public ClientGetDialog() throws IOException { super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get()); } @@ -148,39 +151,24 @@ class ClientGetCommandImpl implements ClientGetCommand { @Override protected void consumeAllData(AllData allData) { - if (allData.getIdentifier().equals(identifier.get())) { - synchronized (this) { - contentType = allData.getContentType(); - dataLength = allData.getDataLength(); - try { - payload = new TempInputStream(allData.getPayloadInputStream(), dataLength); - finished.set(true); - } catch (IOException e) { - // TODO – logging - failed.set(true); - } + synchronized (this) { + contentType = allData.getContentType(); + dataLength = allData.getDataLength(); + try { + payload = new TempInputStream(allData.getPayloadInputStream(), dataLength); + finished.set(true); + } catch (IOException e) { + // TODO – logging + failed.set(true); } } } @Override protected void consumeGetFailed(GetFailed getFailed) { - if (getFailed.getIdentifier().equals(identifier.get())) { - failed.set(true); - } - } - - @Override - protected void consumeConnectionClosed(Throwable throwable) { failed.set(true); } - @Override - public ListenableFuture> send(FcpMessage fcpMessage) throws IOException { - identifier.set(fcpMessage.getField("Identifier")); - return super.send(fcpMessage); - } - } }