X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterface.java;h=20c8da76b488fd57284f097e5a8e9e32985aa8e8;hb=1bc78b582ac59f2438002997f5780db4dcee0a2a;hp=05b1fba331350dfdc05f8e3125967eca171ea839;hpb=cf8268f6427fd1f437658f6cf9029e1a44255085;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 05b1fba..20c8da7 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 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,48 +17,62 @@
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;
+
import freenet.client.FetchException;
import freenet.client.FetchResult;
import freenet.client.HighLevelSimpleClient;
+import freenet.client.HighLevelSimpleClientImpl;
import freenet.client.InsertException;
+import freenet.client.async.ClientContext;
+import freenet.client.async.USKCallback;
import freenet.keys.FreenetURI;
+import freenet.keys.USK;
import freenet.node.Node;
+import freenet.node.RequestStarter;
/**
* Contains all necessary functionality for interacting with the Freenet node.
*
* @author David âBombeâ Roden
*/
-public class FreenetInterface extends AbstractService {
+public class FreenetInterface {
/** The logger. */
private static final Logger logger = Logging.getLogger(FreenetInterface.class);
/** The node to interact with. */
- @SuppressWarnings("unused")
private final Node node;
/** The high-level client to use for requests. */
private final HighLevelSimpleClient client;
+ /** 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);
}
//
@@ -72,17 +86,22 @@ 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) {
- logger.entering(FreenetInterface.class.getName(), "fetchUri(FreenetURI)", uri);
+ public Pair fetchUri(FreenetURI uri) {
FetchResult fetchResult = null;
- try {
- fetchResult = client.fetch(uri);
- } catch (FetchException fe1) {
- logger.log(Level.WARNING, "Could not fetch â" + uri + "â!", fe1);
- } finally {
- logger.exiting(FreenetInterface.class.getName(), "fetchUri(FreenetURI)", fetchResult);
+ FreenetURI currentUri = new FreenetURI(uri);
+ while (true) {
+ try {
+ fetchResult = client.fetch(currentUri);
+ return new Pair(currentUri, fetchResult);
+ } catch (FetchException fe1) {
+ if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) {
+ currentUri = fe1.newURI;
+ continue;
+ }
+ logger.log(Level.WARNING, "Could not fetch â" + uri + "â!", fe1);
+ return null;
+ }
}
- return fetchResult;
}
/**
@@ -116,4 +135,149 @@ public class FreenetInterface extends AbstractService {
}
}
+ /**
+ * 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) {
+ 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 (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;
+ }
+
+ @Override
+ public short getPollingPriorityNormal() {
+ return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
+ }
+ };
+ soneUskCallbacks.put(sone.getId(), uskCallback);
+ node.clientCore.uskManager.subscribe(USK.create(sone.getRequestUri()), uskCallback, (System.currentTimeMillis() - sone.getTime()) < 7 * 24 * 60 * 60 * 1000, (HighLevelSimpleClientImpl) client);
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.WARNING, "Could not subscribe USK â" + sone.getRequestUri() + "â!", mue1);
+ }
+ }
+
+ /**
+ * Unsubscribes the request URI of the given Sone.
+ *
+ * @param sone
+ * The Sone to unregister
+ */
+ public void unregisterUsk(Sone sone) {
+ USKCallback uskCallback = soneUskCallbacks.remove(sone.getId());
+ if (uskCallback == null) {
+ return;
+ }
+ try {
+ logger.log(Level.FINEST, "Unsubscribing from USK for %sâ¦", new Object[] { sone });
+ node.clientCore.uskManager.unsubscribe(USK.create(sone.getRequestUri()), uskCallback);
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.FINE, "Could not unsubscribe USK â" + 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, 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);
+
+ }
+
}