X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterface.java;h=20c8da76b488fd57284f097e5a8e9e32985aa8e8;hb=25ca7aaaa3b481e1d22d907427d53c83387b1a55;hp=6737d634a922b43decf0e9d300453eb2b99a894a;hpb=a211a3bd360593efc9e7fba33d607a3b14d0a965;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 6737d63..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 @@ -18,6 +18,7 @@ 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; @@ -60,6 +61,9 @@ 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()); + /** * Creates a new Freenet interface. * @@ -149,8 +153,8 @@ public class FreenetInterface { @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.setLatestEdition(key.suggestedEdition); + if (edition > sone.getLatestEdition()) { + sone.setLatestEdition(edition); new Thread(new Runnable() { @Override @@ -172,7 +176,7 @@ public class FreenetInterface { } }; soneUskCallbacks.put(sone.getId(), uskCallback); - node.clientCore.uskManager.subscribe(USK.create(sone.getRequestUri()), uskCallback, true, (HighLevelSimpleClientImpl) client); + 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); } @@ -197,4 +201,83 @@ public class FreenetInterface { } } + /** + * 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); + + } + }