0731b1a1e6f47a76bc8f01d78c7ca4c12f65df46
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneRescuer.java
1 /*
2  * Sone - SoneRescuer.java - Copyright © 2011–2016 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import net.pterodactylus.sone.data.Sone;
21 import net.pterodactylus.util.service.AbstractService;
22 import freenet.keys.FreenetURI;
23
24 /**
25  * The Sone rescuer downloads older editions of a Sone and updates the currently
26  * stored Sone with it.
27  *
28  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29  */
30 public class SoneRescuer extends AbstractService {
31
32         /** The core. */
33         private final Core core;
34
35         /** The Sone downloader. */
36         private final SoneDownloader soneDownloader;
37
38         /** The Sone being rescued. */
39         private final Sone sone;
40
41         /** Whether the rescuer is currently fetching a Sone. */
42         private volatile boolean fetching;
43
44         /** The currently tried edition. */
45         private volatile long currentEdition;
46
47         /** Whether the last fetch was successful. */
48         private volatile boolean lastFetchSuccessful = true;
49
50         /**
51          * Creates a new Sone rescuer.
52          *
53          * @param core
54          *            The core
55          * @param soneDownloader
56          *            The Sone downloader
57          * @param sone
58          *            The Sone to rescue
59          */
60         public SoneRescuer(Core core, SoneDownloader soneDownloader, Sone sone) {
61                 super("Sone Rescuer for " + sone.getName());
62                 this.core = core;
63                 this.soneDownloader = soneDownloader;
64                 this.sone = sone;
65                 currentEdition = sone.getRequestUri().getEdition();
66         }
67
68         //
69         // ACCESSORS
70         //
71
72         /**
73          * Returns whether the Sone rescuer is currently fetching a Sone.
74          *
75          * @return {@code true} if the Sone rescuer is currently fetching a Sone
76          */
77         @SuppressWarnings("unused") // used in rescue.html
78         public boolean isFetching() {
79                 return fetching;
80         }
81
82         /**
83          * Returns the edition that is currently being downloaded.
84          *
85          * @return The edition that is currently being downloaded
86          */
87         @SuppressWarnings("unused") // used in rescue.html
88         public long getCurrentEdition() {
89                 return currentEdition;
90         }
91
92         /**
93          * Returns whether the Sone rescuer can download a next edition.
94          *
95          * @return {@code true} if the Sone rescuer can download a next edition,
96          *         {@code false} if the last edition was already tried
97          */
98         public boolean hasNextEdition() {
99                 return currentEdition > 0;
100         }
101
102         /**
103          * Returns the next edition the Sone rescuer can download.
104          *
105          * @return The next edition the Sone rescuer can download
106          */
107         @SuppressWarnings("unused") // used in rescue.html
108         public long getNextEdition() {
109                 return currentEdition - 1;
110         }
111
112         /**
113          * Sets the edition to rescue.
114          *
115          * @param edition
116          *            The edition to rescue
117          * @return This Sone rescuer
118          */
119         public SoneRescuer setEdition(long edition) {
120                 currentEdition = edition;
121                 return this;
122         }
123
124         /**
125          * Sets whether the last fetch was successful.
126          *
127          * @return {@code true} if the last fetch was successful, {@code false}
128          *         otherwise
129          */
130         @SuppressWarnings("unused") // used in rescue.html
131         public boolean isLastFetchSuccessful() {
132                 return lastFetchSuccessful;
133         }
134
135         //
136         // ACTIONS
137         //
138
139         /**
140          * Starts the next fetch. If you want to fetch a different edition than “the
141          * next older one,” remember to call {@link #setEdition(long)} before
142          * calling this method.
143          */
144         public void startNextFetch() {
145                 fetching = true;
146                 notifySyncObject();
147         }
148
149         //
150         // SERVICE METHODS
151         //
152
153         /**
154          * {@inheritDoc}
155          */
156         @Override
157         protected void serviceRun() {
158                 while (!shouldStop()) {
159                         while (!shouldStop() && !fetching) {
160                                 sleep();
161                         }
162                         if (fetching) {
163                                 core.lockSone(sone);
164                                 FreenetURI soneUri = sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + currentEdition).setMetaString(new String[] { "sone.xml" });
165                                 System.out.println("URI: " + soneUri);
166                                 Sone fetchedSone = soneDownloader.fetchSone(sone, soneUri, true);
167                                 System.out.println("Sone: " + fetchedSone);
168                                 lastFetchSuccessful = (fetchedSone != null);
169                                 if (lastFetchSuccessful) {
170                                         core.updateSone(fetchedSone, true);
171                                 }
172                                 fetching = false;
173                         }
174                 }
175         }
176
177 }