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