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