1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.util.Objects;
5 import java.util.Queue;
6 import java.util.concurrent.ConcurrentLinkedQueue;
7 import java.util.concurrent.ExecutionException;
8 import java.util.concurrent.ExecutorService;
9 import java.util.concurrent.atomic.AtomicBoolean;
10 import java.util.concurrent.atomic.AtomicReference;
11 import java.util.function.Consumer;
13 import net.pterodactylus.fcp.AllData;
14 import net.pterodactylus.fcp.BaseMessage;
15 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
16 import net.pterodactylus.fcp.ConfigData;
17 import net.pterodactylus.fcp.DataFound;
18 import net.pterodactylus.fcp.EndListPeerNotes;
19 import net.pterodactylus.fcp.EndListPeers;
20 import net.pterodactylus.fcp.EndListPersistentRequests;
21 import net.pterodactylus.fcp.FCPPluginReply;
22 import net.pterodactylus.fcp.FcpConnection;
23 import net.pterodactylus.fcp.FcpListener;
24 import net.pterodactylus.fcp.FcpMessage;
25 import net.pterodactylus.fcp.FinishedCompression;
26 import net.pterodactylus.fcp.GetFailed;
27 import net.pterodactylus.fcp.IdentifierCollision;
28 import net.pterodactylus.fcp.NodeData;
29 import net.pterodactylus.fcp.NodeHello;
30 import net.pterodactylus.fcp.Peer;
31 import net.pterodactylus.fcp.PeerNote;
32 import net.pterodactylus.fcp.PeerRemoved;
33 import net.pterodactylus.fcp.PersistentGet;
34 import net.pterodactylus.fcp.PersistentPut;
35 import net.pterodactylus.fcp.PersistentPutDir;
36 import net.pterodactylus.fcp.PersistentRequestModified;
37 import net.pterodactylus.fcp.PersistentRequestRemoved;
38 import net.pterodactylus.fcp.PluginInfo;
39 import net.pterodactylus.fcp.ProtocolError;
40 import net.pterodactylus.fcp.PutFailed;
41 import net.pterodactylus.fcp.PutFetchable;
42 import net.pterodactylus.fcp.PutSuccessful;
43 import net.pterodactylus.fcp.ReceivedBookmarkFeed;
44 import net.pterodactylus.fcp.SSKKeypair;
45 import net.pterodactylus.fcp.SentFeed;
46 import net.pterodactylus.fcp.SimpleProgress;
47 import net.pterodactylus.fcp.StartedCompression;
48 import net.pterodactylus.fcp.SubscribedUSKUpdate;
49 import net.pterodactylus.fcp.TestDDAComplete;
50 import net.pterodactylus.fcp.TestDDAReply;
51 import net.pterodactylus.fcp.URIGenerated;
52 import net.pterodactylus.fcp.UnknownNodeIdentifier;
53 import net.pterodactylus.fcp.UnknownPeerNoteType;
55 import com.google.common.util.concurrent.ListenableFuture;
56 import com.google.common.util.concurrent.ListeningExecutorService;
57 import com.google.common.util.concurrent.MoreExecutors;
60 * An FCP reply sequence enables you to conveniently wait for a specific set of FCP replies.
62 * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
64 public abstract class FcpReplySequence<R> implements AutoCloseable, FcpListener {
66 private final Object syncObject = new Object();
67 private final ListeningExecutorService executorService;
68 private final FcpConnection fcpConnection;
69 private final Queue<FcpMessage> messages = new ConcurrentLinkedQueue<>();
70 private final AtomicReference<String> identifier = new AtomicReference<>();
71 private final AtomicBoolean connectionClosed = new AtomicBoolean();
72 private final AtomicReference<Throwable> connectionFailureReason = new AtomicReference<>();
74 public FcpReplySequence(ExecutorService executorService, FcpConnection fcpConnection) {
75 this.executorService = MoreExecutors.listeningDecorator(executorService);
76 this.fcpConnection = fcpConnection;
79 protected void setIdentifier(String identifier) {
80 this.identifier.set(identifier);
83 protected abstract boolean isFinished();
85 public ListenableFuture<R> send(FcpMessage fcpMessage) throws IOException {
86 setIdentifier(fcpMessage.getField("Identifier"));
87 fcpConnection.addFcpListener(this);
88 messages.add(fcpMessage);
89 return executorService.submit(() -> {
90 synchronized (syncObject) {
91 while (!connectionClosed.get() && (!isFinished() || !messages.isEmpty())) {
92 while (messages.peek() != null) {
93 FcpMessage message = messages.poll();
94 fcpConnection.sendMessage(message);
96 if (isFinished() || connectionClosed.get()) {
102 Throwable throwable = connectionFailureReason.get();
103 if (throwable != null) {
104 throw new ExecutionException(throwable);
110 protected void sendMessage(FcpMessage fcpMessage) {
111 messages.add(fcpMessage);
115 private void notifySyncObject() {
116 synchronized (syncObject) {
117 syncObject.notifyAll();
121 protected R getResult() {
126 public void close() {
127 fcpConnection.removeFcpListener(this);
130 private <M extends BaseMessage> void consume(Consumer<M> consumer, M message) {
131 consume(consumer, message, "Identifier");
134 private <M extends BaseMessage> void consume(Consumer<M> consumer, M message,
136 if (Objects.equals(message.getField(identifier), this.identifier.get())) {
137 consumeAlways(consumer, message);
141 private <M extends BaseMessage> void consumeAlways(Consumer<M> consumer, M message) {
142 consumer.accept(message);
146 private void consumeUnknown(FcpMessage fcpMessage) {
147 consumeUnknownMessage(fcpMessage);
151 private void consumeClose(Throwable throwable) {
152 connectionFailureReason.set(throwable);
153 connectionClosed.set(true);
158 public final void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
159 consume(this::consumeNodeHello, nodeHello);
162 protected void consumeNodeHello(NodeHello nodeHello) { }
165 public final void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection,
166 CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
167 connectionFailureReason.set(new IOException("duplicate client name"));
168 connectionClosed.set(true);
173 public final void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
174 consume(this::consumeSSKKeypair, sskKeypair);
177 protected void consumeSSKKeypair(SSKKeypair sskKeypair) { }
180 public final void receivedPeer(FcpConnection fcpConnection, Peer peer) {
181 consume(this::consumePeer, peer);
184 protected void consumePeer(Peer peer) { }
187 public final void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
188 consume(this::consumeEndListPeers, endListPeers);
191 protected void consumeEndListPeers(EndListPeers endListPeers) { }
194 public final void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
195 consume(this::consumePeerNote, peerNote);
198 protected void consumePeerNote(PeerNote peerNote) { }
201 public final void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
202 consume(this::consumeEndListPeerNotes, endListPeerNotes);
205 protected void consumeEndListPeerNotes(EndListPeerNotes endListPeerNotes) { }
208 public final void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
209 consume(this::consumePeerRemoved, peerRemoved);
212 protected void consumePeerRemoved(PeerRemoved peerRemoved) { }
215 public final void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) {
216 consume(this::consumeNodeData, nodeData);
219 protected void consumeNodeData(NodeData nodeData) { }
222 public final void receivedTestDDAReply(FcpConnection fcpConnection, TestDDAReply testDDAReply) {
223 consume(this::consumeTestDDAReply, testDDAReply, "Directory");
226 protected void consumeTestDDAReply(TestDDAReply testDDAReply) { }
229 public final void receivedTestDDAComplete(FcpConnection fcpConnection, TestDDAComplete testDDAComplete) {
230 consume(this::consumeTestDDAComplete, testDDAComplete, "Directory");
233 protected void consumeTestDDAComplete(TestDDAComplete testDDAComplete) { }
236 public final void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
237 consume(this::consumePersistentGet, persistentGet);
240 protected void consumePersistentGet(PersistentGet persistentGet) { }
243 public final void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) {
244 consume(this::consumePersistentPut, persistentPut);
247 protected void consumePersistentPut(PersistentPut persistentPut) { }
250 public final void receivedEndListPersistentRequests(FcpConnection fcpConnection,
251 EndListPersistentRequests endListPersistentRequests) {
252 consume(this::consumeEndListPersistentRequests, endListPersistentRequests);
255 protected void consumeEndListPersistentRequests(EndListPersistentRequests endListPersistentRequests) { }
258 public final void receivedURIGenerated(FcpConnection fcpConnection, URIGenerated uriGenerated) {
259 consume(this::consumeURIGenerated, uriGenerated);
262 protected void consumeURIGenerated(URIGenerated uriGenerated) { }
265 public final void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
266 consume(this::consumeDataFound, dataFound);
269 protected void consumeDataFound(DataFound dataFound) { }
272 public final void receivedAllData(FcpConnection fcpConnection, AllData allData) {
273 consume(this::consumeAllData, allData);
276 protected void consumeAllData(AllData allData) { }
279 public final void receivedSimpleProgress(FcpConnection fcpConnection, SimpleProgress simpleProgress) {
280 consume(this::consumeSimpleProgress, simpleProgress);
283 protected void consumeSimpleProgress(SimpleProgress simpleProgress) { }
286 public final void receivedStartedCompression(FcpConnection fcpConnection, StartedCompression startedCompression) {
287 consume(this::consumeStartedCompression, startedCompression);
290 protected void consumeStartedCompression(StartedCompression startedCompression) { }
293 public final void receivedFinishedCompression(FcpConnection fcpConnection, FinishedCompression finishedCompression) {
294 consume(this::consumeFinishedCompression, finishedCompression);
297 protected void consumeFinishedCompression(FinishedCompression finishedCompression) { }
300 public final void receivedUnknownPeerNoteType(FcpConnection fcpConnection, UnknownPeerNoteType unknownPeerNoteType) {
301 consume(this::consumeUnknownPeerNoteType, unknownPeerNoteType);
304 protected void consumeUnknownPeerNoteType(UnknownPeerNoteType unknownPeerNoteType) { }
307 public final void receivedUnknownNodeIdentifier(FcpConnection fcpConnection,
308 UnknownNodeIdentifier unknownNodeIdentifier) {
309 consume(this::consumeUnknownNodeIdentifier, unknownNodeIdentifier);
312 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) { }
315 public final void receivedConfigData(FcpConnection fcpConnection, ConfigData configData) {
316 consume(this::consumeConfigData, configData);
319 protected void consumeConfigData(ConfigData configData) { }
322 public final void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
323 consume(this::consumeGetFailed, getFailed);
326 protected void consumeGetFailed(GetFailed getFailed) { }
329 public final void receivedPutFailed(FcpConnection fcpConnection, PutFailed putFailed) {
330 consume(this::consumePutFailed, putFailed);
333 protected void consumePutFailed(PutFailed putFailed) { }
336 public final void receivedIdentifierCollision(FcpConnection fcpConnection, IdentifierCollision identifierCollision) {
337 consume(this::consumeIdentifierCollision, identifierCollision);
340 protected void consumeIdentifierCollision(IdentifierCollision identifierCollision) { }
343 public final void receivedPersistentPutDir(FcpConnection fcpConnection, PersistentPutDir persistentPutDir) {
344 consume(this::consumePersistentPutDir, persistentPutDir);
347 protected void consumePersistentPutDir(PersistentPutDir persistentPutDir) { }
350 public final void receivedPersistentRequestRemoved(FcpConnection fcpConnection,
351 PersistentRequestRemoved persistentRequestRemoved) {
352 consume(this::consumePersistentRequestRemoved, persistentRequestRemoved);
355 protected void consumePersistentRequestRemoved(PersistentRequestRemoved persistentRequestRemoved) { }
358 public final void receivedSubscribedUSKUpdate(FcpConnection fcpConnection, SubscribedUSKUpdate subscribedUSKUpdate) {
359 consume(this::consumeSubscribedUSKUpdate, subscribedUSKUpdate);
362 protected void consumeSubscribedUSKUpdate(SubscribedUSKUpdate subscribedUSKUpdate) { }
365 public final void receivedPluginInfo(FcpConnection fcpConnection, PluginInfo pluginInfo) {
366 consume(this::consumePluginInfo, pluginInfo);
369 protected void consumePluginInfo(PluginInfo pluginInfo) { }
372 public final void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply) {
373 consume(this::consumeFCPPluginReply, fcpPluginReply);
376 protected void consumeFCPPluginReply(FCPPluginReply fcpPluginReply) { }
379 public final void receivedPersistentRequestModified(FcpConnection fcpConnection,
380 PersistentRequestModified persistentRequestModified) {
381 consume(this::consumePersistentRequestModified, persistentRequestModified);
384 protected void consumePersistentRequestModified(PersistentRequestModified persistentRequestModified) { }
387 public final void receivedPutSuccessful(FcpConnection fcpConnection, PutSuccessful putSuccessful) {
388 consume(this::consumePutSuccessful, putSuccessful);
391 protected void consumePutSuccessful(PutSuccessful putSuccessful) { }
394 public final void receivedPutFetchable(FcpConnection fcpConnection, PutFetchable putFetchable) {
395 consume(this::consumePutFetchable, putFetchable);
398 protected void consumePutFetchable(PutFetchable putFetchable) { }
401 public final void receivedSentFeed(FcpConnection source, SentFeed sentFeed) {
402 consume(this::consumeSentFeed, sentFeed);
405 protected void consumeSentFeed(SentFeed sentFeed) { }
408 public final void receivedBookmarkFeed(FcpConnection fcpConnection, ReceivedBookmarkFeed receivedBookmarkFeed) {
409 consume(this::consumeReceivedBookmarkFeed, receivedBookmarkFeed);
412 protected void consumeReceivedBookmarkFeed(ReceivedBookmarkFeed receivedBookmarkFeed) { }
415 public final void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
416 consume(this::consumeProtocolError, protocolError);
419 protected void consumeProtocolError(ProtocolError protocolError) { }
422 public final void receivedMessage(FcpConnection fcpConnection, FcpMessage fcpMessage) {
423 consumeUnknown(fcpMessage);
426 protected void consumeUnknownMessage(FcpMessage fcpMessage) { }
429 public final void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
430 consumeClose(throwable);