1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Optional;
6 import java.util.concurrent.ExecutionException;
7 import java.util.concurrent.ExecutorService;
8 import java.util.concurrent.atomic.AtomicBoolean;
9 import java.util.function.Supplier;
11 import net.pterodactylus.fcp.AllData;
12 import net.pterodactylus.fcp.ClientGet;
13 import net.pterodactylus.fcp.FcpUtils.TempInputStream;
14 import net.pterodactylus.fcp.GetFailed;
15 import net.pterodactylus.fcp.Priority;
16 import net.pterodactylus.fcp.ReturnType;
18 import com.google.common.util.concurrent.ListeningExecutorService;
19 import com.google.common.util.concurrent.MoreExecutors;
22 * Implementation of the {@link ClientGetCommand}.
24 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26 class ClientGetCommandImpl implements ClientGetCommand {
28 private final ListeningExecutorService threadPool;
29 private final ConnectionSupplier connectionSupplier;
30 private final Supplier<String> identifierGenerator;
32 private boolean ignoreDataStore;
33 private boolean dataStoreOnly;
35 private Priority priority;
36 private boolean realTime;
37 private boolean global;
39 public ClientGetCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
40 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
41 this.connectionSupplier = connectionSupplier;
42 this.identifierGenerator = identifierGenerator;
46 public ClientGetCommand ignoreDataStore() {
47 ignoreDataStore = true;
52 public ClientGetCommand dataStoreOnly() {
58 public ClientGetCommand maxSize(long maxSize) {
59 this.maxSize = maxSize;
64 public ClientGetCommand priority(Priority priority) {
65 this.priority = priority;
70 public ClientGetCommand realTime() {
76 public ClientGetCommand global() {
82 public Executable<Optional<Data>> uri(String uri) {
83 return () -> threadPool.submit(() -> execute(uri));
86 private Optional<Data> execute(String uri) throws InterruptedException, ExecutionException, IOException {
87 ClientGet clientGet = createClientGetCommand(uri);
88 try (ClientGetDialog clientGetDialog = new ClientGetDialog()) {
89 return clientGetDialog.send(clientGet).get();
93 private ClientGet createClientGetCommand(String uri) {
94 String identifier = identifierGenerator.get();
95 ClientGet clientGet = new ClientGet(uri, identifier, ReturnType.direct);
96 if (ignoreDataStore) {
97 clientGet.setIgnoreDataStore(true);
100 clientGet.setDataStoreOnly(true);
102 if (maxSize != null) {
103 clientGet.setMaxSize(maxSize);
105 if (priority != null) {
106 clientGet.setPriority(priority);
109 clientGet.setRealTimeFlag(true);
112 clientGet.setGlobal(true);
117 private class ClientGetDialog extends FcpDialog<Optional<Data>> {
119 private final AtomicBoolean finished = new AtomicBoolean();
120 private final AtomicBoolean failed = new AtomicBoolean();
122 private String contentType;
123 private long dataLength;
124 private InputStream payload;
126 public ClientGetDialog() throws IOException {
127 super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get());
131 protected boolean isFinished() {
132 return finished.get() || failed.get();
136 protected Optional<Data> getResult() {
137 return failed.get() ? Optional.empty() : Optional.of(new Data() {
139 public String getMimeType() {
149 public InputStream getInputStream() {
156 protected void consumeAllData(AllData allData) {
157 synchronized (this) {
158 contentType = allData.getContentType();
159 dataLength = allData.getDataLength();
161 payload = new TempInputStream(allData.getPayloadInputStream(), dataLength);
163 } catch (IOException e) {
171 protected void consumeGetFailed(GetFailed getFailed) {