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