X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterface.java;h=48c1c6bf717e98880dbe0c840ef3438f8503e961;hp=af9aa5a72c1cb6e6e14dead512d904e995791727;hb=c37b5e0044a3d48d6bc2c3bb71a0b38a9302d455;hpb=cb7aebca2a351028dd4fdd00dcd637c6aafac79b diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index af9aa5a..48c1c6b 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -17,12 +17,22 @@ package net.pterodactylus.sone.core; +import static freenet.client.FetchException.PERMANENT_REDIRECT; +import static freenet.node.RequestStarter.INTERACTIVE_PRIORITY_CLASS; +import static freenet.node.RequestStarter.PREFETCH_PRIORITY_CLASS; +import static java.lang.String.format; +import static java.util.Collections.synchronizedMap; +import static java.util.concurrent.TimeUnit.DAYS; +import static java.util.logging.Level.FINE; +import static java.util.logging.Level.FINEST; +import static java.util.logging.Level.INFO; +import static java.util.logging.Level.WARNING; +import static net.pterodactylus.sone.data.Sone.TO_FREENET_URI; +import static net.pterodactylus.util.logging.Logging.getLogger; + 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 net.pterodactylus.sone.core.event.ImageInsertAbortedEvent; @@ -34,6 +44,10 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.util.logging.Logging; +import com.db4o.ObjectContainer; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.eventbus.EventBus; +import com.google.inject.Inject; import freenet.client.ClientMetadata; import freenet.client.FetchException; import freenet.client.FetchResult; @@ -56,11 +70,6 @@ import freenet.node.RequestClient; import freenet.node.RequestStarter; import freenet.support.api.Bucket; import freenet.support.io.ArrayBucket; -import com.db4o.ObjectContainer; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.eventbus.EventBus; -import com.google.inject.Inject; /** * Contains all necessary functionality for interacting with the Freenet node. @@ -70,7 +79,7 @@ import com.google.inject.Inject; public class FreenetInterface { /** The logger. */ - private static final Logger logger = Logging.getLogger(FreenetInterface.class); + private static final Logger logger = getLogger(FreenetInterface.class); /** The event bus. */ private final EventBus eventBus; @@ -86,7 +95,7 @@ public class FreenetInterface { private final Map soneUskCallbacks = new HashMap(); /** The not-Sone-related USK callbacks. */ - private final Map uriUskCallbacks = Collections.synchronizedMap(new HashMap()); + private final Map uriUskCallbacks = synchronizedMap(new HashMap()); private USKManager uskManager; /** @@ -101,7 +110,7 @@ public class FreenetInterface { public FreenetInterface(EventBus eventBus, Node node) { this.eventBus = eventBus; this.node = node; - this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true); + this.client = node.clientCore.makeClient(INTERACTIVE_PRIORITY_CLASS, false, true); this.requestClient = (HighLevelSimpleClientImpl) client; this.uskManager = node.clientCore.uskManager; } @@ -134,11 +143,11 @@ public class FreenetInterface { fetchResult = client.fetch(currentUri); return new Fetched(currentUri, fetchResult); } catch (FetchException fe1) { - if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) { + if (fe1.getMode() == PERMANENT_REDIRECT) { currentUri = fe1.newURI; continue; } - logger.log(Level.WARNING, String.format("Could not fetch “%s”!", uri), fe1); + logger.log(WARNING, format("Could not fetch “%s”!", uri), fe1); return null; } } @@ -177,7 +186,7 @@ public class FreenetInterface { ClientMetadata metadata = new ClientMetadata(temporaryImage.getMimeType()); InsertBlock insertBlock = new InsertBlock(bucket, metadata, targetUri); try { - ClientPutter clientPutter = client.insert(insertBlock, false, null, false, insertContext, insertToken, RequestStarter.INTERACTIVE_PRIORITY_CLASS); + ClientPutter clientPutter = client.insert(insertBlock, false, null, false, insertContext, insertToken, INTERACTIVE_PRIORITY_CLASS); insertToken.setClientPutter(clientPutter); } catch (InsertException ie1) { throw new SoneInsertException("Could not start image insert.", ie1); @@ -216,43 +225,20 @@ 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, 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, 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; - } - }; + logger.log(FINE, format("Registering Sone “%s” for USK updates at %s…", sone, TO_FREENET_URI.apply(sone).setMetaString(new String[]{"sone.xml"}))); + 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(sone.getRequestUri()), uskCallback, runBackgroundFetch, requestClient); + boolean runBackgroundFetch = soneWasUpdatedInTheLastWeek(sone); + uskManager.subscribe(USK.create(TO_FREENET_URI.apply(sone)), uskCallback, runBackgroundFetch, 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”!", TO_FREENET_URI.apply(sone)), mue1); } } + private boolean soneWasUpdatedInTheLastWeek(Sone sone) { + return (System.currentTimeMillis() - sone.getTime()) < DAYS.toMillis(7); + } + /** * Unsubscribes the request URI of the given Sone. * @@ -265,10 +251,10 @@ public class FreenetInterface { return; } try { - logger.log(Level.FINEST, String.format("Unsubscribing from USK for %s…", sone)); - uskManager.unsubscribe(USK.create(sone.getRequestUri()), uskCallback); + logger.log(FINEST, format("Unsubscribing from USK for %s…", sone)); + uskManager.unsubscribe(USK.create(TO_FREENET_URI.apply(sone)), uskCallback); } catch (MalformedURLException mue1) { - logger.log(Level.FINE, String.format("Could not unsubscribe USK “%s”!", sone.getRequestUri()), mue1); + logger.log(FINE, format("Could not unsubscribe USK “%s”!", TO_FREENET_URI.apply(sone)), mue1); } } @@ -282,29 +268,12 @@ 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); } catch (MalformedURLException mue1) { - logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1); + logger.log(WARNING, format("Could not subscribe to USK: %s", uri), mue1); } } @@ -317,13 +286,13 @@ public class FreenetInterface { public void unregisterUsk(FreenetURI uri) { USKCallback uskCallback = uriUskCallbacks.remove(uri); if (uskCallback == null) { - logger.log(Level.INFO, String.format("Could not unregister unknown USK: %s", uri)); + logger.log(INFO, format("Could not unregister unknown USK: %s", uri)); return; } try { uskManager.unsubscribe(USK.create(uri), uskCallback); } catch (MalformedURLException mue1) { - logger.log(Level.INFO, String.format("Could not unregister invalid USK: %s", uri), mue1); + logger.log(INFO, format("Could not unregister invalid USK: %s", uri), mue1); } } @@ -401,6 +370,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(FINE, 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 INTERACTIVE_PRIORITY_CLASS; + } + + @Override + public short getPollingPriorityNormal() { + return 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 PREFETCH_PRIORITY_CLASS; + } + + @Override + public short getPollingPriorityProgress() { + return INTERACTIVE_PRIORITY_CLASS; + } + + } + /** * Insert token that can cancel a running insert and sends events. *