import java.io.InputStream;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
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;
+
/**
* Implementation of the {@link ClientGetCommand}.
*
*/
class ClientGetCommandImpl implements ClientGetCommand {
- private final ExecutorService threadPool;
+ private final ListeningExecutorService threadPool;
private final ConnectionSupplier connectionSupplier;
private boolean ignoreDataStore;
private boolean global;
public ClientGetCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
- this.threadPool = threadPool;
+ this.threadPool = MoreExecutors.listeningDecorator(threadPool);
this.connectionSupplier = connectionSupplier;
}
}
@Override
- public Future<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) {
private class ClientGetReplySequence extends FcpReplySequence<Optional<Data>> {
- private final AtomicReference<String> identifier = new AtomicReference<>();
private final AtomicBoolean finished = new AtomicBoolean();
private final AtomicBoolean failed = new AtomicBoolean();
@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 Future<Optional<Data>> send(FcpMessage fcpMessage) throws IOException {
- identifier.set(fcpMessage.getField("Identifier"));
- return super.send(fcpMessage);
- }
-
}
}