2 * Sone - SoneRescuer.java - Copyright © 2011–2013 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 net.pterodactylus.sone.data.LocalSone;
21 import net.pterodactylus.sone.data.Sone;
22 import net.pterodactylus.util.service.AbstractService;
23 import freenet.keys.FreenetURI;
26 * The Sone rescuer downloads older editions of a Sone and updates the currently
27 * stored Sone with it.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public class SoneRescuer extends AbstractService {
34 private final Core core;
36 /** The Sone downloader. */
37 private final SoneDownloader soneDownloader;
39 /** The Sone being rescued. */
40 private final LocalSone sone;
42 /** Whether the rescuer is currently fetching a Sone. */
43 private volatile boolean fetching;
45 /** The currently tried edition. */
46 private volatile long currentEdition;
48 /** Whether the last fetch was successful. */
49 private volatile boolean lastFetchSuccessful = true;
52 * Creates a new Sone rescuer.
56 * @param soneDownloader
61 public SoneRescuer(Core core, SoneDownloader soneDownloader, LocalSone sone) {
62 super("Sone Rescuer for " + sone.getName());
64 this.soneDownloader = soneDownloader;
66 currentEdition = sone.getRequestUri().getEdition();
74 * Returns whether the Sone rescuer is currently fetching a Sone.
76 * @return {@code true} if the Sone rescuer is currently fetching a Sone
78 @SuppressWarnings("unused") // used in rescue.html
79 public boolean isFetching() {
84 * Returns the edition that is currently being downloaded.
86 * @return The edition that is currently being downloaded
88 @SuppressWarnings("unused") // used in rescue.html
89 public long getCurrentEdition() {
90 return currentEdition;
94 * Returns whether the Sone rescuer can download a next edition.
96 * @return {@code true} if the Sone rescuer can download a next edition,
97 * {@code false} if the last edition was already tried
99 public boolean hasNextEdition() {
100 return currentEdition > 0;
104 * Returns the next edition the Sone rescuer can download.
106 * @return The next edition the Sone rescuer can download
108 @SuppressWarnings("unused") // used in rescue.html
109 public long getNextEdition() {
110 return currentEdition - 1;
114 * Sets the edition to rescue.
117 * The edition to rescue
118 * @return This Sone rescuer
120 public SoneRescuer setEdition(long edition) {
121 currentEdition = edition;
126 * Sets whether the last fetch was successful.
128 * @return {@code true} if the last fetch was successful, {@code false}
131 @SuppressWarnings("unused") // used in rescue.html
132 public boolean isLastFetchSuccessful() {
133 return lastFetchSuccessful;
141 * Starts the next fetch. If you want to fetch a different edition than “the
142 * next older one,” remember to call {@link #setEdition(long)} before
143 * calling this method.
145 public void startNextFetch() {
158 protected void serviceRun() {
159 while (!shouldStop()) {
160 while (!shouldStop() && !fetching) {
165 FreenetURI soneUri = sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + currentEdition).setMetaString(new String[] { "sone.xml" });
166 System.out.println("URI: " + soneUri);
167 Sone fetchedSone = soneDownloader.fetchSone(sone, soneUri, true);
168 System.out.println("Sone: " + fetchedSone);
169 lastFetchSuccessful = (fetchedSone != null);
170 if (lastFetchSuccessful) {
171 core.updateSone(fetchedSone, true);