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 = new ClientGet(uri, identifier, ReturnType.direct);
85 if (ignoreDataStore) {
86 clientGet.setIgnoreDataStore(true);
89 clientGet.setDataStoreOnly(true);
91 if (maxSize != null) {
92 clientGet.setMaxSize(maxSize);
94 if (priority != null) {
95 clientGet.setPriority(priority);
98 clientGet.setRealTimeFlag(true);
101 clientGet.setGlobal(true);
103 return threadPool.submit(() -> {
104 FcpReplySequence<Optional<Data>> replySequence =
105 new ClientGetReplySequence();
106 return replySequence.send(clientGet).get();
110 private class ClientGetReplySequence extends FcpReplySequence<Optional<Data>> {
112 private final AtomicBoolean finished = new AtomicBoolean();
113 private final AtomicBoolean failed = new AtomicBoolean();
115 private final String identifier = ClientGetCommandImpl.this.identifier;
117 private String contentType;
118 private long dataLength;
119 private InputStream payload;
121 public ClientGetReplySequence() throws IOException {
122 super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get());
126 protected boolean isFinished() {
127 return finished.get() || failed.get();
131 protected Optional<Data> getResult() {
132 return failed.get() ? Optional.empty() : Optional.of(new Data() {
134 public String getMimeType() {
144 public InputStream getInputStream() {
151 protected void consumeAllData(AllData allData) {
152 if (allData.getIdentifier().equals(identifier)) {
153 synchronized (this) {
154 contentType = allData.getContentType();
155 dataLength = allData.getDataLength();
157 payload = new TempInputStream(allData.getPayloadInputStream(), dataLength);
159 } catch (IOException e) {
168 protected void consumeGetFailed(GetFailed getFailed) {
169 if (getFailed.getIdentifier().equals(identifier)) {
175 protected void consumeConnectionClosed(Throwable throwable) {