2 * Sone - SoneRescuer.java - Copyright © 2011–2019 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.Sone;
21 import net.pterodactylus.util.service.AbstractService;
22 import freenet.keys.FreenetURI;
25 * The Sone rescuer downloads older editions of a Sone and updates the currently
26 * stored Sone with it.
28 public class SoneRescuer extends AbstractService {
31 private final Core core;
33 /** The Sone downloader. */
34 private final SoneDownloader soneDownloader;
36 /** The Sone being rescued. */
37 private final Sone sone;
39 /** Whether the rescuer is currently fetching a Sone. */
40 private volatile boolean fetching;
42 /** The currently tried edition. */
43 private volatile long currentEdition;
45 /** Whether the last fetch was successful. */
46 private volatile boolean lastFetchSuccessful = true;
49 * Creates a new Sone rescuer.
53 * @param soneDownloader
58 public SoneRescuer(Core core, SoneDownloader soneDownloader, Sone sone) {
59 super("Sone Rescuer for " + sone.getName());
61 this.soneDownloader = soneDownloader;
63 currentEdition = sone.getRequestUri().getEdition();
71 * Returns whether the Sone rescuer is currently fetching a Sone.
73 * @return {@code true} if the Sone rescuer is currently fetching a Sone
75 @SuppressWarnings("unused") // used in rescue.html
76 public boolean isFetching() {
81 * Returns the edition that is currently being downloaded.
83 * @return The edition that is currently being downloaded
85 @SuppressWarnings("unused") // used in rescue.html
86 public long getCurrentEdition() {
87 return currentEdition;
91 * Returns whether the Sone rescuer can download a next edition.
93 * @return {@code true} if the Sone rescuer can download a next edition,
94 * {@code false} if the last edition was already tried
96 public boolean hasNextEdition() {
97 return currentEdition > 0;
101 * Returns the next edition the Sone rescuer can download.
103 * @return The next edition the Sone rescuer can download
105 @SuppressWarnings("unused") // used in rescue.html
106 public long getNextEdition() {
107 return currentEdition - 1;
111 * Sets whether the last fetch was successful.
113 * @return {@code true} if the last fetch was successful, {@code false}
116 @SuppressWarnings("unused") // used in rescue.html
117 public boolean isLastFetchSuccessful() {
118 return lastFetchSuccessful;
126 * Starts the next fetch.
128 public void startNextFetch() {
141 protected void serviceRun() {
142 while (!shouldStop()) {
143 while (!shouldStop() && !fetching) {
148 FreenetURI soneUri = sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + getNextEdition()).setMetaString(new String[] { "sone.xml" });
149 System.out.println("URI: " + soneUri);
150 Sone fetchedSone = soneDownloader.fetchSone(sone, soneUri, true);
151 System.out.println("Sone: " + fetchedSone);
152 lastFetchSuccessful = (fetchedSone != null);
153 if (lastFetchSuccessful) {
154 core.updateSone(fetchedSone, true);
155 currentEdition = getNextEdition();