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