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.ExecutorService;
7 import java.util.concurrent.Future;
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;
18 * Implementation of the {@link ClientGetCommand}.
20 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
22 class ClientGetCommandImpl implements ClientGetCommand {
24 private final ExecutorService threadPool;
25 private final ConnectionSupplier connectionSupplier;
27 private String identifier;
28 private boolean ignoreDataStore;
29 private boolean dataStoreOnly;
31 private Priority priority;
32 private boolean realTime;
33 private boolean global;
35 public ClientGetCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
36 this.threadPool = threadPool;
37 this.connectionSupplier = connectionSupplier;
41 public ClientGetCommand identifier(String identifier) {
42 this.identifier = identifier;
47 public ClientGetCommand ignoreDataStore() {
48 ignoreDataStore = true;
53 public ClientGetCommand dataStoreOnly() {
59 public ClientGetCommand maxSize(long maxSize) {
60 this.maxSize = maxSize;
65 public ClientGetCommand priority(Priority priority) {
66 this.priority = priority;
71 public ClientGetCommand realTime() {
77 public ClientGetCommand global() {
83 public Future<Optional<Data>> uri(String uri) {
84 ClientGet clientGet = createClientGetCommand(uri);
85 return threadPool.submit(() -> new ClientGetReplySequence().send(clientGet).get());
88 private ClientGet createClientGetCommand(String uri) {
89 ClientGet clientGet = new ClientGet(uri, identifier, ReturnType.direct);
90 if (ignoreDataStore) {
91 clientGet.setIgnoreDataStore(true);
94 clientGet.setDataStoreOnly(true);
96 if (maxSize != null) {
97 clientGet.setMaxSize(maxSize);
99 if (priority != null) {
100 clientGet.setPriority(priority);
103 clientGet.setRealTimeFlag(true);
106 clientGet.setGlobal(true);
111 private class ClientGetReplySequence extends FcpReplySequence<Optional<Data>> {
113 private final AtomicBoolean finished = new AtomicBoolean();
114 private final AtomicBoolean failed = new AtomicBoolean();
116 private final String identifier = ClientGetCommandImpl.this.identifier;
118 private String contentType;
119 private long dataLength;
120 private InputStream payload;
122 public ClientGetReplySequence() throws IOException {
123 super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get());
127 protected boolean isFinished() {
128 return finished.get() || failed.get();
132 protected Optional<Data> getResult() {
133 return failed.get() ? Optional.empty() : Optional.of(new Data() {
135 public String getMimeType() {
145 public InputStream getInputStream() {
152 protected void consumeAllData(AllData allData) {
153 if (allData.getIdentifier().equals(identifier)) {
154 synchronized (this) {
155 contentType = allData.getContentType();
156 dataLength = allData.getDataLength();
158 payload = new TempInputStream(allData.getPayloadInputStream(), dataLength);
160 } catch (IOException e) {
169 protected void consumeGetFailed(GetFailed getFailed) {
170 if (getFailed.getIdentifier().equals(identifier)) {
176 protected void consumeConnectionClosed(Throwable throwable) {