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;
10 import net.pterodactylus.fcp.AllData;
11 import net.pterodactylus.fcp.ClientGet;
12 import net.pterodactylus.fcp.FcpUtils.TempInputStream;
13 import net.pterodactylus.fcp.GetFailed;
14 import net.pterodactylus.fcp.Priority;
15 import net.pterodactylus.fcp.ReturnType;
17 import com.google.common.util.concurrent.ListeningExecutorService;
18 import com.google.common.util.concurrent.MoreExecutors;
21 * Implementation of the {@link ClientGetCommand}.
23 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
25 class ClientGetCommandImpl implements ClientGetCommand {
27 private final ListeningExecutorService threadPool;
28 private final ConnectionSupplier connectionSupplier;
30 private boolean ignoreDataStore;
31 private boolean dataStoreOnly;
33 private Priority priority;
34 private boolean realTime;
35 private boolean global;
37 public ClientGetCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
38 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
39 this.connectionSupplier = connectionSupplier;
43 public ClientGetCommand ignoreDataStore() {
44 ignoreDataStore = true;
49 public ClientGetCommand dataStoreOnly() {
55 public ClientGetCommand maxSize(long maxSize) {
56 this.maxSize = maxSize;
61 public ClientGetCommand priority(Priority priority) {
62 this.priority = priority;
67 public ClientGetCommand realTime() {
73 public ClientGetCommand global() {
79 public Executable<Optional<Data>> uri(String uri) {
80 return () -> threadPool.submit(() -> execute(uri));
83 private Optional<Data> execute(String uri) throws InterruptedException, ExecutionException, IOException {
84 ClientGet clientGet = createClientGetCommand(uri);
85 try (ClientGetDialog clientGetDialog = new ClientGetDialog()) {
86 return clientGetDialog.send(clientGet).get();
90 private ClientGet createClientGetCommand(String uri) {
91 String identifier = new RandomIdentifierGenerator().generate();
92 ClientGet clientGet = new ClientGet(uri, identifier, ReturnType.direct);
93 if (ignoreDataStore) {
94 clientGet.setIgnoreDataStore(true);
97 clientGet.setDataStoreOnly(true);
99 if (maxSize != null) {
100 clientGet.setMaxSize(maxSize);
102 if (priority != null) {
103 clientGet.setPriority(priority);
106 clientGet.setRealTimeFlag(true);
109 clientGet.setGlobal(true);
114 private class ClientGetDialog extends FcpDialog<Optional<Data>> {
116 private final AtomicBoolean finished = new AtomicBoolean();
117 private final AtomicBoolean failed = new AtomicBoolean();
119 private String contentType;
120 private long dataLength;
121 private InputStream payload;
123 public ClientGetDialog() throws IOException {
124 super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get());
128 protected boolean isFinished() {
129 return finished.get() || failed.get();
133 protected Optional<Data> getResult() {
134 return failed.get() ? Optional.empty() : Optional.of(new Data() {
136 public String getMimeType() {
146 public InputStream getInputStream() {
153 protected void consumeAllData(AllData allData) {
154 synchronized (this) {
155 contentType = allData.getContentType();
156 dataLength = allData.getDataLength();
158 payload = new TempInputStream(allData.getPayloadInputStream(), dataLength);
160 } catch (IOException e) {
168 protected void consumeGetFailed(GetFailed getFailed) {