X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Ffcp%2Fquelaton%2FClientGetCommandImpl.java;h=55d3e1c056495212626a95f1b272a4de82e96ff2;hb=fc1c3f3719425dfafb42fedef9ecad05783dd32c;hp=4439ccee800b53c4dc31c8cab2708c7dbede8cd5;hpb=52ddba35dcbe01d9c18d33814bab1dce85897d7e;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 4439cce..55d3e1c 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java @@ -3,6 +3,8 @@ package net.pterodactylus.fcp.quelaton; import java.io.IOException; import java.io.InputStream; import java.util.Optional; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; @@ -78,9 +80,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 (ClientGetReplySequence clientGetReplySequence = new ClientGetReplySequence()) { + return clientGetReplySequence.send(clientGet).get(); + } } private ClientGet createClientGetCommand(String uri) { @@ -109,7 +117,6 @@ class ClientGetCommandImpl implements ClientGetCommand { private class ClientGetReplySequence extends FcpReplySequence> { - private final AtomicReference identifier = new AtomicReference<>(); private final AtomicBoolean finished = new AtomicBoolean(); private final AtomicBoolean failed = new AtomicBoolean(); @@ -148,32 +155,22 @@ 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 - public ListenableFuture> send(FcpMessage fcpMessage) throws IOException { - identifier.set(fcpMessage.getField("Identifier")); - return super.send(fcpMessage); + failed.set(true); } }