1a8f870bd706e94b00b1c159d40f5bd8c91a7373
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneDownloader.java
1 /*
2  * Sone - SoneDownloader.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import java.util.HashSet;
21 import java.util.Set;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24
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;
29
30 /**
31  * The Sone downloader is responsible for download Sones as they are updated.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class SoneDownloader extends AbstractService {
36
37         /** The logger. */
38         private static final Logger logger = Logging.getLogger(SoneDownloader.class);
39
40         /** The Freenet interface. */
41         private final FreenetInterface freenetInterface;
42
43         /** The sones to update. */
44         private final Set<Sone> sones = new HashSet<Sone>();
45
46         /**
47          * Creates a new Sone downloader.
48          *
49          * @param freenetInterface
50          *            The Freenet interface
51          */
52         public SoneDownloader(FreenetInterface freenetInterface) {
53                 super("Sone Downloader");
54                 this.freenetInterface = freenetInterface;
55         }
56
57         //
58         // ACTIONS
59         //
60
61         /**
62          * Adds the given Sone to the set of Sones that will be watched for updates.
63          *
64          * @param sone
65          *            The Sone to add
66          */
67         public void addSone(Sone sone) {
68                 if (sones.add(sone)) {
69                         freenetInterface.registerUsk(sone, this);
70                 }
71         }
72
73         /**
74          * Fetches the updated Sone. This method is a callback method for
75          * {@link FreenetInterface#registerUsk(Sone, SoneDownloader)}.
76          *
77          * @param sone
78          *            The Sone to fetch
79          */
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());
84         }
85
86         //
87         // SERVICE METHODS
88         //
89
90         /**
91          * {@inheritDoc}
92          */
93         @Override
94         protected void serviceStop() {
95                 for (Sone sone : sones) {
96                         freenetInterface.unregisterUsk(sone);
97                 }
98         }
99
100 }