X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneRescuer.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneRescuer.java;h=542ec4aa69596c609af5d0159afa56ca77641016;hb=bb2c8cb2d47a77bcef2d299ad4bf8e16f22a6198;hp=0000000000000000000000000000000000000000;hpb=3c7c28dad396ea710bec838550ffd54718d7999a;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/SoneRescuer.java b/src/main/java/net/pterodactylus/sone/core/SoneRescuer.java new file mode 100644 index 0000000..542ec4a --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/core/SoneRescuer.java @@ -0,0 +1,173 @@ +/* + * Sone - SoneRescuer.java - Copyright © 2011 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.core; + +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.util.service.AbstractService; +import freenet.keys.FreenetURI; + +/** + * The Sone rescuer downloads older editions of a Sone and updates the currently + * stored Sone with it. + * + * @author David ‘Bombe’ Roden + */ +public class SoneRescuer extends AbstractService { + + /** The core. */ + private final Core core; + + /** The Sone downloader. */ + private final SoneDownloader soneDownloader; + + /** The Sone being rescued. */ + private final Sone sone; + + /** Whether the rescuer is currently fetching a Sone. */ + private volatile boolean fetching; + + /** The currently tried edition. */ + private volatile long currentEdition; + + /** Whether the last fetch was successful. */ + private volatile boolean lastFetchSuccessful = true; + + /** + * Creates a new Sone rescuer. + * + * @param core + * The core + * @param soneDownloader + * The Sone downloader + * @param sone + * The Sone to rescue + */ + public SoneRescuer(Core core, SoneDownloader soneDownloader, Sone sone) { + super("Sone Rescuer for " + sone.getName()); + this.core = core; + this.soneDownloader = soneDownloader; + this.sone = sone; + currentEdition = sone.getRequestUri().getEdition(); + } + + // + // ACCESSORS + // + + /** + * Returns whether the Sone rescuer is currently fetching a Sone. + * + * @return {@code true} if the Sone rescuer is currently fetching a Sone + */ + public boolean isFetching() { + return fetching; + } + + /** + * Returns the edition that is currently being downloaded. + * + * @return The edition that is currently being downloaded + */ + public long getCurrentEdition() { + return currentEdition; + } + + /** + * Returns whether the Sone rescuer can download a next edition. + * + * @return {@code true} if the Sone rescuer can download a next edition, + * {@code false} if the last edition was already tried + */ + public boolean hasNextEdition() { + return currentEdition > 0; + } + + /** + * Returns the next edition the Sone rescuer can download. + * + * @return The next edition the Sone rescuer can download + */ + public long getNextEdition() { + return currentEdition - 1; + } + + /** + * Sets the edition to rescue. + * + * @param edition + * The edition to rescue + * @return This Sone rescuer + */ + public SoneRescuer setEdition(long edition) { + currentEdition = edition; + return this; + } + + /** + * Sets whether the last fetch was successful. + * + * @return {@code true} if the last fetch was successful, {@code false} + * otherwise + */ + public boolean isLastFetchSuccessful() { + return lastFetchSuccessful; + } + + // + // ACTIONS + // + + /** + * Starts the next fetch. If you want to fetch a different edition than “the + * next older one,” remember to call {@link #setEdition(long)} before + * calling this method. + */ + public void startNextFetch() { + fetching = true; + notifySyncObject(); + } + + // + // SERVICE METHODS + // + + /** + * {@inheritDoc} + */ + @Override + protected void serviceRun() { + while (!shouldStop()) { + while (!shouldStop() && !fetching) { + sleep(); + } + if (fetching) { + core.lockSone(sone); + FreenetURI soneUri = sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + currentEdition).setMetaString(new String[] { "sone.xml" }); + System.out.println("URI: " + soneUri); + Sone fetchedSone = soneDownloader.fetchSone(sone, soneUri, true); + System.out.println("Sone: " + fetchedSone); + lastFetchSuccessful = (fetchedSone != null); + if (lastFetchSuccessful) { + core.updateSone(fetchedSone, true); + } + fetching = false; + } + } + } + +}