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;
25 import freenet.keys.FreenetURI;
27 import com.google.common.base.Optional;
30 * The Sone rescuer downloads older editions of a Sone and updates the currently
31 * stored Sone with it.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class SoneRescuer extends AbstractService {
38 private final Core core;
40 /** The Sone downloader. */
41 private final SoneDownloader soneDownloader;
43 /** The Sone being rescued. */
44 private final Sone sone;
46 /** Whether the rescuer is currently fetching a Sone. */
47 private volatile boolean fetching;
49 /** The currently tried edition. */
50 private volatile long currentEdition;
52 /** Whether the last fetch was successful. */
53 private volatile boolean lastFetchSuccessful = true;
56 * Creates a new Sone rescuer.
60 * @param soneDownloader
65 public SoneRescuer(Core core, SoneDownloader soneDownloader, Sone sone) {
66 super("Sone Rescuer for " + sone.getName());
68 this.soneDownloader = soneDownloader;
70 currentEdition = TO_FREENET_URI.apply(sone).getEdition();
78 * Returns whether the Sone rescuer is currently fetching a Sone.
80 * @return {@code true} if the Sone rescuer is currently fetching a Sone
82 public boolean isFetching() {
87 * Returns the edition that is currently being downloaded.
89 * @return The edition that is currently being downloaded
91 public long getCurrentEdition() {
92 return currentEdition;
96 * Returns whether the Sone rescuer can download a next edition.
98 * @return {@code true} if the Sone rescuer can download a next edition,
99 * {@code false} if the last edition was already tried
101 public boolean hasNextEdition() {
102 return currentEdition > 0;
106 * Returns the next edition the Sone rescuer can download.
108 * @return The next edition the Sone rescuer can download
110 public long getNextEdition() {
111 return currentEdition - 1;
115 * Sets the edition to rescue.
118 * The edition to rescue
119 * @return This Sone rescuer
121 public SoneRescuer setEdition(long edition) {
122 currentEdition = edition;
127 * Sets whether the last fetch was successful.
129 * @return {@code true} if the last fetch was successful, {@code false}
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() {
155 protected void serviceRun() {
156 while (!shouldStop()) {
157 while (!shouldStop() && !fetching) {
162 FreenetURI soneUri = TO_FREENET_URI.apply(sone).setKeyType("SSK").setDocName("Sone-" + currentEdition).setMetaString(new String[] { "sone.xml" });
163 System.out.println("URI: " + soneUri);
164 Optional<Sone> fetchedSone = soneDownloader.fetchSone(sone, soneUri, true);
165 System.out.println("Sone: " + fetchedSone);
166 lastFetchSuccessful = fetchedSone.isPresent();
167 if (lastFetchSuccessful) {
168 core.updateSone(fetchedSone.get(), true);