2 * Sone - FreenetInterface.java - Copyright © 2010–2016 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.keys.USK.create;
21 import static java.lang.String.format;
22 import static java.util.logging.Level.WARNING;
23 import static java.util.logging.Logger.getLogger;
24 import static net.pterodactylus.sone.freenet.Key.routingKey;
26 import java.io.IOException;
27 import java.net.MalformedURLException;
28 import java.util.Collections;
29 import java.util.HashMap;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
34 import javax.annotation.Nonnull;
36 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
37 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
38 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
39 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
40 import net.pterodactylus.sone.data.Image;
41 import net.pterodactylus.sone.data.Sone;
42 import net.pterodactylus.sone.data.TemporaryImage;
44 import com.google.common.base.Function;
45 import com.google.common.eventbus.EventBus;
46 import com.google.inject.Inject;
47 import com.google.inject.Singleton;
49 import freenet.client.ClientMetadata;
50 import freenet.client.FetchContext;
51 import freenet.client.FetchException;
52 import freenet.client.FetchException.FetchExceptionMode;
53 import freenet.client.FetchResult;
54 import freenet.client.HighLevelSimpleClient;
55 import freenet.client.InsertBlock;
56 import freenet.client.InsertContext;
57 import freenet.client.InsertException;
58 import freenet.client.Metadata;
59 import freenet.client.async.BaseClientPutter;
60 import freenet.client.async.ClientContext;
61 import freenet.client.async.ClientGetCallback;
62 import freenet.client.async.ClientGetter;
63 import freenet.client.async.ClientPutCallback;
64 import freenet.client.async.ClientPutter;
65 import freenet.client.async.SnoopMetadata;
66 import freenet.client.async.USKCallback;
67 import freenet.keys.FreenetURI;
68 import freenet.keys.InsertableClientSSK;
69 import freenet.keys.USK;
70 import freenet.node.Node;
71 import freenet.node.RequestClient;
72 import freenet.node.RequestClientBuilder;
73 import freenet.node.RequestStarter;
74 import freenet.support.api.Bucket;
75 import freenet.support.api.RandomAccessBucket;
76 import freenet.support.io.ArrayBucket;
77 import freenet.support.io.ResumeFailedException;
80 * Contains all necessary functionality for interacting with the Freenet node.
82 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
85 public class FreenetInterface {
88 private static final Logger logger = getLogger(FreenetInterface.class.getName());
91 private final EventBus eventBus;
93 /** The node to interact with. */
94 private final Node node;
96 /** The high-level client to use for requests. */
97 private final HighLevelSimpleClient client;
99 /** The USK callbacks. */
100 private final Map<String, USKCallback> soneUskCallbacks = new HashMap<String, USKCallback>();
102 /** The not-Sone-related USK callbacks. */
103 private final Map<FreenetURI, USKCallback> uriUskCallbacks = Collections.synchronizedMap(new HashMap<FreenetURI, USKCallback>());
105 private final RequestClient imageInserts = new RequestClientBuilder().realTime().build();
106 private final RequestClient imageLoader = new RequestClientBuilder().realTime().build();
109 * Creates a new Freenet interface.
114 * The node to interact with
117 public FreenetInterface(EventBus eventBus, Node node) {
118 this.eventBus = eventBus;
120 this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true);
128 * Fetches the given URI.
132 * @return The result of the fetch, or {@code null} if an error occured
134 public Fetched fetchUri(FreenetURI uri) {
135 FreenetURI currentUri = new FreenetURI(uri);
138 FetchResult fetchResult = client.fetch(currentUri);
139 return new Fetched(currentUri, fetchResult);
140 } catch (FetchException fe1) {
141 if (fe1.getMode() == FetchExceptionMode.PERMANENT_REDIRECT) {
142 currentUri = fe1.newURI;
145 logger.log(Level.WARNING, String.format("Could not fetch “%s”!", uri), fe1);
151 public void startFetch(final FreenetURI uri, final BackgroundFetchCallback backgroundFetchCallback) {
152 ClientGetCallback callback = new ClientGetCallback() {
154 public void onSuccess(FetchResult result, ClientGetter state) {
156 backgroundFetchCallback.loaded(uri, result.getMimeType(), result.asByteArray());
157 } catch (IOException e) {
158 backgroundFetchCallback.failed(uri);
163 public void onFailure(FetchException e, ClientGetter state) {
164 backgroundFetchCallback.failed(uri);
168 public void onResume(ClientContext context) throws ResumeFailedException {
173 public RequestClient getRequestClient() {
177 SnoopMetadata snoop = new SnoopMetadata() {
179 public boolean snoopMetadata(Metadata meta, ClientContext context) {
180 String mimeType = meta.getMIMEType();
181 boolean cancel = (mimeType == null) || backgroundFetchCallback.shouldCancel(uri, mimeType, meta.dataLength());
183 backgroundFetchCallback.failed(uri);
188 FetchContext fetchContext = client.getFetchContext();
190 ClientGetter clientGetter = client.fetch(uri, 2097152, callback, fetchContext, RequestStarter.INTERACTIVE_PRIORITY_CLASS);
191 clientGetter.setMetaSnoop(snoop);
192 clientGetter.restart(uri, fetchContext.filterData, node.clientCore.clientContext);
193 } catch (FetchException fe) {
194 /* stupid exception that can not actually be thrown! */
198 public interface BackgroundFetchCallback {
199 boolean shouldCancel(@Nonnull FreenetURI uri, @Nonnull String mimeType, long size);
200 void loaded(@Nonnull FreenetURI uri, @Nonnull String mimeType, @Nonnull byte[] data);
201 void failed(@Nonnull FreenetURI uri);
205 * Inserts the image data of the given {@link TemporaryImage} and returns
206 * the given insert token that can be used to add listeners or cancel the
209 * @param temporaryImage
210 * The temporary image data
215 * @throws SoneException
216 * if the insert could not be started
218 public void insertImage(TemporaryImage temporaryImage, Image image, InsertToken insertToken) throws SoneException {
219 String filenameHint = image.getId() + "." + temporaryImage.getMimeType().substring(temporaryImage.getMimeType().lastIndexOf("/") + 1);
220 InsertableClientSSK key = InsertableClientSSK.createRandom(node.random, "");
221 FreenetURI targetUri = key.getInsertURI().setDocName(filenameHint);
222 InsertContext insertContext = client.getInsertContext(true);
223 RandomAccessBucket bucket = new ArrayBucket(temporaryImage.getImageData());
224 insertToken.setBucket(bucket);
225 ClientMetadata metadata = new ClientMetadata(temporaryImage.getMimeType());
226 InsertBlock insertBlock = new InsertBlock(bucket, metadata, targetUri);
228 ClientPutter clientPutter = client.insert(insertBlock, null, false, insertContext, insertToken, RequestStarter.INTERACTIVE_PRIORITY_CLASS);
229 insertToken.setClientPutter(clientPutter);
230 } catch (InsertException ie1) {
231 throw new SoneInsertException("Could not start image insert.", ie1);
236 * Inserts a directory into Freenet.
240 * @param manifestEntries
241 * The directory entries
243 * The name of the default file
244 * @return The generated URI
245 * @throws SoneException
246 * if an insert error occurs
248 public FreenetURI insertDirectory(FreenetURI insertUri, HashMap<String, Object> manifestEntries, String defaultFile) throws SoneException {
250 return client.insertManifest(insertUri, manifestEntries, defaultFile);
251 } catch (InsertException ie1) {
252 throw new SoneException(ie1);
256 public void registerActiveUsk(FreenetURI requestUri,
257 USKCallback uskCallback) {
259 soneUskCallbacks.put(routingKey(requestUri), uskCallback);
260 node.clientCore.uskManager.subscribe(create(requestUri),
261 uskCallback, true, (RequestClient) client);
262 } catch (MalformedURLException mue1) {
263 logger.log(WARNING, format("Could not subscribe USK “%s”!",
268 public void registerPassiveUsk(FreenetURI requestUri,
269 USKCallback uskCallback) {
271 soneUskCallbacks.put(routingKey(requestUri), uskCallback);
274 .subscribe(create(requestUri), uskCallback, false,
275 (RequestClient) client);
276 } catch (MalformedURLException mue1) {
278 format("Could not subscribe USK “%s”!", requestUri),
284 * Unsubscribes the request URI of the given Sone.
287 * The Sone to unregister
289 public void unregisterUsk(Sone sone) {
290 USKCallback uskCallback = soneUskCallbacks.remove(sone.getId());
291 if (uskCallback == null) {
295 logger.log(Level.FINEST, String.format("Unsubscribing from USK for %s…", sone));
296 node.clientCore.uskManager.unsubscribe(USK.create(sone.getRequestUri()), uskCallback);
297 } catch (MalformedURLException mue1) {
298 logger.log(Level.FINE, String.format("Could not unsubscribe USK “%s”!", sone.getRequestUri()), mue1);
303 * Registers an arbitrary URI and calls the given callback if a new edition
309 * The callback to call
311 public void registerUsk(FreenetURI uri, final Callback callback) {
312 USKCallback uskCallback = new USKCallback() {
315 public void onFoundEdition(long edition, USK key, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
316 callback.editionFound(key.getURI(), edition, newKnownGood, newSlotToo);
320 public short getPollingPriorityNormal() {
321 return RequestStarter.PREFETCH_PRIORITY_CLASS;
325 public short getPollingPriorityProgress() {
326 return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
331 node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, (RequestClient) client);
332 uriUskCallbacks.put(uri, uskCallback);
333 } catch (MalformedURLException mue1) {
334 logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1);
339 * Unregisters the USK watcher for the given URI.
342 * The URI to unregister the USK watcher for
344 public void unregisterUsk(FreenetURI uri) {
345 USKCallback uskCallback = uriUskCallbacks.remove(uri);
346 if (uskCallback == null) {
347 logger.log(Level.INFO, String.format("Could not unregister unknown USK: %s", uri));
351 node.clientCore.uskManager.unsubscribe(USK.create(uri), uskCallback);
352 } catch (MalformedURLException mue1) {
353 logger.log(Level.INFO, String.format("Could not unregister invalid USK: %s", uri), mue1);
358 * Container for a fetched URI and the {@link FetchResult}.
360 * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
362 public static class Fetched {
364 /** The fetched URI. */
365 private final FreenetURI freenetUri;
367 /** The fetch result. */
368 private final FetchResult fetchResult;
371 * Creates a new fetched URI.
374 * The URI that was fetched
378 public Fetched(FreenetURI freenetUri, FetchResult fetchResult) {
379 this.freenetUri = freenetUri;
380 this.fetchResult = fetchResult;
388 * Returns the fetched URI.
390 * @return The fetched URI
392 public FreenetURI getFreenetUri() {
397 * Returns the fetch result.
399 * @return The fetch result
401 public FetchResult getFetchResult() {
408 * Callback for USK watcher events.
410 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
412 public static interface Callback {
415 * Notifies a listener that a new edition was found for a URI.
418 * The URI that a new edition was found for
421 * @param newKnownGood
422 * Whether the found edition was actually fetched
424 * Whether the found edition is higher than all previously
427 public void editionFound(FreenetURI uri, long edition, boolean newKnownGood, boolean newSlot);
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;
447 private Bucket bucket;
449 /** The final URI. */
450 private volatile FreenetURI resultingUri;
453 * Creates a new insert token for the given image.
456 * The image being inserted
458 public InsertToken(Image image) {
467 * Sets the client putter that is inserting the image. This will also
468 * signal all registered listeners that the image has started.
470 * @param clientPutter
473 @SuppressWarnings("synthetic-access")
474 public void setClientPutter(ClientPutter clientPutter) {
475 this.clientPutter = clientPutter;
476 eventBus.post(new ImageInsertStartedEvent(image));
479 public void setBucket(Bucket bucket) {
480 this.bucket = bucket;
488 * Cancels the running insert.
490 @SuppressWarnings("synthetic-access")
491 public void cancel() {
492 clientPutter.cancel(node.clientCore.clientContext);
493 eventBus.post(new ImageInsertAbortedEvent(image));
498 // INTERFACE ClientPutCallback
502 public RequestClient getRequestClient() {
507 public void onResume(ClientContext context) throws ResumeFailedException {
515 @SuppressWarnings("synthetic-access")
516 public void onFailure(InsertException insertException, BaseClientPutter clientPutter) {
517 if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) {
518 eventBus.post(new ImageInsertAbortedEvent(image));
520 eventBus.post(new ImageInsertFailedEvent(image, insertException));
529 public void onFetchable(BaseClientPutter clientPutter) {
530 /* ignore, we don’t care. */
537 public void onGeneratedMetadata(Bucket metadata, BaseClientPutter clientPutter) {
538 /* ignore, we don’t care. */
545 public void onGeneratedURI(FreenetURI generatedUri, BaseClientPutter clientPutter) {
546 resultingUri = generatedUri;
553 @SuppressWarnings("synthetic-access")
554 public void onSuccess(BaseClientPutter clientPutter) {
555 eventBus.post(new ImageInsertFinishedEvent(image, resultingUri));
561 public class InsertTokenSupplier implements Function<Image, InsertToken> {
564 public InsertToken apply(Image image) {
565 return new InsertToken(image);