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