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.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 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30 public class SoneRescuer extends AbstractService {
33 private final Core core;
35 /** The Sone downloader. */
36 private final SoneDownloader soneDownloader;
38 /** The Sone being rescued. */
39 private final Sone sone;
41 /** Whether the rescuer is currently fetching a Sone. */
42 private volatile boolean fetching;
44 /** The currently tried edition. */
45 private volatile long currentEdition;
47 /** Whether the last fetch was successful. */
48 private volatile boolean lastFetchSuccessful = true;
51 * Creates a new Sone rescuer.
55 * @param soneDownloader
60 public SoneRescuer(Core core, SoneDownloader soneDownloader, Sone sone) {
61 super("Sone Rescuer for " + sone.getName());
63 this.soneDownloader = soneDownloader;
65 currentEdition = sone.getRequestUri().getEdition();
73 * Returns whether the Sone rescuer is currently fetching a Sone.
75 * @return {@code true} if the Sone rescuer is currently fetching a Sone
77 public boolean isFetching() {
82 * Returns the edition that is currently being downloaded.
84 * @return The edition that is currently being downloaded
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 public long getNextEdition() {
106 return currentEdition - 1;
110 * Sets the edition to rescue.
113 * The edition to rescue
114 * @return This Sone rescuer
116 public SoneRescuer setEdition(long edition) {
117 currentEdition = edition;
122 * Sets whether the last fetch was successful.
124 * @return {@code true} if the last fetch was successful, {@code false}
127 public boolean isLastFetchSuccessful() {
128 return lastFetchSuccessful;
136 * Starts the next fetch. If you want to fetch a different edition than “the
137 * next older one,” remember to call {@link #setEdition(long)} before
138 * calling this method.
140 public void startNextFetch() {
153 protected void serviceRun() {
154 while (!shouldStop()) {
155 while (!shouldStop() && !fetching) {
160 FreenetURI soneUri = sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + currentEdition).setMetaString(new String[] { "sone.xml" });
161 System.out.println("URI: " + soneUri);
162 Sone fetchedSone = soneDownloader.fetchSone(sone, soneUri, true);
163 System.out.println("Sone: " + fetchedSone);
164 lastFetchSuccessful = (fetchedSone != null);
165 if (lastFetchSuccessful) {
166 core.updateSone(fetchedSone, true);