1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.util.concurrent.ExecutorService;
5 import java.util.concurrent.Future;
6 import java.util.function.Consumer;
8 import net.pterodactylus.fcp.AllData;
9 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
10 import net.pterodactylus.fcp.ConfigData;
11 import net.pterodactylus.fcp.DataFound;
12 import net.pterodactylus.fcp.EndListPeerNotes;
13 import net.pterodactylus.fcp.EndListPeers;
14 import net.pterodactylus.fcp.EndListPersistentRequests;
15 import net.pterodactylus.fcp.FCPPluginReply;
16 import net.pterodactylus.fcp.FcpConnection;
17 import net.pterodactylus.fcp.FcpListener;
18 import net.pterodactylus.fcp.FcpMessage;
19 import net.pterodactylus.fcp.FinishedCompression;
20 import net.pterodactylus.fcp.GetFailed;
21 import net.pterodactylus.fcp.IdentifierCollision;
22 import net.pterodactylus.fcp.NodeData;
23 import net.pterodactylus.fcp.NodeHello;
24 import net.pterodactylus.fcp.Peer;
25 import net.pterodactylus.fcp.PeerNote;
26 import net.pterodactylus.fcp.PeerRemoved;
27 import net.pterodactylus.fcp.PersistentGet;
28 import net.pterodactylus.fcp.PersistentPut;
29 import net.pterodactylus.fcp.PersistentPutDir;
30 import net.pterodactylus.fcp.PersistentRequestModified;
31 import net.pterodactylus.fcp.PersistentRequestRemoved;
32 import net.pterodactylus.fcp.PluginInfo;
33 import net.pterodactylus.fcp.ProtocolError;
34 import net.pterodactylus.fcp.PutFailed;
35 import net.pterodactylus.fcp.PutFetchable;
36 import net.pterodactylus.fcp.PutSuccessful;
37 import net.pterodactylus.fcp.ReceivedBookmarkFeed;
38 import net.pterodactylus.fcp.SSKKeypair;
39 import net.pterodactylus.fcp.SentFeed;
40 import net.pterodactylus.fcp.SimpleProgress;
41 import net.pterodactylus.fcp.StartedCompression;
42 import net.pterodactylus.fcp.SubscribedUSKUpdate;
43 import net.pterodactylus.fcp.TestDDAComplete;
44 import net.pterodactylus.fcp.TestDDAReply;
45 import net.pterodactylus.fcp.URIGenerated;
46 import net.pterodactylus.fcp.UnknownNodeIdentifier;
47 import net.pterodactylus.fcp.UnknownPeerNoteType;
50 * An FCP reply sequence enables you to conveniently wait for a specific set of FCP replies.
52 * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
54 public abstract class FcpReplySequence<R> implements AutoCloseable, FcpListener {
56 private final Object syncObject = new Object();
57 private final ExecutorService executorService;
58 private final FcpConnection fcpConnection;
60 public FcpReplySequence(ExecutorService executorService, FcpConnection fcpConnection) {
61 this.executorService = executorService;
62 this.fcpConnection = fcpConnection;
65 protected abstract boolean isFinished();
67 public Future<R> send(FcpMessage fcpMessage) throws IOException {
69 fcpConnection.addFcpListener(this);
71 } catch (Throwable throwable) {
72 throwable.printStackTrace();
74 fcpConnection.sendMessage(fcpMessage);
75 return executorService.submit(() -> {
76 synchronized (syncObject) {
77 while (!isFinished()) {
85 protected R getResult() {
91 fcpConnection.removeFcpListener(this);
94 private <M> void consume(Consumer<M> consumer, M message) {
95 consumer.accept(message);
96 synchronized (syncObject) {
97 syncObject.notifyAll();
101 private void consumeUnknown(FcpMessage fcpMessage) {
102 consumeUnknownMessage(fcpMessage);
103 synchronized (syncObject) {
104 syncObject.notifyAll();
108 private void consumeClose(Throwable throwable) {
109 consumeConnectionClosed(throwable);
110 synchronized (syncObject) {
111 syncObject.notifyAll();
116 public final void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
117 consume(this::consumeNodeHello, nodeHello);
120 protected void consumeNodeHello(NodeHello nodeHello) { }
123 public final void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection,
124 CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
125 consume(this::consumeCloseConnectionDuplicateClientName, closeConnectionDuplicateClientName);
128 protected void consumeCloseConnectionDuplicateClientName(CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) { }
131 public final void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
132 consume(this::consumeSSKKeypair, sskKeypair);
135 protected void consumeSSKKeypair(SSKKeypair sskKeypair) { }
138 public final void receivedPeer(FcpConnection fcpConnection, Peer peer) {
139 consume(this::consumePeer, peer);
142 protected void consumePeer(Peer peer) { }
145 public final void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
146 consume(this::consumeEndListPeers, endListPeers);
149 protected void consumeEndListPeers(EndListPeers endListPeers) { }
152 public final void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
153 consume(this::consumePeerNote, peerNote);
156 protected void consumePeerNote(PeerNote peerNote) { }
159 public final void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
160 consume(this::consumeEndListPeerNotes, endListPeerNotes);
163 protected void consumeEndListPeerNotes(EndListPeerNotes endListPeerNotes) { }
166 public final void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
167 consume(this::consumePeerRemoved, peerRemoved);
170 protected void consumePeerRemoved(PeerRemoved peerRemoved) { }
173 public final void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) {
174 consume(this::consumeNodeData, nodeData);
177 protected void consumeNodeData(NodeData nodeData) { }
180 public final void receivedTestDDAReply(FcpConnection fcpConnection, TestDDAReply testDDAReply) {
181 consume(this::consumeTestDDAReply, testDDAReply);
184 protected void consumeTestDDAReply(TestDDAReply testDDAReply) { }
187 public final void receivedTestDDAComplete(FcpConnection fcpConnection, TestDDAComplete testDDAComplete) {
188 consume(this::consumeTestDDAComplete, testDDAComplete);
191 protected void consumeTestDDAComplete(TestDDAComplete testDDAComplete) { }
194 public final void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
195 consume(this::consumePersistentGet, persistentGet);
198 protected void consumePersistentGet(PersistentGet persistentGet) { }
201 public final void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) {
202 consume(this::consumePersistentPut, persistentPut);
205 protected void consumePersistentPut(PersistentPut persistentPut) { }
208 public final void receivedEndListPersistentRequests(FcpConnection fcpConnection,
209 EndListPersistentRequests endListPersistentRequests) {
210 consume(this::consumeEndListPersistentRequests, endListPersistentRequests);
213 protected void consumeEndListPersistentRequests(EndListPersistentRequests endListPersistentRequests) { }
216 public final void receivedURIGenerated(FcpConnection fcpConnection, URIGenerated uriGenerated) {
217 consume(this::consumeURIGenerated, uriGenerated);
220 protected void consumeURIGenerated(URIGenerated uriGenerated) { }
223 public final void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
224 consume(this::consumeDataFound, dataFound);
227 protected void consumeDataFound(DataFound dataFound) { }
230 public final void receivedAllData(FcpConnection fcpConnection, AllData allData) {
231 consume(this::consumeAllData, allData);
234 protected void consumeAllData(AllData allData) { }
237 public final void receivedSimpleProgress(FcpConnection fcpConnection, SimpleProgress simpleProgress) {
238 consume(this::consumeSimpleProgress, simpleProgress);
241 protected void consumeSimpleProgress(SimpleProgress simpleProgress) { }
244 public final void receivedStartedCompression(FcpConnection fcpConnection, StartedCompression startedCompression) {
245 consume(this::consumeStartedCompression, startedCompression);
248 protected void consumeStartedCompression(StartedCompression startedCompression) { }
251 public final void receivedFinishedCompression(FcpConnection fcpConnection, FinishedCompression finishedCompression) {
252 consume(this::consumeFinishedCompression, finishedCompression);
255 protected void consumeFinishedCompression(FinishedCompression finishedCompression) { }
258 public final void receivedUnknownPeerNoteType(FcpConnection fcpConnection, UnknownPeerNoteType unknownPeerNoteType) {
259 consume(this::consumeUnknownPeerNoteType, unknownPeerNoteType);
262 protected void consumeUnknownPeerNoteType(UnknownPeerNoteType unknownPeerNoteType) { }
265 public final void receivedUnknownNodeIdentifier(FcpConnection fcpConnection,
266 UnknownNodeIdentifier unknownNodeIdentifier) {
267 consume(this::consumeUnknownNodeIdentifier, unknownNodeIdentifier);
270 protected void consumeUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) { }
273 public final void receivedConfigData(FcpConnection fcpConnection, ConfigData configData) {
274 consume(this::consumeConfigData, configData);
277 protected void consumeConfigData(ConfigData configData) { }
280 public final void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
281 consume(this::consumeGetFailed, getFailed);
284 protected void consumeGetFailed(GetFailed getFailed) { }
287 public final void receivedPutFailed(FcpConnection fcpConnection, PutFailed putFailed) {
288 consume(this::consumePutFailed, putFailed);
291 protected void consumePutFailed(PutFailed putFailed) { }
294 public final void receivedIdentifierCollision(FcpConnection fcpConnection, IdentifierCollision identifierCollision) {
295 consume(this::consumeIdentifierCollision, identifierCollision);
298 protected void consumeIdentifierCollision(IdentifierCollision identifierCollision) { }
301 public final void receivedPersistentPutDir(FcpConnection fcpConnection, PersistentPutDir persistentPutDir) {
302 consume(this::consumePersistentPutDir, persistentPutDir);
305 protected void consumePersistentPutDir(PersistentPutDir persistentPutDir) { }
308 public final void receivedPersistentRequestRemoved(FcpConnection fcpConnection,
309 PersistentRequestRemoved persistentRequestRemoved) {
310 consume(this::consumePersistentRequestRemoved, persistentRequestRemoved);
313 protected void consumePersistentRequestRemoved(PersistentRequestRemoved persistentRequestRemoved) { }
316 public final void receivedSubscribedUSKUpdate(FcpConnection fcpConnection, SubscribedUSKUpdate subscribedUSKUpdate) {
317 consume(this::consumeSubscribedUSKUpdate, subscribedUSKUpdate);
320 protected void consumeSubscribedUSKUpdate(SubscribedUSKUpdate subscribedUSKUpdate) { }
323 public final void receivedPluginInfo(FcpConnection fcpConnection, PluginInfo pluginInfo) {
324 consume(this::consumePluginInfo, pluginInfo);
327 protected void consumePluginInfo(PluginInfo pluginInfo) { }
330 public final void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply) {
331 consume(this::consumeFCPPluginReply, fcpPluginReply);
334 protected void consumeFCPPluginReply(FCPPluginReply fcpPluginReply) { }
337 public final void receivedPersistentRequestModified(FcpConnection fcpConnection,
338 PersistentRequestModified persistentRequestModified) {
339 consume(this::consumePersistentRequestModified, persistentRequestModified);
342 protected void consumePersistentRequestModified(PersistentRequestModified persistentRequestModified) { }
345 public final void receivedPutSuccessful(FcpConnection fcpConnection, PutSuccessful putSuccessful) {
346 consume(this::consumePutSuccessful, putSuccessful);
349 protected void consumePutSuccessful(PutSuccessful putSuccessful) { }
352 public final void receivedPutFetchable(FcpConnection fcpConnection, PutFetchable putFetchable) {
353 consume(this::consumePutFetchable, putFetchable);
356 protected void consumePutFetchable(PutFetchable putFetchable) { }
359 public final void receivedSentFeed(FcpConnection source, SentFeed sentFeed) {
360 consume(this::consumeSentFeed, sentFeed);
363 protected void consumeSentFeed(SentFeed sentFeed) { }
366 public final void receivedBookmarkFeed(FcpConnection fcpConnection, ReceivedBookmarkFeed receivedBookmarkFeed) {
367 consume(this::consumeReceivedBookmarkFeed, receivedBookmarkFeed);
370 protected void consumeReceivedBookmarkFeed(ReceivedBookmarkFeed receivedBookmarkFeed) { }
373 public final void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
374 consume(this::consumeProtocolError, protocolError);
377 protected void consumeProtocolError(ProtocolError protocolError) { }
380 public final void receivedMessage(FcpConnection fcpConnection, FcpMessage fcpMessage) {
381 consumeUnknown(fcpMessage);
384 protected void consumeUnknownMessage(FcpMessage fcpMessage) { }
387 public final void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
388 consumeClose(throwable);
391 protected void consumeConnectionClosed(Throwable throwable) { }