/*
- * Sone - SoneDownloader.java - Copyright © 2010–2013 David Roden
+ * Sone - SoneDownloaderImpl.java - Copyright © 2010–2020 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
import static java.lang.String.format;
import static java.lang.System.currentTimeMillis;
import static java.util.concurrent.TimeUnit.DAYS;
+import static java.util.logging.Logger.getLogger;
import java.io.InputStream;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
-import net.pterodactylus.sone.core.FreenetInterface.Fetched;
+import javax.inject.Inject;
+
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.Sone.SoneStatus;
-import net.pterodactylus.util.io.Closer;
-import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.service.AbstractService;
import freenet.client.FetchResult;
import freenet.keys.USK;
import freenet.node.RequestStarter;
import freenet.support.api.Bucket;
-import com.db4o.ObjectContainer;
-
-import com.google.common.annotations.VisibleForTesting;
/**
* The Sone downloader is responsible for download Sones as they are updated.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public class SoneDownloaderImpl extends AbstractService implements SoneDownloader {
/** The logger. */
- private static final Logger logger = Logging.getLogger(SoneDownloaderImpl.class);
+ private static final Logger logger = getLogger(SoneDownloaderImpl.class.getName());
/** The maximum protocol version. */
private static final int MAX_PROTOCOL_VERSION = 0;
/** The core. */
- private final Core core;
+ private final UpdatedSoneProcessor updatedSoneProcessor;
private final SoneParser soneParser;
/** The Freenet interface. */
private final FreenetInterface freenetInterface;
/** The sones to update. */
- private final Set<Sone> sones = new HashSet<Sone>();
+ private final Set<Sone> sones = new HashSet<>();
- /**
- * Creates a new Sone downloader.
- *
- * @param core
- * The core
- * @param freenetInterface
- * The Freenet interface
- */
- public SoneDownloaderImpl(Core core, FreenetInterface freenetInterface) {
- this(core, freenetInterface, new SoneParser(core));
- }
-
- /**
- * Creates a new Sone downloader.
- *
- * @param core
- * The core
- * @param freenetInterface
- * The Freenet interface
- * @param soneParser
- */
- @VisibleForTesting
- SoneDownloaderImpl(Core core, FreenetInterface freenetInterface, SoneParser soneParser) {
+ @Inject
+ SoneDownloaderImpl(UpdatedSoneProcessor updatedSoneProcessor, FreenetInterface freenetInterface, SoneParser soneParser) {
super("Sone Downloader", false);
- this.core = core;
+ this.updatedSoneProcessor = updatedSoneProcessor;
this.freenetInterface = freenetInterface;
this.soneParser = soneParser;
}
@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) {
sone, key, newKnownGood, newSlotToo));
if (edition > sone.getLatestEdition()) {
sone.setLatestEdition(edition);
- new Thread(fetchSoneAction(sone),
+ new Thread(fetchSoneAsSskAction(sone),
"Sone Downloader").start();
}
}
return (currentTimeMillis() - sone.getTime()) < DAYS.toMillis(7);
}
- private void fetchSone(Sone sone) {
- fetchSone(sone, sone.getRequestUri().sskForUSK());
- }
-
- /**
- * Fetches the updated Sone. This method can be used to fetch a Sone from a
- * specific URI.
- *
- * @param sone
- * The Sone to fetch
- * @param soneUri
- * The URI to fetch the Sone from
- */
- @Override
- public void fetchSone(Sone sone, FreenetURI soneUri) {
- fetchSone(sone, soneUri, false);
+ private void fetchSoneAsSsk(Sone sone) {
+ fetchSone(sone, sone.getRequestUri().sskForUSK(), false);
}
/**
if (parsedSone != null) {
if (!fetchOnly) {
parsedSone.setStatus((parsedSone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
- core.updateSone(parsedSone);
+ updatedSoneProcessor.updateSone(parsedSone);
addSone(parsedSone);
}
}
* @return The parsed Sone, or {@code null} if the Sone could not be parsed
*/
private Sone parseSone(Sone originalSone, FetchResult fetchResult, FreenetURI requestUri) {
- logger.log(Level.FINEST, String.format("Parsing FetchResult (%d bytes, %s) for %s…", fetchResult.size(), fetchResult.getMimeType(), originalSone));
+ logger.finest(() -> format("Parsing FetchResult (%d bytes, %s) for %s…", fetchResult.size(), fetchResult.getMimeType(), originalSone));
Bucket soneBucket = fetchResult.asBucket();
- InputStream soneInputStream = null;
- try {
- soneInputStream = soneBucket.getInputStream();
- Sone parsedSone = soneParser.parseSone(originalSone,
- soneInputStream);
+ try (InputStream soneInputStream = soneBucket.getInputStream()) {
+ Sone parsedSone = soneParser.parseSone(originalSone, soneInputStream);
if (parsedSone != null) {
+ logger.finer(() -> format("Sone %s was successfully parsed.", parsedSone));
parsedSone.setLatestEdition(requestUri.getEdition());
}
return parsedSone;
} catch (Exception e1) {
- logger.log(Level.WARNING, String.format("Could not parse Sone from %s!", requestUri), e1);
+ logger.log(Level.WARNING, e1, () -> format("Could not parse Sone from %s!", requestUri));
} finally {
- Closer.close(soneInputStream);
soneBucket.free();
}
return null;
}
@Override
- public Runnable fetchSoneWithUriAction(final Sone sone) {
+ public Runnable fetchSoneAsUskAction(final Sone sone) {
return new Runnable() {
@Override
public void run() {
- fetchSone(sone, sone.getRequestUri());
+ fetchSone(sone, sone.getRequestUri(), false);
}
};
}
@Override
- public Runnable fetchSoneAction(final Sone sone) {
+ public Runnable fetchSoneAsSskAction(final Sone sone) {
return new Runnable() {
@Override
public void run() {
- fetchSone(sone);
+ fetchSoneAsSsk(sone);
}
};
}