X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterface.java;h=87664857540d847d5df21b6c86037cd3a375ef3b;hb=7fb49938b9198110c34bcc600c545bfa91acf6f2;hp=bea268b35e76ef2697c1fc019fdc78d3d7250097;hpb=1380d276bf5886e320f89cea232a6db7e4cdb7b8;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 bea268b..8766485 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -17,6 +17,7 @@ package net.pterodactylus.sone.core; +import static java.lang.String.format; import static net.pterodactylus.sone.data.Sone.TO_FREENET_URI; import java.net.MalformedURLException; @@ -218,34 +219,7 @@ public class FreenetInterface { public void registerUsk(final Sone sone, final SoneDownloader soneDownloader) { try { logger.log(Level.FINE, String.format("Registering Sone “%s” for USK updates at %s…", sone, TO_FREENET_URI.apply(sone).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, 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; - } - - @Override - public short getPollingPriorityNormal() { - return RequestStarter.INTERACTIVE_PRIORITY_CLASS; - } - }; + USKCallback uskCallback = new NewEditionFound(sone, soneDownloader); soneUskCallbacks.put(sone.getId(), uskCallback); boolean runBackgroundFetch = (System.currentTimeMillis() - sone.getTime()) < TimeUnit.DAYS.toMillis(7); uskManager.subscribe(USK.create(TO_FREENET_URI.apply(sone)), uskCallback, runBackgroundFetch, requestClient); @@ -283,24 +257,7 @@ public class FreenetInterface { * 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; - } - - }; + USKCallback uskCallback = new CallbackWrapper(callback); try { uskManager.subscribe(USK.create(uri), uskCallback, true, requestClient); uriUskCallbacks.put(uri, uskCallback); @@ -402,6 +359,68 @@ public class FreenetInterface { } + private static class NewEditionFound implements USKCallback { + + private final Sone sone; + private final SoneDownloader soneDownloader; + + public NewEditionFound(Sone sone, SoneDownloader soneDownloader) { + this.sone = sone; + this.soneDownloader = soneDownloader; + } + + @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, 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.modify().setLatestEdition(edition).update(); + new Thread(new Runnable() { + + @Override + public void run() { + soneDownloader.fetchSone(sone); + } + }, format("Sone Downloader for %s", sone.getId())).start(); + } + } + + @Override + public short getPollingPriorityProgress() { + return RequestStarter.INTERACTIVE_PRIORITY_CLASS; + } + + @Override + public short getPollingPriorityNormal() { + return RequestStarter.INTERACTIVE_PRIORITY_CLASS; + } + } + + private static class CallbackWrapper implements USKCallback { + + private final Callback callback; + + public CallbackWrapper(Callback callback) { + this.callback = callback; + } + + @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; + } + + } + /** * Insert token that can cancel a running insert and sends events. *