X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterface.java;h=946371bcf3b3c6a91e4c43823fad254dadaeb1a0;hb=648e6f419534c315ade5a506e8ec8820872ee7c8;hp=30ba69a23e00f36e24de7b35a9e2d163cddb5a27;hpb=b29cf0908e6dfd2b55220a3a7e44200f2fe5b19e;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java
index 30ba69a..946371b 100644
--- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java
+++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java
@@ -1,5 +1,5 @@
/*
- * FreenetSone - FreenetInterface.java - Copyright © 2010 David Roden
+ * Sone - FreenetInterface.java - Copyright © 2010â2016 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -17,38 +17,78 @@
package net.pterodactylus.sone.core;
+import static freenet.keys.USK.create;
+import static java.lang.String.format;
+import static java.util.logging.Level.WARNING;
+import static java.util.logging.Logger.getLogger;
+import static net.pterodactylus.sone.freenet.Key.routingKey;
+
+import java.io.IOException;
import java.net.MalformedURLException;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.annotation.Nonnull;
+
+import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
+import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
+import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
+import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
+import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.util.logging.Logging;
+import net.pterodactylus.sone.data.TemporaryImage;
-import com.db4o.ObjectContainer;
+import com.google.common.base.Function;
+import com.google.common.eventbus.EventBus;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import freenet.client.ClientMetadata;
+import freenet.client.FetchContext;
import freenet.client.FetchException;
+import freenet.client.FetchException.FetchExceptionMode;
import freenet.client.FetchResult;
import freenet.client.HighLevelSimpleClient;
-import freenet.client.HighLevelSimpleClientImpl;
+import freenet.client.InsertBlock;
+import freenet.client.InsertContext;
import freenet.client.InsertException;
+import freenet.client.Metadata;
+import freenet.client.async.BaseClientPutter;
import freenet.client.async.ClientContext;
+import freenet.client.async.ClientGetCallback;
+import freenet.client.async.ClientGetter;
+import freenet.client.async.ClientPutCallback;
+import freenet.client.async.ClientPutter;
+import freenet.client.async.SnoopMetadata;
import freenet.client.async.USKCallback;
import freenet.keys.FreenetURI;
+import freenet.keys.InsertableClientSSK;
import freenet.keys.USK;
import freenet.node.Node;
+import freenet.node.RequestClient;
+import freenet.node.RequestClientBuilder;
import freenet.node.RequestStarter;
+import freenet.support.api.Bucket;
+import freenet.support.api.RandomAccessBucket;
+import freenet.support.io.ArrayBucket;
+import freenet.support.io.ResumeFailedException;
/**
* Contains all necessary functionality for interacting with the Freenet node.
*
* @author David âBombeâ Roden
*/
+@Singleton
public class FreenetInterface {
/** The logger. */
- private static final Logger logger = Logging.getLogger(FreenetInterface.class);
+ private static final Logger logger = getLogger(FreenetInterface.class.getName());
+
+ /** The event bus. */
+ private final EventBus eventBus;
/** The node to interact with. */
private final Node node;
@@ -59,17 +99,25 @@ public class FreenetInterface {
/** The USK callbacks. */
private final Map soneUskCallbacks = new HashMap();
+ /** The not-Sone-related USK callbacks. */
+ private final Map uriUskCallbacks = Collections.synchronizedMap(new HashMap());
+
+ private final RequestClient imageInserts = new RequestClientBuilder().realTime().build();
+ private final RequestClient imageLoader = new RequestClientBuilder().realTime().build();
+
/**
* Creates a new Freenet interface.
*
+ * @param eventBus
+ * The event bus
* @param node
* The node to interact with
- * @param client
- * The high-level client
*/
- public FreenetInterface(Node node, HighLevelSimpleClient client) {
+ @Inject
+ public FreenetInterface(EventBus eventBus, Node node) {
+ this.eventBus = eventBus;
this.node = node;
- this.client = client;
+ this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true);
}
//
@@ -83,32 +131,105 @@ public class FreenetInterface {
* The URI to fetch
* @return The result of the fetch, or {@code null} if an error occured
*/
- public FetchResult fetchUri(FreenetURI uri) {
- FetchResult fetchResult = null;
+ public Fetched fetchUri(FreenetURI uri) {
FreenetURI currentUri = new FreenetURI(uri);
while (true) {
try {
- fetchResult = client.fetch(currentUri);
- return fetchResult;
+ FetchResult fetchResult = client.fetch(currentUri);
+ return new Fetched(currentUri, fetchResult);
} catch (FetchException fe1) {
- if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) {
+ if (fe1.getMode() == FetchExceptionMode.PERMANENT_REDIRECT) {
currentUri = fe1.newURI;
continue;
}
- logger.log(Level.WARNING, "Could not fetch â" + uri + "â!", fe1);
+ logger.log(Level.WARNING, String.format("Could not fetch â%sâ!", uri), fe1);
return null;
}
}
}
+ public void startFetch(final FreenetURI uri, final BackgroundFetchCallback backgroundFetchCallback) {
+ ClientGetCallback callback = new ClientGetCallback() {
+ @Override
+ public void onSuccess(FetchResult result, ClientGetter state) {
+ try {
+ backgroundFetchCallback.loaded(uri, result.getMimeType(), result.asByteArray());
+ } catch (IOException e) {
+ backgroundFetchCallback.failed(uri);
+ }
+ }
+
+ @Override
+ public void onFailure(FetchException e, ClientGetter state) {
+ backgroundFetchCallback.failed(uri);
+ }
+
+ @Override
+ public void onResume(ClientContext context) throws ResumeFailedException {
+ /* do nothing. */
+ }
+
+ @Override
+ public RequestClient getRequestClient() {
+ return imageLoader;
+ }
+ };
+ SnoopMetadata snoop = new SnoopMetadata() {
+ @Override
+ public boolean snoopMetadata(Metadata meta, ClientContext context) {
+ String mimeType = meta.getMIMEType();
+ boolean cancel = (mimeType == null) || backgroundFetchCallback.shouldCancel(uri, mimeType, meta.dataLength());
+ if (cancel) {
+ backgroundFetchCallback.failed(uri);
+ }
+ return cancel;
+ }
+ };
+ FetchContext fetchContext = client.getFetchContext();
+ try {
+ ClientGetter clientGetter = client.fetch(uri, 2097152, callback, fetchContext, RequestStarter.INTERACTIVE_PRIORITY_CLASS);
+ clientGetter.setMetaSnoop(snoop);
+ clientGetter.restart(uri, fetchContext.filterData, node.clientCore.clientContext);
+ } catch (FetchException fe) {
+ /* stupid exception that can not actually be thrown! */
+ }
+ }
+
+ public interface BackgroundFetchCallback {
+ boolean shouldCancel(@Nonnull FreenetURI uri, @Nonnull String mimeType, long size);
+ void loaded(@Nonnull FreenetURI uri, @Nonnull String mimeType, @Nonnull byte[] data);
+ void failed(@Nonnull FreenetURI uri);
+ }
+
/**
- * Creates a key pair.
+ * Inserts the image data of the given {@link TemporaryImage} and returns
+ * the given insert token that can be used to add listeners or cancel the
+ * insert.
*
- * @return The request key at index 0, the insert key at index 1
+ * @param temporaryImage
+ * The temporary image data
+ * @param image
+ * The image
+ * @param insertToken
+ * The insert token
+ * @throws SoneException
+ * if the insert could not be started
*/
- public String[] generateKeyPair() {
- FreenetURI[] keyPair = client.generateKeyPair("");
- return new String[] { keyPair[1].toString(), keyPair[0].toString() };
+ public void insertImage(TemporaryImage temporaryImage, Image image, InsertToken insertToken) throws SoneException {
+ String filenameHint = image.getId() + "." + temporaryImage.getMimeType().substring(temporaryImage.getMimeType().lastIndexOf("/") + 1);
+ InsertableClientSSK key = InsertableClientSSK.createRandom(node.random, "");
+ FreenetURI targetUri = key.getInsertURI().setDocName(filenameHint);
+ InsertContext insertContext = client.getInsertContext(true);
+ RandomAccessBucket bucket = new ArrayBucket(temporaryImage.getImageData());
+ insertToken.setBucket(bucket);
+ ClientMetadata metadata = new ClientMetadata(temporaryImage.getMimeType());
+ InsertBlock insertBlock = new InsertBlock(bucket, metadata, targetUri);
+ try {
+ ClientPutter clientPutter = client.insert(insertBlock, null, false, insertContext, insertToken, RequestStarter.INTERACTIVE_PRIORITY_CLASS);
+ insertToken.setClientPutter(clientPutter);
+ } catch (InsertException ie1) {
+ throw new SoneInsertException("Could not start image insert.", ie1);
+ }
}
/**
@@ -128,48 +249,34 @@ public class FreenetInterface {
try {
return client.insertManifest(insertUri, manifestEntries, defaultFile);
} catch (InsertException ie1) {
- throw new SoneException(null, ie1);
+ throw new SoneException(ie1);
}
}
- /**
- * Registers the USK for the given Sone and notifies the given
- * {@link SoneDownloader} if an update was found.
- *
- * @param sone
- * The Sone to watch
- * @param soneDownloader
- * The Sone download to notify on updates
- */
- public void registerUsk(final Sone sone, final SoneDownloader soneDownloader) {
+ public void registerActiveUsk(FreenetURI requestUri,
+ USKCallback uskCallback) {
try {
- logger.log(Level.FINE, "Registering Sone â%sâ for USK updates at %sâ¦", new Object[] { sone, sone.getRequestUri().setMetaString(new String[] { "sone.xml" }) });
- USKCallback uskCallback = new USKCallback() {
-
- @Override
- @SuppressWarnings("synthetic-access")
- public void onFoundEdition(long edition, USK key, ObjectContainer objectContainer, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
- logger.log(Level.FINE, "Found USK update for Sone â%sâ at %s, new known good: %s, new slot too: %s.", new Object[] { sone, key, newKnownGood, newSlotToo });
- if (newKnownGood) {
- sone.updateUris(key.suggestedEdition);
- soneDownloader.fetchSone(sone);
- }
- }
-
- @Override
- public short getPollingPriorityProgress() {
- return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
- }
+ soneUskCallbacks.put(routingKey(requestUri), uskCallback);
+ node.clientCore.uskManager.subscribe(create(requestUri),
+ uskCallback, true, (RequestClient) client);
+ } catch (MalformedURLException mue1) {
+ logger.log(WARNING, format("Could not subscribe USK â%sâ!",
+ requestUri), mue1);
+ }
+ }
- @Override
- public short getPollingPriorityNormal() {
- return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
- }
- };
- soneUskCallbacks.put(sone.getId(), uskCallback);
- node.clientCore.uskManager.subscribe(USK.create(sone.getRequestUri()), uskCallback, true, (HighLevelSimpleClientImpl) client);
+ public void registerPassiveUsk(FreenetURI requestUri,
+ USKCallback uskCallback) {
+ try {
+ soneUskCallbacks.put(routingKey(requestUri), uskCallback);
+ node.clientCore
+ .uskManager
+ .subscribe(create(requestUri), uskCallback, false,
+ (RequestClient) client);
} catch (MalformedURLException mue1) {
- logger.log(Level.WARNING, "Could not subscribe USK â" + sone.getRequestUri() + "â!", mue1);
+ logger.log(WARNING,
+ format("Could not subscribe USK â%sâ!", requestUri),
+ mue1);
}
}
@@ -185,11 +292,279 @@ public class FreenetInterface {
return;
}
try {
- logger.log(Level.FINEST, "Unsubscribing from USK for %sâ¦", new Object[] { sone });
+ logger.log(Level.FINEST, String.format("Unsubscribing from USK for %sâ¦", sone));
node.clientCore.uskManager.unsubscribe(USK.create(sone.getRequestUri()), uskCallback);
} catch (MalformedURLException mue1) {
- logger.log(Level.FINE, "Could not unsubscribe USK â" + sone.getRequestUri() + "â!", mue1);
+ logger.log(Level.FINE, String.format("Could not unsubscribe USK â%sâ!", sone.getRequestUri()), mue1);
+ }
+ }
+
+ /**
+ * Registers an arbitrary URI and calls the given callback if a new edition
+ * is found.
+ *
+ * @param uri
+ * The URI to watch
+ * @param callback
+ * The callback to call
+ */
+ public void registerUsk(FreenetURI uri, final Callback callback) {
+ USKCallback uskCallback = new USKCallback() {
+
+ @Override
+ public void onFoundEdition(long edition, USK key, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
+ callback.editionFound(key.getURI(), edition, newKnownGood, newSlotToo);
+ }
+
+ @Override
+ public short getPollingPriorityNormal() {
+ return RequestStarter.PREFETCH_PRIORITY_CLASS;
+ }
+
+ @Override
+ public short getPollingPriorityProgress() {
+ return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
+ }
+
+ };
+ try {
+ node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, (RequestClient) client);
+ uriUskCallbacks.put(uri, uskCallback);
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1);
+ }
+ }
+
+ /**
+ * Unregisters the USK watcher for the given URI.
+ *
+ * @param uri
+ * The URI to unregister the USK watcher for
+ */
+ public void unregisterUsk(FreenetURI uri) {
+ USKCallback uskCallback = uriUskCallbacks.remove(uri);
+ if (uskCallback == null) {
+ logger.log(Level.INFO, String.format("Could not unregister unknown USK: %s", uri));
+ return;
}
+ try {
+ node.clientCore.uskManager.unsubscribe(USK.create(uri), uskCallback);
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.INFO, String.format("Could not unregister invalid USK: %s", uri), mue1);
+ }
+ }
+
+ /**
+ * Container for a fetched URI and the {@link FetchResult}.
+ *
+ * @author David Roden
+ */
+ public static class Fetched {
+
+ /** The fetched URI. */
+ private final FreenetURI freenetUri;
+
+ /** The fetch result. */
+ private final FetchResult fetchResult;
+
+ /**
+ * Creates a new fetched URI.
+ *
+ * @param freenetUri
+ * The URI that was fetched
+ * @param fetchResult
+ * The fetch result
+ */
+ public Fetched(FreenetURI freenetUri, FetchResult fetchResult) {
+ this.freenetUri = freenetUri;
+ this.fetchResult = fetchResult;
+ }
+
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * Returns the fetched URI.
+ *
+ * @return The fetched URI
+ */
+ public FreenetURI getFreenetUri() {
+ return freenetUri;
+ }
+
+ /**
+ * Returns the fetch result.
+ *
+ * @return The fetch result
+ */
+ public FetchResult getFetchResult() {
+ return fetchResult;
+ }
+
+ }
+
+ /**
+ * Callback for USK watcher events.
+ *
+ * @author David âBombeâ Roden
+ */
+ public static interface Callback {
+
+ /**
+ * Notifies a listener that a new edition was found for a URI.
+ *
+ * @param uri
+ * The URI that a new edition was found for
+ * @param edition
+ * The found edition
+ * @param newKnownGood
+ * Whether the found edition was actually fetched
+ * @param newSlot
+ * Whether the found edition is higher than all previously
+ * found editions
+ */
+ public void editionFound(FreenetURI uri, long edition, boolean newKnownGood, boolean newSlot);
+
+ }
+
+ /**
+ * Insert token that can cancel a running insert and sends events.
+ *
+ * @see ImageInsertAbortedEvent
+ * @see ImageInsertStartedEvent
+ * @see ImageInsertFailedEvent
+ * @see ImageInsertFinishedEvent
+ * @author David âBombeâ Roden
+ */
+ public class InsertToken implements ClientPutCallback {
+
+ /** The image being inserted. */
+ private final Image image;
+
+ /** The client putter. */
+ private ClientPutter clientPutter;
+ private Bucket bucket;
+
+ /** The final URI. */
+ private volatile FreenetURI resultingUri;
+
+ /**
+ * Creates a new insert token for the given image.
+ *
+ * @param image
+ * The image being inserted
+ */
+ public InsertToken(Image image) {
+ this.image = image;
+ }
+
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * Sets the client putter that is inserting the image. This will also
+ * signal all registered listeners that the image has started.
+ *
+ * @param clientPutter
+ * The client putter
+ */
+ @SuppressWarnings("synthetic-access")
+ public void setClientPutter(ClientPutter clientPutter) {
+ this.clientPutter = clientPutter;
+ eventBus.post(new ImageInsertStartedEvent(image));
+ }
+
+ public void setBucket(Bucket bucket) {
+ this.bucket = bucket;
+ }
+
+ //
+ // ACTIONS
+ //
+
+ /**
+ * Cancels the running insert.
+ */
+ @SuppressWarnings("synthetic-access")
+ public void cancel() {
+ clientPutter.cancel(node.clientCore.clientContext);
+ eventBus.post(new ImageInsertAbortedEvent(image));
+ bucket.free();
+ }
+
+ //
+ // INTERFACE ClientPutCallback
+ //
+
+ @Override
+ public RequestClient getRequestClient() {
+ return imageInserts;
+ }
+
+ @Override
+ public void onResume(ClientContext context) throws ResumeFailedException {
+ /* ignore. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void onFailure(InsertException insertException, BaseClientPutter clientPutter) {
+ if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) {
+ eventBus.post(new ImageInsertAbortedEvent(image));
+ } else {
+ eventBus.post(new ImageInsertFailedEvent(image, insertException));
+ }
+ bucket.free();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void onFetchable(BaseClientPutter clientPutter) {
+ /* ignore, we donât care. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void onGeneratedMetadata(Bucket metadata, BaseClientPutter clientPutter) {
+ /* ignore, we donât care. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void onGeneratedURI(FreenetURI generatedUri, BaseClientPutter clientPutter) {
+ resultingUri = generatedUri;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void onSuccess(BaseClientPutter clientPutter) {
+ eventBus.post(new ImageInsertFinishedEvent(image, resultingUri));
+ bucket.free();
+ }
+
+ }
+
+ public class InsertTokenSupplier implements Function {
+
+ @Override
+ public InsertToken apply(Image image) {
+ return new InsertToken(image);
+ }
+
}
}