X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterface.java;h=52c2857a6da0a1132aba8cdc3a12a6c757d16c20;hb=977406b376374680eb2dae63dc17d8aea1a780dd;hp=2c98a7e940904878ed8781f16626bc1faaaf06be;hpb=b2f452ea2e5fdc2771e8f16139eb599b91ab9a38;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 2c98a7e..52c2857 100644
--- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java
+++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java
@@ -18,14 +18,15 @@
package net.pterodactylus.sone.core;
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 net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.util.collection.Pair;
import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.service.AbstractService;
import com.db4o.ObjectContainer;
@@ -46,7 +47,7 @@ import freenet.node.RequestStarter;
*
* @author David âBombeâ Roden
*/
-public class FreenetInterface extends AbstractService {
+public class FreenetInterface {
/** The logger. */
private static final Logger logger = Logging.getLogger(FreenetInterface.class);
@@ -60,18 +61,18 @@ public class FreenetInterface extends AbstractService {
/** The USK callbacks. */
private final Map soneUskCallbacks = new HashMap();
+ /** The not-Sone-related USK callbacks. */
+ private final Map uriUskCallbacks = Collections.synchronizedMap(new HashMap());
+
/**
* Creates a new Freenet interface.
*
* @param node
* The node to interact with
- * @param client
- * The high-level client
*/
- public FreenetInterface(Node node, HighLevelSimpleClient client) {
- super("Sone Freenet Interface");
+ public FreenetInterface(Node node) {
this.node = node;
- this.client = client;
+ this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true);
}
//
@@ -85,13 +86,13 @@ public class FreenetInterface extends AbstractService {
* The URI to fetch
* @return The result of the fetch, or {@code null} if an error occured
*/
- public FetchResult fetchUri(FreenetURI uri) {
+ public Pair fetchUri(FreenetURI uri) {
FetchResult fetchResult = null;
FreenetURI currentUri = new FreenetURI(uri);
while (true) {
try {
fetchResult = client.fetch(currentUri);
- return fetchResult;
+ return new Pair(currentUri, fetchResult);
} catch (FetchException fe1) {
if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) {
currentUri = fe1.newURI;
@@ -153,8 +154,14 @@ public class FreenetInterface extends AbstractService {
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.getURI());
- soneDownloader.fetchSone(sone);
+ sone.setLatestEdition(key.suggestedEdition);
+ new Thread(new Runnable() {
+
+ @Override
+ public void run() {
+ soneDownloader.fetchSone(sone);
+ }
+ }, "Sone Downloader").start();
}
}
@@ -194,4 +201,83 @@ public class FreenetInterface extends AbstractService {
}
}
+ /**
+ * 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, ObjectContainer objectContainer, 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, (HighLevelSimpleClientImpl) client);
+ uriUskCallbacks.put(uri, uskCallback);
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.WARNING, "Could not subscribe to USK: " + uri, uri);
+ }
+ }
+
+ /**
+ * 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, "Could not unregister unknown USK: " + uri);
+ return;
+ }
+ try {
+ node.clientCore.uskManager.unsubscribe(USK.create(uri), uskCallback);
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.INFO, "Could not unregister invalid USK: " + uri);
+ }
+ }
+
+ /**
+ * 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);
+
+ }
+
}