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=9394e93a90080962f4532dd02086ff0c031a9ee4;hpb=9f3bbe5bb4450085d23bfa39c95da13267104916;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 9394e93..8766485 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 @@ /* - * Sone - FreenetInterface.java - Copyright © 2010–2012 David Roden + * Sone - FreenetInterface.java - Copyright © 2010–2013 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,10 +17,14 @@ 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; 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; @@ -34,9 +38,9 @@ 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; @@ -50,10 +54,12 @@ import freenet.client.async.ClientContext; import freenet.client.async.ClientPutCallback; import freenet.client.async.ClientPutter; import freenet.client.async.USKCallback; +import freenet.client.async.USKManager; import freenet.keys.FreenetURI; import freenet.keys.InsertableClientSSK; import freenet.keys.USK; import freenet.node.Node; +import freenet.node.RequestClient; import freenet.node.RequestStarter; import freenet.support.api.Bucket; import freenet.support.io.ArrayBucket; @@ -76,12 +82,14 @@ public class FreenetInterface { /** The high-level client to use for requests. */ private final HighLevelSimpleClient client; + private final RequestClient requestClient; /** The USK callbacks. */ private final Map soneUskCallbacks = new HashMap(); /** The not-Sone-related USK callbacks. */ private final Map uriUskCallbacks = Collections.synchronizedMap(new HashMap()); + private USKManager uskManager; /** * Creates a new Freenet interface. @@ -96,6 +104,17 @@ public class FreenetInterface { this.eventBus = eventBus; this.node = node; this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true); + this.requestClient = (HighLevelSimpleClientImpl) client; + this.uskManager = node.clientCore.uskManager; + } + + @VisibleForTesting + public FreenetInterface(EventBus eventBus, Node node, HighLevelSimpleClient highLevelSimpleClient, RequestClient requestClient, USKManager uskManager) { + this.eventBus = eventBus; + this.node = node; + this.client = highLevelSimpleClient; + this.requestClient = requestClient; + this.uskManager = uskManager; } // @@ -199,39 +218,13 @@ 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(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 NewEditionFound(sone, soneDownloader); 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); + boolean runBackgroundFetch = (System.currentTimeMillis() - sone.getTime()) < TimeUnit.DAYS.toMillis(7); + 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(Level.WARNING, String.format("Could not subscribe USK “%s”!", TO_FREENET_URI.apply(sone)), mue1); } } @@ -248,9 +241,9 @@ public class FreenetInterface { } try { logger.log(Level.FINEST, String.format("Unsubscribing from USK for %s…", sone)); - node.clientCore.uskManager.unsubscribe(USK.create(sone.getRequestUri()), uskCallback); + 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(Level.FINE, String.format("Could not unsubscribe USK “%s”!", TO_FREENET_URI.apply(sone)), mue1); } } @@ -264,26 +257,9 @@ 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 { - node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, (HighLevelSimpleClientImpl) client); + 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); @@ -303,7 +279,7 @@ public class FreenetInterface { return; } try { - node.clientCore.uskManager.unsubscribe(USK.create(uri), uskCallback); + uskManager.unsubscribe(USK.create(uri), uskCallback); } catch (MalformedURLException mue1) { logger.log(Level.INFO, String.format("Could not unregister invalid USK: %s", uri), mue1); } @@ -383,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. * @@ -447,17 +485,11 @@ public class FreenetInterface { // INTERFACE ClientPutCallback // - /** - * {@inheritDoc} - */ @Override public void onMajorProgress(ObjectContainer objectContainer) { /* ignore, we don’t care. */ } - /** - * {@inheritDoc} - */ @Override @SuppressWarnings("synthetic-access") public void onFailure(InsertException insertException, BaseClientPutter clientPutter, ObjectContainer objectContainer) { @@ -468,33 +500,21 @@ public class FreenetInterface { } } - /** - * {@inheritDoc} - */ @Override public void onFetchable(BaseClientPutter clientPutter, ObjectContainer objectContainer) { /* ignore, we don’t care. */ } - /** - * {@inheritDoc} - */ @Override public void onGeneratedMetadata(Bucket metadata, BaseClientPutter clientPutter, ObjectContainer objectContainer) { /* ignore, we don’t care. */ } - /** - * {@inheritDoc} - */ @Override public void onGeneratedURI(FreenetURI generatedUri, BaseClientPutter clientPutter, ObjectContainer objectContainer) { resultingUri = generatedUri; } - /** - * {@inheritDoc} - */ @Override @SuppressWarnings("synthetic-access") public void onSuccess(BaseClientPutter clientPutter, ObjectContainer objectContainer) {