2 * Sone - FreenetInterface.java - Copyright © 2010–2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.core;
20 import static freenet.client.FetchException.PERMANENT_REDIRECT;
21 import static freenet.node.RequestStarter.INTERACTIVE_PRIORITY_CLASS;
22 import static freenet.node.RequestStarter.PREFETCH_PRIORITY_CLASS;
23 import static java.lang.String.format;
24 import static java.util.Collections.synchronizedMap;
25 import static java.util.concurrent.TimeUnit.DAYS;
26 import static java.util.logging.Level.FINE;
27 import static java.util.logging.Level.FINEST;
28 import static java.util.logging.Level.INFO;
29 import static java.util.logging.Level.WARNING;
30 import static net.pterodactylus.sone.data.Sone.TO_FREENET_URI;
31 import static net.pterodactylus.util.logging.Logging.getLogger;
33 import java.net.MalformedURLException;
34 import java.util.HashMap;
36 import java.util.logging.Logger;
38 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
39 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
40 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
41 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
42 import net.pterodactylus.sone.data.Image;
43 import net.pterodactylus.sone.data.Sone;
44 import net.pterodactylus.sone.data.TemporaryImage;
45 import net.pterodactylus.util.logging.Logging;
47 import com.db4o.ObjectContainer;
48 import com.google.common.annotations.VisibleForTesting;
49 import com.google.common.eventbus.EventBus;
50 import com.google.inject.Inject;
51 import freenet.client.ClientMetadata;
52 import freenet.client.FetchException;
53 import freenet.client.FetchResult;
54 import freenet.client.HighLevelSimpleClient;
55 import freenet.client.HighLevelSimpleClientImpl;
56 import freenet.client.InsertBlock;
57 import freenet.client.InsertContext;
58 import freenet.client.InsertException;
59 import freenet.client.async.BaseClientPutter;
60 import freenet.client.async.ClientContext;
61 import freenet.client.async.ClientPutCallback;
62 import freenet.client.async.ClientPutter;
63 import freenet.client.async.USKCallback;
64 import freenet.client.async.USKManager;
65 import freenet.keys.FreenetURI;
66 import freenet.keys.InsertableClientSSK;
67 import freenet.keys.USK;
68 import freenet.node.Node;
69 import freenet.node.RequestClient;
70 import freenet.node.RequestStarter;
71 import freenet.support.api.Bucket;
72 import freenet.support.io.ArrayBucket;
75 * Contains all necessary functionality for interacting with the Freenet node.
77 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
79 public class FreenetInterface {
82 private static final Logger logger = getLogger(FreenetInterface.class);
85 private final EventBus eventBus;
87 /** The node to interact with. */
88 private final Node node;
90 /** The high-level client to use for requests. */
91 private final HighLevelSimpleClient client;
92 private final RequestClient requestClient;
94 /** The USK callbacks. */
95 private final Map<String, USKCallback> soneUskCallbacks = new HashMap<String, USKCallback>();
97 /** The not-Sone-related USK callbacks. */
98 private final Map<FreenetURI, USKCallback> uriUskCallbacks = synchronizedMap(new HashMap<FreenetURI, USKCallback>());
99 private USKManager uskManager;
102 * Creates a new Freenet interface.
107 * The node to interact with
110 public FreenetInterface(EventBus eventBus, Node node) {
111 this.eventBus = eventBus;
113 this.client = node.clientCore.makeClient(INTERACTIVE_PRIORITY_CLASS, false, true);
114 this.requestClient = (HighLevelSimpleClientImpl) client;
115 this.uskManager = node.clientCore.uskManager;
119 public FreenetInterface(EventBus eventBus, Node node, HighLevelSimpleClient highLevelSimpleClient, RequestClient requestClient, USKManager uskManager) {
120 this.eventBus = eventBus;
122 this.client = highLevelSimpleClient;
123 this.requestClient = requestClient;
124 this.uskManager = uskManager;
132 * Fetches the given URI.
136 * @return The result of the fetch, or {@code null} if an error occured
138 public Fetched fetchUri(FreenetURI uri) {
139 FetchResult fetchResult = null;
140 FreenetURI currentUri = new FreenetURI(uri);
143 fetchResult = client.fetch(currentUri);
144 return new Fetched(currentUri, fetchResult);
145 } catch (FetchException fe1) {
146 if (fe1.getMode() == PERMANENT_REDIRECT) {
147 currentUri = fe1.newURI;
150 logger.log(WARNING, format("Could not fetch “%s”!", uri), fe1);
157 * Creates a key pair.
159 * @return The request key at index 0, the insert key at index 1
161 public String[] generateKeyPair() {
162 FreenetURI[] keyPair = client.generateKeyPair("");
163 return new String[] { keyPair[1].toString(), keyPair[0].toString() };
167 * Inserts the image data of the given {@link TemporaryImage} and returns
168 * the given insert token that can be used to add listeners or cancel the
171 * @param temporaryImage
172 * The temporary image data
177 * @throws SoneException
178 * if the insert could not be started
180 public void insertImage(TemporaryImage temporaryImage, Image image, InsertToken insertToken) throws SoneException {
181 String filenameHint = image.getId() + "." + temporaryImage.getMimeType().substring(temporaryImage.getMimeType().lastIndexOf("/") + 1);
182 InsertableClientSSK key = InsertableClientSSK.createRandom(node.random, "");
183 FreenetURI targetUri = key.getInsertURI().setDocName(filenameHint);
184 InsertContext insertContext = client.getInsertContext(true);
185 Bucket bucket = new ArrayBucket(temporaryImage.getImageData());
186 ClientMetadata metadata = new ClientMetadata(temporaryImage.getMimeType());
187 InsertBlock insertBlock = new InsertBlock(bucket, metadata, targetUri);
189 ClientPutter clientPutter = client.insert(insertBlock, false, null, false, insertContext, insertToken, INTERACTIVE_PRIORITY_CLASS);
190 insertToken.setClientPutter(clientPutter);
191 } catch (InsertException ie1) {
192 throw new SoneInsertException("Could not start image insert.", ie1);
197 * Inserts a directory into Freenet.
201 * @param manifestEntries
202 * The directory entries
204 * The name of the default file
205 * @return The generated URI
206 * @throws SoneException
207 * if an insert error occurs
209 public FreenetURI insertDirectory(FreenetURI insertUri, HashMap<String, Object> manifestEntries, String defaultFile) throws SoneException {
211 return client.insertManifest(insertUri, manifestEntries, defaultFile);
212 } catch (InsertException ie1) {
213 throw new SoneException(ie1);
218 * Registers the USK for the given Sone and notifies the given
219 * {@link SoneDownloader} if an update was found.
223 * @param soneDownloader
224 * The Sone download to notify on updates
226 public void registerUsk(final Sone sone, final SoneDownloader soneDownloader) {
228 logger.log(FINE, format("Registering Sone “%s” for USK updates at %s…", sone, TO_FREENET_URI.apply(sone).setMetaString(new String[]{"sone.xml"})));
229 USKCallback uskCallback = new NewEditionFound(sone, soneDownloader);
230 soneUskCallbacks.put(sone.getId(), uskCallback);
231 boolean runBackgroundFetch = (System.currentTimeMillis() - sone.getTime()) < DAYS.toMillis(7);
232 uskManager.subscribe(USK.create(TO_FREENET_URI.apply(sone)), uskCallback, runBackgroundFetch, requestClient);
233 } catch (MalformedURLException mue1) {
234 logger.log(WARNING, format("Could not subscribe USK “%s”!", TO_FREENET_URI.apply(sone)), mue1);
239 * Unsubscribes the request URI of the given Sone.
242 * The Sone to unregister
244 public void unregisterUsk(Sone sone) {
245 USKCallback uskCallback = soneUskCallbacks.remove(sone.getId());
246 if (uskCallback == null) {
250 logger.log(FINEST, format("Unsubscribing from USK for %s…", sone));
251 uskManager.unsubscribe(USK.create(TO_FREENET_URI.apply(sone)), uskCallback);
252 } catch (MalformedURLException mue1) {
253 logger.log(FINE, format("Could not unsubscribe USK “%s”!", TO_FREENET_URI.apply(sone)), mue1);
258 * Registers an arbitrary URI and calls the given callback if a new edition
264 * The callback to call
266 public void registerUsk(FreenetURI uri, final Callback callback) {
267 USKCallback uskCallback = new CallbackWrapper(callback);
269 uskManager.subscribe(USK.create(uri), uskCallback, true, requestClient);
270 uriUskCallbacks.put(uri, uskCallback);
271 } catch (MalformedURLException mue1) {
272 logger.log(WARNING, format("Could not subscribe to USK: %s", uri), mue1);
277 * Unregisters the USK watcher for the given URI.
280 * The URI to unregister the USK watcher for
282 public void unregisterUsk(FreenetURI uri) {
283 USKCallback uskCallback = uriUskCallbacks.remove(uri);
284 if (uskCallback == null) {
285 logger.log(INFO, format("Could not unregister unknown USK: %s", uri));
289 uskManager.unsubscribe(USK.create(uri), uskCallback);
290 } catch (MalformedURLException mue1) {
291 logger.log(INFO, format("Could not unregister invalid USK: %s", uri), mue1);
296 * Container for a fetched URI and the {@link FetchResult}.
298 * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
300 public static class Fetched {
302 /** The fetched URI. */
303 private final FreenetURI freenetUri;
305 /** The fetch result. */
306 private final FetchResult fetchResult;
309 * Creates a new fetched URI.
312 * The URI that was fetched
316 public Fetched(FreenetURI freenetUri, FetchResult fetchResult) {
317 this.freenetUri = freenetUri;
318 this.fetchResult = fetchResult;
326 * Returns the fetched URI.
328 * @return The fetched URI
330 public FreenetURI getFreenetUri() {
335 * Returns the fetch result.
337 * @return The fetch result
339 public FetchResult getFetchResult() {
346 * Callback for USK watcher events.
348 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
350 public static interface Callback {
353 * Notifies a listener that a new edition was found for a URI.
356 * The URI that a new edition was found for
359 * @param newKnownGood
360 * Whether the found edition was actually fetched
362 * Whether the found edition is higher than all previously
365 public void editionFound(FreenetURI uri, long edition, boolean newKnownGood, boolean newSlot);
369 private static class NewEditionFound implements USKCallback {
371 private final Sone sone;
372 private final SoneDownloader soneDownloader;
374 public NewEditionFound(Sone sone, SoneDownloader soneDownloader) {
376 this.soneDownloader = soneDownloader;
380 @SuppressWarnings("synthetic-access")
381 public void onFoundEdition(long edition, USK key, ObjectContainer objectContainer, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
382 logger.log(FINE, format("Found USK update for Sone “%s” at %s, new known good: %s, new slot too: %s.", sone, key, newKnownGood, newSlotToo));
383 if (edition > sone.getLatestEdition()) {
384 sone.modify().setLatestEdition(edition).update();
385 new Thread(new Runnable() {
389 soneDownloader.fetchSone(sone);
391 }, format("Sone Downloader for %s", sone.getId())).start();
396 public short getPollingPriorityProgress() {
397 return INTERACTIVE_PRIORITY_CLASS;
401 public short getPollingPriorityNormal() {
402 return INTERACTIVE_PRIORITY_CLASS;
406 private static class CallbackWrapper implements USKCallback {
408 private final Callback callback;
410 public CallbackWrapper(Callback callback) {
411 this.callback = callback;
415 public void onFoundEdition(long edition, USK key, ObjectContainer objectContainer, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
416 callback.editionFound(key.getURI(), edition, newKnownGood, newSlotToo);
420 public short getPollingPriorityNormal() {
421 return PREFETCH_PRIORITY_CLASS;
425 public short getPollingPriorityProgress() {
426 return INTERACTIVE_PRIORITY_CLASS;
432 * Insert token that can cancel a running insert and sends events.
434 * @see ImageInsertAbortedEvent
435 * @see ImageInsertStartedEvent
436 * @see ImageInsertFailedEvent
437 * @see ImageInsertFinishedEvent
438 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
440 public class InsertToken implements ClientPutCallback {
442 /** The image being inserted. */
443 private final Image image;
445 /** The client putter. */
446 private ClientPutter clientPutter;
448 /** The final URI. */
449 private volatile FreenetURI resultingUri;
452 * Creates a new insert token for the given image.
455 * The image being inserted
457 public InsertToken(Image image) {
466 * Sets the client putter that is inserting the image. This will also
467 * signal all registered listeners that the image has started.
469 * @param clientPutter
472 @SuppressWarnings("synthetic-access")
473 public void setClientPutter(ClientPutter clientPutter) {
474 this.clientPutter = clientPutter;
475 eventBus.post(new ImageInsertStartedEvent(image));
483 * Cancels the running insert.
485 @SuppressWarnings("synthetic-access")
486 public void cancel() {
487 clientPutter.cancel(null, node.clientCore.clientContext);
488 eventBus.post(new ImageInsertAbortedEvent(image));
492 // INTERFACE ClientPutCallback
496 public void onMajorProgress(ObjectContainer objectContainer) {
497 /* ignore, we don’t care. */
501 @SuppressWarnings("synthetic-access")
502 public void onFailure(InsertException insertException, BaseClientPutter clientPutter, ObjectContainer objectContainer) {
503 if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) {
504 eventBus.post(new ImageInsertAbortedEvent(image));
506 eventBus.post(new ImageInsertFailedEvent(image, insertException));
511 public void onFetchable(BaseClientPutter clientPutter, ObjectContainer objectContainer) {
512 /* ignore, we don’t care. */
516 public void onGeneratedMetadata(Bucket metadata, BaseClientPutter clientPutter, ObjectContainer objectContainer) {
517 /* ignore, we don’t care. */
521 public void onGeneratedURI(FreenetURI generatedUri, BaseClientPutter clientPutter, ObjectContainer objectContainer) {
522 resultingUri = generatedUri;
526 @SuppressWarnings("synthetic-access")
527 public void onSuccess(BaseClientPutter clientPutter, ObjectContainer objectContainer) {
528 eventBus.post(new ImageInsertFinishedEvent(image, resultingUri));