X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterface.java;h=b0c108ab582ba2e1aa1db0f12464907cbab96c93;hb=40a1ac4c90c1480936c9c782727209cf23acd5e5;hp=d525fc7978a057f684e6768a2949065c8f2e7997;hpb=99888ce13cc17d49f5e217ab6f2c9ad5ef168792;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 d525fc7..b0c108a 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -21,6 +21,7 @@ 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,14 +35,16 @@ import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.util.logging.Logging; import com.db4o.ObjectContainer; + +import com.google.common.base.Function; import com.google.common.eventbus.EventBus; import com.google.inject.Inject; +import com.google.inject.Singleton; import freenet.client.ClientMetadata; import freenet.client.FetchException; import freenet.client.FetchResult; import freenet.client.HighLevelSimpleClient; -import freenet.client.HighLevelSimpleClientImpl; import freenet.client.InsertBlock; import freenet.client.InsertContext; import freenet.client.InsertException; @@ -54,6 +57,7 @@ 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; @@ -63,6 +67,7 @@ import freenet.support.io.ArrayBucket; * * @author David ‘Bombe’ Roden */ +@Singleton public class FreenetInterface { /** The logger. */ @@ -110,11 +115,10 @@ public class FreenetInterface { * @return The result of the fetch, or {@code null} if an error occured */ public Fetched fetchUri(FreenetURI uri) { - FetchResult fetchResult = null; FreenetURI currentUri = new FreenetURI(uri); while (true) { try { - fetchResult = client.fetch(currentUri); + FetchResult fetchResult = client.fetch(currentUri); return new Fetched(currentUri, fetchResult); } catch (FetchException fe1) { if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) { @@ -128,16 +132,6 @@ public class FreenetInterface { } /** - * Creates a key pair. - * - * @return The request key at index 0, the insert key at index 1 - */ - public String[] generateKeyPair() { - FreenetURI[] keyPair = client.generateKeyPair(""); - return new String[] { keyPair[1].toString(), keyPair[0].toString() }; - } - - /** * Inserts the image data of the given {@link TemporaryImage} and returns * the given insert token that can be used to add listeners or cancel the * insert. @@ -188,16 +182,7 @@ public class FreenetInterface { } } - /** - * 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) { + public void registerUsk(final Sone sone, final SoneUpdater soneUpdater) { 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() { @@ -206,16 +191,7 @@ 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, 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(); - } + soneUpdater.updateSone(sone, edition); } @Override @@ -229,7 +205,8 @@ public class FreenetInterface { } }; 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); + node.clientCore.uskManager.subscribe(USK.create(sone.getRequestUri()), uskCallback, runBackgroundFetch, (RequestClient) client); } catch (MalformedURLException mue1) { logger.log(Level.WARNING, String.format("Could not subscribe USK “%s”!", sone.getRequestUri()), mue1); } @@ -283,7 +260,7 @@ public class FreenetInterface { }; try { - node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, (HighLevelSimpleClientImpl) client); + node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, (RequestClient) client); uriUskCallbacks.put(uri, uskCallback); } catch (MalformedURLException mue1) { logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1); @@ -503,4 +480,13 @@ public class FreenetInterface { } + public class InsertTokenSupplier implements Function { + + @Override + public InsertToken apply(Image image) { + return new InsertToken(image); + } + + } + }