2 * Sone - SoneDownloader.java - Copyright © 2010 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core;
20 import java.util.HashSet;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.util.logging.Logging;
27 import net.pterodactylus.util.service.AbstractService;
28 import freenet.client.FetchResult;
31 * The Sone downloader is responsible for download Sones as they are updated.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class SoneDownloader extends AbstractService {
38 private static final Logger logger = Logging.getLogger(SoneDownloader.class);
40 /** The Freenet interface. */
41 private final FreenetInterface freenetInterface;
43 /** The sones to update. */
44 private final Set<Sone> sones = new HashSet<Sone>();
47 * Creates a new Sone downloader.
49 * @param freenetInterface
50 * The Freenet interface
52 public SoneDownloader(FreenetInterface freenetInterface) {
53 super("Sone Downloader");
54 this.freenetInterface = freenetInterface;
62 * Adds the given Sone to the set of Sones that will be watched for updates.
67 public void addSone(Sone sone) {
68 if (sones.add(sone)) {
69 freenetInterface.registerUsk(sone, this);
74 * Fetches the updated Sone. This method is a callback method for
75 * {@link FreenetInterface#registerUsk(Sone, SoneDownloader)}.
80 public void fetchSone(Sone sone) {
81 logger.log(Level.FINE, "Starting fetch for Sone “%s” from %s…", new Object[] { sone, sone.getRequestUri().setMetaString(new String[] { "sone.xml" }) });
82 FetchResult fetchResult = freenetInterface.fetchUri(sone.getRequestUri().setMetaString(new String[] { "sone.xml" }));
83 logger.log(Level.FINEST, "Got %d bytes back.", fetchResult.size());
94 protected void serviceStop() {
95 for (Sone sone : sones) {
96 freenetInterface.unregisterUsk(sone);