Remove superfluous fetch method
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneDownloaderImpl.java
1 /*
2  * Sone - SoneDownloaderImpl.java - Copyright © 2010–2016 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 static freenet.support.io.Closer.close;
21 import static java.lang.String.format;
22 import static java.lang.System.currentTimeMillis;
23 import static java.util.concurrent.TimeUnit.DAYS;
24 import static java.util.logging.Logger.getLogger;
25
26 import java.io.InputStream;
27 import java.util.HashSet;
28 import java.util.Set;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31
32 import javax.inject.Inject;
33
34 import net.pterodactylus.sone.core.FreenetInterface.Fetched;
35 import net.pterodactylus.sone.data.Sone;
36 import net.pterodactylus.sone.data.Sone.SoneStatus;
37 import net.pterodactylus.util.service.AbstractService;
38
39 import freenet.client.FetchResult;
40 import freenet.client.async.ClientContext;
41 import freenet.client.async.USKCallback;
42 import freenet.keys.FreenetURI;
43 import freenet.keys.USK;
44 import freenet.node.RequestStarter;
45 import freenet.support.api.Bucket;
46
47 import com.google.common.annotations.VisibleForTesting;
48
49 /**
50  * The Sone downloader is responsible for download Sones as they are updated.
51  *
52  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53  */
54 public class SoneDownloaderImpl extends AbstractService implements SoneDownloader {
55
56         /** The logger. */
57         private static final Logger logger = getLogger(SoneDownloaderImpl.class.getName());
58
59         /** The maximum protocol version. */
60         private static final int MAX_PROTOCOL_VERSION = 0;
61
62         /** The core. */
63         private final Core core;
64         private final SoneParser soneParser;
65
66         /** The Freenet interface. */
67         private final FreenetInterface freenetInterface;
68
69         /** The sones to update. */
70         private final Set<Sone> sones = new HashSet<Sone>();
71
72         /**
73          * Creates a new Sone downloader.
74          *
75          * @param core
76          *              The core
77          * @param freenetInterface
78          *              The Freenet interface
79          */
80         @Inject
81         public SoneDownloaderImpl(Core core, FreenetInterface freenetInterface) {
82                 this(core, freenetInterface, new SoneParser(core));
83         }
84
85         /**
86          * Creates a new Sone downloader.
87          *
88          * @param core
89          *              The core
90          * @param freenetInterface
91          *              The Freenet interface
92          * @param soneParser
93          */
94         @VisibleForTesting
95         SoneDownloaderImpl(Core core, FreenetInterface freenetInterface, SoneParser soneParser) {
96                 super("Sone Downloader", false);
97                 this.core = core;
98                 this.freenetInterface = freenetInterface;
99                 this.soneParser = soneParser;
100         }
101
102         //
103         // ACTIONS
104         //
105
106         /**
107          * Adds the given Sone to the set of Sones that will be watched for updates.
108          *
109          * @param sone
110          *              The Sone to add
111          */
112         @Override
113         public void addSone(final Sone sone) {
114                 if (!sones.add(sone)) {
115                         freenetInterface.unregisterUsk(sone);
116                 }
117                 final USKCallback uskCallback = new USKCallback() {
118
119                         @Override
120                         @SuppressWarnings("synthetic-access")
121                         public void onFoundEdition(long edition, USK key,
122                                         ClientContext clientContext, boolean metadata,
123                                         short codec, byte[] data, boolean newKnownGood,
124                                         boolean newSlotToo) {
125                                 logger.log(Level.FINE, format(
126                                                 "Found USK update for Sone “%s” at %s, new known good: %s, new slot too: %s.",
127                                                 sone, key, newKnownGood, newSlotToo));
128                                 if (edition > sone.getLatestEdition()) {
129                                         sone.setLatestEdition(edition);
130                                         new Thread(fetchSoneAction(sone),
131                                                         "Sone Downloader").start();
132                                 }
133                         }
134
135                         @Override
136                         public short getPollingPriorityProgress() {
137                                 return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
138                         }
139
140                         @Override
141                         public short getPollingPriorityNormal() {
142                                 return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
143                         }
144                 };
145                 if (soneHasBeenActiveRecently(sone)) {
146                         freenetInterface.registerActiveUsk(sone.getRequestUri(),
147                                         uskCallback);
148                 } else {
149                         freenetInterface.registerPassiveUsk(sone.getRequestUri(),
150                                         uskCallback);
151                 }
152         }
153
154         private boolean soneHasBeenActiveRecently(Sone sone) {
155                 return (currentTimeMillis() - sone.getTime()) < DAYS.toMillis(7);
156         }
157
158         private void fetchSone(Sone sone) {
159                 fetchSone(sone, sone.getRequestUri().sskForUSK(), false);
160         }
161
162         /**
163          * Fetches the Sone from the given URI.
164          *
165          * @param sone
166          *              The Sone to fetch
167          * @param soneUri
168          *              The URI of the Sone to fetch
169          * @param fetchOnly
170          *              {@code true} to only fetch and parse the Sone, {@code false}
171          *              to {@link Core#updateSone(Sone) update} it in the core
172          * @return The downloaded Sone, or {@code null} if the Sone could not be
173          *         downloaded
174          */
175         @Override
176         public Sone fetchSone(Sone sone, FreenetURI soneUri, boolean fetchOnly) {
177                 logger.log(Level.FINE, String.format("Starting fetch for Sone “%s” from %s…", sone, soneUri));
178                 FreenetURI requestUri = soneUri.setMetaString(new String[] { "sone.xml" });
179                 sone.setStatus(SoneStatus.downloading);
180                 try {
181                         Fetched fetchResults = freenetInterface.fetchUri(requestUri);
182                         if (fetchResults == null) {
183                                 /* TODO - mark Sone as bad. */
184                                 return null;
185                         }
186                         logger.log(Level.FINEST, String.format("Got %d bytes back.", fetchResults.getFetchResult().size()));
187                         Sone parsedSone = parseSone(sone, fetchResults.getFetchResult(), fetchResults.getFreenetUri());
188                         if (parsedSone != null) {
189                                 if (!fetchOnly) {
190                                         parsedSone.setStatus((parsedSone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
191                                         core.updateSone(parsedSone);
192                                         addSone(parsedSone);
193                                 }
194                         }
195                         return parsedSone;
196                 } finally {
197                         sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
198                 }
199         }
200
201         /**
202          * Parses a Sone from a fetch result.
203          *
204          * @param originalSone
205          *              The sone to parse, or {@code null} if the Sone is yet unknown
206          * @param fetchResult
207          *              The fetch result
208          * @param requestUri
209          *              The requested URI
210          * @return The parsed Sone, or {@code null} if the Sone could not be parsed
211          */
212         private Sone parseSone(Sone originalSone, FetchResult fetchResult, FreenetURI requestUri) {
213                 logger.log(Level.FINEST, String.format("Parsing FetchResult (%d bytes, %s) for %s…", fetchResult.size(), fetchResult.getMimeType(), originalSone));
214                 Bucket soneBucket = fetchResult.asBucket();
215                 InputStream soneInputStream = null;
216                 try {
217                         soneInputStream = soneBucket.getInputStream();
218                         Sone parsedSone = soneParser.parseSone(originalSone,
219                                         soneInputStream);
220                         if (parsedSone != null) {
221                                 parsedSone.setLatestEdition(requestUri.getEdition());
222                         }
223                         return parsedSone;
224                 } catch (Exception e1) {
225                         logger.log(Level.WARNING, String.format("Could not parse Sone from %s!", requestUri), e1);
226                 } finally {
227                         close(soneInputStream);
228                         close(soneBucket);
229                 }
230                 return null;
231         }
232
233         @Override
234         public Runnable fetchSoneWithUriAction(final Sone sone) {
235                 return new Runnable() {
236                         @Override
237                         public void run() {
238                                 fetchSone(sone, sone.getRequestUri(), false);
239                         }
240                 };
241         }
242
243         @Override
244         public Runnable fetchSoneAction(final Sone sone) {
245                 return new Runnable() {
246                         @Override
247                         public void run() {
248                                 fetchSone(sone);
249                         }
250                 };
251         }
252
253         /** {@inheritDoc} */
254         @Override
255         protected void serviceStop() {
256                 for (Sone sone : sones) {
257                         freenetInterface.unregisterUsk(sone);
258                 }
259         }
260
261 }