Add background fetch to freenet interface
[Sone.git] / src / main / java / net / pterodactylus / sone / core / FreenetInterface.java
1 /*
2  * Sone - FreenetInterface.java - Copyright © 2010–2016 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import static freenet.keys.USK.create;
21 import static java.lang.String.format;
22 import static java.util.logging.Level.WARNING;
23 import static java.util.logging.Logger.getLogger;
24 import static net.pterodactylus.sone.freenet.Key.routingKey;
25
26 import java.io.IOException;
27 import java.net.MalformedURLException;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33
34 import javax.annotation.Nonnull;
35
36 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
37 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
38 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
39 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
40 import net.pterodactylus.sone.data.Image;
41 import net.pterodactylus.sone.data.Sone;
42 import net.pterodactylus.sone.data.TemporaryImage;
43
44 import com.google.common.base.Function;
45 import com.google.common.eventbus.EventBus;
46 import com.google.inject.Inject;
47 import com.google.inject.Singleton;
48
49 import freenet.client.ClientMetadata;
50 import freenet.client.FetchContext;
51 import freenet.client.FetchException;
52 import freenet.client.FetchException.FetchExceptionMode;
53 import freenet.client.FetchResult;
54 import freenet.client.HighLevelSimpleClient;
55 import freenet.client.InsertBlock;
56 import freenet.client.InsertContext;
57 import freenet.client.InsertException;
58 import freenet.client.async.BaseClientPutter;
59 import freenet.client.async.ClientContext;
60 import freenet.client.async.ClientGetCallback;
61 import freenet.client.async.ClientGetter;
62 import freenet.client.async.ClientPutCallback;
63 import freenet.client.async.ClientPutter;
64 import freenet.client.async.USKCallback;
65 import freenet.keys.FreenetURI;
66 import freenet.keys.InsertableClientSSK;
67 import freenet.keys.USK;
68 import freenet.node.Node;
69 import freenet.node.RequestClient;
70 import freenet.node.RequestClientBuilder;
71 import freenet.node.RequestStarter;
72 import freenet.support.api.Bucket;
73 import freenet.support.api.RandomAccessBucket;
74 import freenet.support.io.ArrayBucket;
75 import freenet.support.io.ResumeFailedException;
76
77 /**
78  * Contains all necessary functionality for interacting with the Freenet node.
79  *
80  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
81  */
82 @Singleton
83 public class FreenetInterface {
84
85         /** The logger. */
86         private static final Logger logger = getLogger(FreenetInterface.class.getName());
87
88         /** The event bus. */
89         private final EventBus eventBus;
90
91         /** The node to interact with. */
92         private final Node node;
93
94         /** The high-level client to use for requests. */
95         private final HighLevelSimpleClient client;
96
97         /** The USK callbacks. */
98         private final Map<String, USKCallback> soneUskCallbacks = new HashMap<String, USKCallback>();
99
100         /** The not-Sone-related USK callbacks. */
101         private final Map<FreenetURI, USKCallback> uriUskCallbacks = Collections.synchronizedMap(new HashMap<FreenetURI, USKCallback>());
102
103         private final RequestClient imageInserts = new RequestClientBuilder().realTime().build();
104         private final RequestClient imageLoader = new RequestClientBuilder().realTime().build();
105
106         /**
107          * Creates a new Freenet interface.
108          *
109          * @param eventBus
110          *            The event bus
111          * @param node
112          *            The node to interact with
113          */
114         @Inject
115         public FreenetInterface(EventBus eventBus, Node node) {
116                 this.eventBus = eventBus;
117                 this.node = node;
118                 this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true);
119         }
120
121         //
122         // ACTIONS
123         //
124
125         /**
126          * Fetches the given URI.
127          *
128          * @param uri
129          *            The URI to fetch
130          * @return The result of the fetch, or {@code null} if an error occured
131          */
132         public Fetched fetchUri(FreenetURI uri) {
133                 FreenetURI currentUri = new FreenetURI(uri);
134                 while (true) {
135                         try {
136                                 FetchResult fetchResult = client.fetch(currentUri);
137                                 return new Fetched(currentUri, fetchResult);
138                         } catch (FetchException fe1) {
139                                 if (fe1.getMode() == FetchExceptionMode.PERMANENT_REDIRECT) {
140                                         currentUri = fe1.newURI;
141                                         continue;
142                                 }
143                                 logger.log(Level.WARNING, String.format("Could not fetch “%s”!", uri), fe1);
144                                 return null;
145                         }
146                 }
147         }
148
149         public void startFetch(final FreenetURI uri, final BackgroundFetchCallback backgroundFetchCallback) {
150                 ClientGetCallback callback = new ClientGetCallback() {
151                         @Override
152                         public void onSuccess(FetchResult result, ClientGetter state) {
153                                 try {
154                                         backgroundFetchCallback.loaded(uri, result.getMimeType(), result.asByteArray());
155                                 } catch (IOException e) {
156                                         backgroundFetchCallback.failed(uri);
157                                 }
158                         }
159
160                         @Override
161                         public void onFailure(FetchException e, ClientGetter state) {
162                                 backgroundFetchCallback.failed(uri);
163                         }
164
165                         @Override
166                         public void onResume(ClientContext context) throws ResumeFailedException {
167                                 /* do nothing. */
168                         }
169
170                         @Override
171                         public RequestClient getRequestClient() {
172                                 return imageLoader;
173                         }
174                 };
175                 FetchContext fetchContext = client.getFetchContext();
176                 try {
177                         client.fetch(uri, 1048576, callback, fetchContext, RequestStarter.INTERACTIVE_PRIORITY_CLASS);
178                 } catch (FetchException fe) {
179                         /* stupid exception that can not actually be thrown! */
180                 }
181         }
182
183         public interface BackgroundFetchCallback {
184                 void loaded(@Nonnull FreenetURI uri, @Nonnull String mimeType, @Nonnull byte[] data);
185                 void failed(@Nonnull FreenetURI uri);
186         }
187
188         /**
189          * Inserts the image data of the given {@link TemporaryImage} and returns
190          * the given insert token that can be used to add listeners or cancel the
191          * insert.
192          *
193          * @param temporaryImage
194          *            The temporary image data
195          * @param image
196          *            The image
197          * @param insertToken
198          *            The insert token
199          * @throws SoneException
200          *             if the insert could not be started
201          */
202         public void insertImage(TemporaryImage temporaryImage, Image image, InsertToken insertToken) throws SoneException {
203                 String filenameHint = image.getId() + "." + temporaryImage.getMimeType().substring(temporaryImage.getMimeType().lastIndexOf("/") + 1);
204                 InsertableClientSSK key = InsertableClientSSK.createRandom(node.random, "");
205                 FreenetURI targetUri = key.getInsertURI().setDocName(filenameHint);
206                 InsertContext insertContext = client.getInsertContext(true);
207                 RandomAccessBucket bucket = new ArrayBucket(temporaryImage.getImageData());
208                 insertToken.setBucket(bucket);
209                 ClientMetadata metadata = new ClientMetadata(temporaryImage.getMimeType());
210                 InsertBlock insertBlock = new InsertBlock(bucket, metadata, targetUri);
211                 try {
212                         ClientPutter clientPutter = client.insert(insertBlock, null, false, insertContext, insertToken, RequestStarter.INTERACTIVE_PRIORITY_CLASS);
213                         insertToken.setClientPutter(clientPutter);
214                 } catch (InsertException ie1) {
215                         throw new SoneInsertException("Could not start image insert.", ie1);
216                 }
217         }
218
219         /**
220          * Inserts a directory into Freenet.
221          *
222          * @param insertUri
223          *            The insert URI
224          * @param manifestEntries
225          *            The directory entries
226          * @param defaultFile
227          *            The name of the default file
228          * @return The generated URI
229          * @throws SoneException
230          *             if an insert error occurs
231          */
232         public FreenetURI insertDirectory(FreenetURI insertUri, HashMap<String, Object> manifestEntries, String defaultFile) throws SoneException {
233                 try {
234                         return client.insertManifest(insertUri, manifestEntries, defaultFile);
235                 } catch (InsertException ie1) {
236                         throw new SoneException(ie1);
237                 }
238         }
239
240         public void registerActiveUsk(FreenetURI requestUri,
241                         USKCallback uskCallback) {
242                 try {
243                         soneUskCallbacks.put(routingKey(requestUri), uskCallback);
244                         node.clientCore.uskManager.subscribe(create(requestUri),
245                                         uskCallback, true, (RequestClient) client);
246                 } catch (MalformedURLException mue1) {
247                         logger.log(WARNING, format("Could not subscribe USK “%s”!",
248                                         requestUri), mue1);
249                 }
250         }
251
252         public void registerPassiveUsk(FreenetURI requestUri,
253                         USKCallback uskCallback) {
254                 try {
255                         soneUskCallbacks.put(routingKey(requestUri), uskCallback);
256                         node.clientCore
257                                         .uskManager
258                                         .subscribe(create(requestUri), uskCallback, false,
259                                                         (RequestClient) client);
260                 } catch (MalformedURLException mue1) {
261                         logger.log(WARNING,
262                                         format("Could not subscribe USK “%s”!", requestUri),
263                                         mue1);
264                 }
265         }
266
267         /**
268          * Unsubscribes the request URI of the given Sone.
269          *
270          * @param sone
271          *            The Sone to unregister
272          */
273         public void unregisterUsk(Sone sone) {
274                 USKCallback uskCallback = soneUskCallbacks.remove(sone.getId());
275                 if (uskCallback == null) {
276                         return;
277                 }
278                 try {
279                         logger.log(Level.FINEST, String.format("Unsubscribing from USK for %s…", sone));
280                         node.clientCore.uskManager.unsubscribe(USK.create(sone.getRequestUri()), uskCallback);
281                 } catch (MalformedURLException mue1) {
282                         logger.log(Level.FINE, String.format("Could not unsubscribe USK “%s”!", sone.getRequestUri()), mue1);
283                 }
284         }
285
286         /**
287          * Registers an arbitrary URI and calls the given callback if a new edition
288          * is found.
289          *
290          * @param uri
291          *            The URI to watch
292          * @param callback
293          *            The callback to call
294          */
295         public void registerUsk(FreenetURI uri, final Callback callback) {
296                 USKCallback uskCallback = new USKCallback() {
297
298                         @Override
299                         public void onFoundEdition(long edition, USK key, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
300                                 callback.editionFound(key.getURI(), edition, newKnownGood, newSlotToo);
301                         }
302
303                         @Override
304                         public short getPollingPriorityNormal() {
305                                 return RequestStarter.PREFETCH_PRIORITY_CLASS;
306                         }
307
308                         @Override
309                         public short getPollingPriorityProgress() {
310                                 return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
311                         }
312
313                 };
314                 try {
315                         node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, (RequestClient) client);
316                         uriUskCallbacks.put(uri, uskCallback);
317                 } catch (MalformedURLException mue1) {
318                         logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1);
319                 }
320         }
321
322         /**
323          * Unregisters the USK watcher for the given URI.
324          *
325          * @param uri
326          *            The URI to unregister the USK watcher for
327          */
328         public void unregisterUsk(FreenetURI uri) {
329                 USKCallback uskCallback = uriUskCallbacks.remove(uri);
330                 if (uskCallback == null) {
331                         logger.log(Level.INFO, String.format("Could not unregister unknown USK: %s", uri));
332                         return;
333                 }
334                 try {
335                         node.clientCore.uskManager.unsubscribe(USK.create(uri), uskCallback);
336                 } catch (MalformedURLException mue1) {
337                         logger.log(Level.INFO, String.format("Could not unregister invalid USK: %s", uri), mue1);
338                 }
339         }
340
341         /**
342          * Container for a fetched URI and the {@link FetchResult}.
343          *
344          * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
345          */
346         public static class Fetched {
347
348                 /** The fetched URI. */
349                 private final FreenetURI freenetUri;
350
351                 /** The fetch result. */
352                 private final FetchResult fetchResult;
353
354                 /**
355                  * Creates a new fetched URI.
356                  *
357                  * @param freenetUri
358                  *            The URI that was fetched
359                  * @param fetchResult
360                  *            The fetch result
361                  */
362                 public Fetched(FreenetURI freenetUri, FetchResult fetchResult) {
363                         this.freenetUri = freenetUri;
364                         this.fetchResult = fetchResult;
365                 }
366
367                 //
368                 // ACCESSORS
369                 //
370
371                 /**
372                  * Returns the fetched URI.
373                  *
374                  * @return The fetched URI
375                  */
376                 public FreenetURI getFreenetUri() {
377                         return freenetUri;
378                 }
379
380                 /**
381                  * Returns the fetch result.
382                  *
383                  * @return The fetch result
384                  */
385                 public FetchResult getFetchResult() {
386                         return fetchResult;
387                 }
388
389         }
390
391         /**
392          * Callback for USK watcher events.
393          *
394          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
395          */
396         public static interface Callback {
397
398                 /**
399                  * Notifies a listener that a new edition was found for a URI.
400                  *
401                  * @param uri
402                  *            The URI that a new edition was found for
403                  * @param edition
404                  *            The found edition
405                  * @param newKnownGood
406                  *            Whether the found edition was actually fetched
407                  * @param newSlot
408                  *            Whether the found edition is higher than all previously
409                  *            found editions
410                  */
411                 public void editionFound(FreenetURI uri, long edition, boolean newKnownGood, boolean newSlot);
412
413         }
414
415         /**
416          * Insert token that can cancel a running insert and sends events.
417          *
418          * @see ImageInsertAbortedEvent
419          * @see ImageInsertStartedEvent
420          * @see ImageInsertFailedEvent
421          * @see ImageInsertFinishedEvent
422          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
423          */
424         public class InsertToken implements ClientPutCallback {
425
426                 /** The image being inserted. */
427                 private final Image image;
428
429                 /** The client putter. */
430                 private ClientPutter clientPutter;
431                 private Bucket bucket;
432
433                 /** The final URI. */
434                 private volatile FreenetURI resultingUri;
435
436                 /**
437                  * Creates a new insert token for the given image.
438                  *
439                  * @param image
440                  *            The image being inserted
441                  */
442                 public InsertToken(Image image) {
443                         this.image = image;
444                 }
445
446                 //
447                 // ACCESSORS
448                 //
449
450                 /**
451                  * Sets the client putter that is inserting the image. This will also
452                  * signal all registered listeners that the image has started.
453                  *
454                  * @param clientPutter
455                  *            The client putter
456                  */
457                 @SuppressWarnings("synthetic-access")
458                 public void setClientPutter(ClientPutter clientPutter) {
459                         this.clientPutter = clientPutter;
460                         eventBus.post(new ImageInsertStartedEvent(image));
461                 }
462
463                 public void setBucket(Bucket bucket) {
464                         this.bucket = bucket;
465                 }
466
467                 //
468                 // ACTIONS
469                 //
470
471                 /**
472                  * Cancels the running insert.
473                  */
474                 @SuppressWarnings("synthetic-access")
475                 public void cancel() {
476                         clientPutter.cancel(node.clientCore.clientContext);
477                         eventBus.post(new ImageInsertAbortedEvent(image));
478                         bucket.free();
479                 }
480
481                 //
482                 // INTERFACE ClientPutCallback
483                 //
484
485                 @Override
486                 public RequestClient getRequestClient() {
487                         return imageInserts;
488                 }
489
490                 @Override
491                 public void onResume(ClientContext context) throws ResumeFailedException {
492                         /* ignore. */
493                 }
494
495                 /**
496                  * {@inheritDoc}
497                  */
498                 @Override
499                 @SuppressWarnings("synthetic-access")
500                 public void onFailure(InsertException insertException, BaseClientPutter clientPutter) {
501                         if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) {
502                                 eventBus.post(new ImageInsertAbortedEvent(image));
503                         } else {
504                                 eventBus.post(new ImageInsertFailedEvent(image, insertException));
505                         }
506                         bucket.free();
507                 }
508
509                 /**
510                  * {@inheritDoc}
511                  */
512                 @Override
513                 public void onFetchable(BaseClientPutter clientPutter) {
514                         /* ignore, we don’t care. */
515                 }
516
517                 /**
518                  * {@inheritDoc}
519                  */
520                 @Override
521                 public void onGeneratedMetadata(Bucket metadata, BaseClientPutter clientPutter) {
522                         /* ignore, we don’t care. */
523                 }
524
525                 /**
526                  * {@inheritDoc}
527                  */
528                 @Override
529                 public void onGeneratedURI(FreenetURI generatedUri, BaseClientPutter clientPutter) {
530                         resultingUri = generatedUri;
531                 }
532
533                 /**
534                  * {@inheritDoc}
535                  */
536                 @Override
537                 @SuppressWarnings("synthetic-access")
538                 public void onSuccess(BaseClientPutter clientPutter) {
539                         eventBus.post(new ImageInsertFinishedEvent(image, resultingUri));
540                         bucket.free();
541                 }
542
543         }
544
545         public class InsertTokenSupplier implements Function<Image, InsertToken> {
546
547                 @Override
548                 public InsertToken apply(Image image) {
549                         return new InsertToken(image);
550                 }
551
552         }
553
554 }