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