X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterface.java;h=f2cbec6930992a41dbe1291251e2ffeb62180d5a;hb=a97bd2a5a5d804d53f9fdf4ff3afa985b6eae2fb;hp=4ee3926c06e408908c75b037ab9fd8676edbfd60;hpb=f1032a3361ff1b4a43d94a84169e9fcafae4afff;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 4ee3926..f2cbec6 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 @@
/*
- * Sone - FreenetInterface.java - Copyright © 2010â2013 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,14 +17,23 @@
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.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.annotation.Nonnull;
+import javax.inject.Inject;
+
import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
@@ -32,30 +41,35 @@ import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.TemporaryImage;
-import net.pterodactylus.util.logging.Logging;
+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;
@@ -64,13 +78,12 @@ 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;
@@ -80,6 +93,7 @@ public class FreenetInterface {
/** The high-level client to use for requests. */
private final HighLevelSimpleClient client;
+ private final RequestClient requestClient = new RequestClientBuilder().realTime().build();
/** The USK callbacks. */
private final Map soneUskCallbacks = new HashMap();
@@ -87,17 +101,8 @@ public class FreenetInterface {
/** The not-Sone-related USK callbacks. */
private final Map uriUskCallbacks = Collections.synchronizedMap(new HashMap());
- private final RequestClient imageInserts = new RequestClient() {
- @Override
- public boolean persistent() {
- return false;
- }
-
- @Override
- public boolean realTimeFlag() {
- return true;
- }
- };
+ private final RequestClient imageInserts = new RequestClientBuilder().realTime().build();
+ private final RequestClient imageLoader = new RequestClientBuilder().realTime().build();
/**
* Creates a new Freenet interface.
@@ -126,11 +131,10 @@ public class FreenetInterface {
* @return The result of the fetch, or {@code null} if an error occured
*/
public Fetched fetchUri(FreenetURI uri) {
- FetchResult fetchResult = null;
FreenetURI currentUri = new FreenetURI(uri);
while (true) {
try {
- fetchResult = client.fetch(currentUri);
+ FetchResult fetchResult = client.fetch(currentUri);
return new Fetched(currentUri, fetchResult);
} catch (FetchException fe1) {
if (fe1.getMode() == FetchExceptionMode.PERMANENT_REDIRECT) {
@@ -143,14 +147,57 @@ public class FreenetInterface {
}
}
- /**
- * Creates a key pair.
- *
- * @return The request key at index 0, the insert key at index 1
- */
- public String[] generateKeyPair() {
- FreenetURI[] keyPair = client.generateKeyPair("");
- return new String[] { keyPair[1].toString(), keyPair[0].toString() };
+ 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);
}
/**
@@ -205,51 +252,29 @@ public class FreenetInterface {
}
}
- /**
- * 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, String.format("Registering Sone â%sâ for USK updates at %sâ¦", sone, sone.getRequestUri().setMetaString(new String[] { "sone.xml" })));
- USKCallback uskCallback = new USKCallback() {
-
- @Override
- @SuppressWarnings("synthetic-access")
- public void onFoundEdition(long edition, USK key, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
- 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));
- if (edition > sone.getLatestEdition()) {
- sone.setLatestEdition(edition);
- new Thread(new Runnable() {
-
- @Override
- public void run() {
- soneDownloader.fetchSone(sone);
- }
- }, "Sone Downloader").start();
- }
- }
-
- @Override
- public short getPollingPriorityProgress() {
- return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
- }
+ soneUskCallbacks.put(routingKey(requestUri), uskCallback);
+ node.clientCore.uskManager.subscribe(create(requestUri),
+ uskCallback, true, requestClient);
+ } 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);
- boolean runBackgroundFetch = (System.currentTimeMillis() - sone.getTime()) < TimeUnit.DAYS.toMillis(7);
- node.clientCore.uskManager.subscribe(USK.create(sone.getRequestUri()), uskCallback, runBackgroundFetch, (HighLevelSimpleClientImpl) client);
+ public void registerPassiveUsk(FreenetURI requestUri,
+ USKCallback uskCallback) {
+ try {
+ soneUskCallbacks.put(routingKey(requestUri), uskCallback);
+ node.clientCore
+ .uskManager
+ .subscribe(create(requestUri), uskCallback, false, requestClient);
} catch (MalformedURLException mue1) {
- logger.log(Level.WARNING, String.format("Could not subscribe USK â%sâ!", sone.getRequestUri()), mue1);
+ logger.log(WARNING,
+ format("Could not subscribe USK â%sâ!", requestUri),
+ mue1);
}
}
@@ -301,7 +326,7 @@ public class FreenetInterface {
};
try {
- node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, (HighLevelSimpleClientImpl) client);
+ node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, requestClient);
uriUskCallbacks.put(uri, uskCallback);
} catch (MalformedURLException mue1) {
logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1);
@@ -328,59 +353,7 @@ public class FreenetInterface {
}
/**
- * 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 {
@@ -408,7 +381,6 @@ public class FreenetInterface {
* @see ImageInsertStartedEvent
* @see ImageInsertFailedEvent
* @see ImageInsertFinishedEvent
- * @author David âBombeâ Roden
*/
public class InsertToken implements ClientPutCallback {
@@ -464,6 +436,7 @@ public class FreenetInterface {
public void cancel() {
clientPutter.cancel(node.clientCore.clientContext);
eventBus.post(new ImageInsertAbortedEvent(image));
+ bucket.free();
}
//
@@ -530,4 +503,20 @@ public class FreenetInterface {
}
+ public static class InsertTokenSupplier implements Function {
+
+ private final FreenetInterface freenetInterface;
+
+ @Inject
+ public InsertTokenSupplier(FreenetInterface freenetInterface) {
+ this.freenetInterface = freenetInterface;
+ }
+
+ @Override
+ public InsertToken apply(Image image) {
+ return freenetInterface.new InsertToken(image);
+ }
+
+ }
+
}