import java.util.logging.Logger;
import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.util.collection.Pair;
import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.service.AbstractService;
import com.db4o.ObjectContainer;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class FreenetInterface extends AbstractService {
+public class FreenetInterface {
/** The logger. */
private static final Logger logger = Logging.getLogger(FreenetInterface.class);
*
* @param node
* The node to interact with
- * @param client
- * The high-level client
*/
- public FreenetInterface(Node node, HighLevelSimpleClient client) {
- super("Sone Freenet Interface");
+ public FreenetInterface(Node node) {
this.node = node;
- this.client = client;
+ this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true);
}
//
* The URI to fetch
* @return The result of the fetch, or {@code null} if an error occured
*/
- public FetchResult fetchUri(FreenetURI uri) {
- logger.entering(FreenetInterface.class.getName(), "fetchUri(FreenetURI)", uri);
+ public Pair<FreenetURI, FetchResult> fetchUri(FreenetURI uri) {
FetchResult fetchResult = null;
- try {
- fetchResult = client.fetch(uri);
- } catch (FetchException fe1) {
- logger.log(Level.WARNING, "Could not fetch “" + uri + "”!", fe1);
- } finally {
- logger.exiting(FreenetInterface.class.getName(), "fetchUri(FreenetURI)", fetchResult);
+ FreenetURI currentUri = new FreenetURI(uri);
+ while (true) {
+ try {
+ fetchResult = client.fetch(currentUri);
+ return new Pair<FreenetURI, FetchResult>(currentUri, fetchResult);
+ } catch (FetchException fe1) {
+ if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) {
+ currentUri = fe1.newURI;
+ continue;
+ }
+ logger.log(Level.WARNING, "Could not fetch “" + uri + "”!", fe1);
+ return null;
+ }
}
- return fetchResult;
}
/**
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.updateUris(key.getURI());
- soneDownloader.fetchSone(sone);
+ sone.setLatestEdition(key.suggestedEdition);
+ new Thread(new Runnable() {
+
+ @Override
+ public void run() {
+ soneDownloader.fetchSone(sone);
+ }
+ }, "Sone Downloader").start();
}
}