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