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 static net.pterodactylus.sone.data.Sone.TO_FREENET_URI;
22 import net.pterodactylus.sone.data.Sone;
23 import net.pterodactylus.util.service.AbstractService;
24 import freenet.keys.FreenetURI;
27 * The Sone rescuer downloads older editions of a Sone and updates the currently
28 * stored Sone with it.
30 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 public class SoneRescuer extends AbstractService {
35 private final Core core;
37 /** The Sone downloader. */
38 private final SoneDownloader soneDownloader;
40 /** The Sone being rescued. */
41 private final Sone sone;
43 /** Whether the rescuer is currently fetching a Sone. */
44 private volatile boolean fetching;
46 /** The currently tried edition. */
47 private volatile long currentEdition;
49 /** Whether the last fetch was successful. */
50 private volatile boolean lastFetchSuccessful = true;
53 * Creates a new Sone rescuer.
57 * @param soneDownloader
62 public SoneRescuer(Core core, SoneDownloader soneDownloader, Sone sone) {
63 super("Sone Rescuer for " + sone.getName());
65 this.soneDownloader = soneDownloader;
67 currentEdition = TO_FREENET_URI.apply(sone).getEdition();
75 * Returns whether the Sone rescuer is currently fetching a Sone.
77 * @return {@code true} if the Sone rescuer is currently fetching a Sone
79 public boolean isFetching() {
84 * Returns the edition that is currently being downloaded.
86 * @return The edition that is currently being downloaded
88 public long getCurrentEdition() {
89 return currentEdition;
93 * Returns whether the Sone rescuer can download a next edition.
95 * @return {@code true} if the Sone rescuer can download a next edition,
96 * {@code false} if the last edition was already tried
98 public boolean hasNextEdition() {
99 return currentEdition > 0;
103 * Returns the next edition the Sone rescuer can download.
105 * @return The next edition the Sone rescuer can download
107 public long getNextEdition() {
108 return currentEdition - 1;
112 * Sets the edition to rescue.
115 * The edition to rescue
116 * @return This Sone rescuer
118 public SoneRescuer setEdition(long edition) {
119 currentEdition = edition;
124 * Sets whether the last fetch was successful.
126 * @return {@code true} if the last fetch was successful, {@code false}
129 public boolean isLastFetchSuccessful() {
130 return lastFetchSuccessful;
138 * Starts the next fetch. If you want to fetch a different edition than “the
139 * next older one,” remember to call {@link #setEdition(long)} before
140 * calling this method.
142 public void startNextFetch() {
152 protected void serviceRun() {
153 while (!shouldStop()) {
154 while (!shouldStop() && !fetching) {
159 FreenetURI soneUri = TO_FREENET_URI.apply(sone).setKeyType("SSK").setDocName("Sone-" + currentEdition).setMetaString(new String[] { "sone.xml" });
160 System.out.println("URI: " + soneUri);
161 Sone fetchedSone = soneDownloader.fetchSone(sone, soneUri, true);
162 System.out.println("Sone: " + fetchedSone);
163 lastFetchSuccessful = (fetchedSone != null);
164 if (lastFetchSuccessful) {
165 core.updateSone(fetchedSone, true);