Add helper method to clear up intent.
[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 static net.pterodactylus.sone.data.Sone.TO_FREENET_URI;
21
22 import net.pterodactylus.sone.data.Sone;
23 import net.pterodactylus.util.service.AbstractService;
24 import freenet.keys.FreenetURI;
25
26 /**
27  * The Sone rescuer downloads older editions of a Sone and updates the currently
28  * stored Sone with it.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class SoneRescuer extends AbstractService {
33
34         /** The core. */
35         private final Core core;
36
37         /** The Sone downloader. */
38         private final SoneDownloader soneDownloader;
39
40         /** The Sone being rescued. */
41         private final Sone sone;
42
43         /** Whether the rescuer is currently fetching a Sone. */
44         private volatile boolean fetching;
45
46         /** The currently tried edition. */
47         private volatile long currentEdition;
48
49         /** Whether the last fetch was successful. */
50         private volatile boolean lastFetchSuccessful = true;
51
52         /**
53          * Creates a new Sone rescuer.
54          *
55          * @param core
56          *            The core
57          * @param soneDownloader
58          *            The Sone downloader
59          * @param sone
60          *            The Sone to rescue
61          */
62         public SoneRescuer(Core core, SoneDownloader soneDownloader, Sone sone) {
63                 super("Sone Rescuer for " + sone.getName());
64                 this.core = core;
65                 this.soneDownloader = soneDownloader;
66                 this.sone = sone;
67                 currentEdition = TO_FREENET_URI.apply(sone).getEdition();
68         }
69
70         //
71         // ACCESSORS
72         //
73
74         /**
75          * Returns whether the Sone rescuer is currently fetching a Sone.
76          *
77          * @return {@code true} if the Sone rescuer is currently fetching a Sone
78          */
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         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         public long getNextEdition() {
108                 return currentEdition - 1;
109         }
110
111         /**
112          * Sets the edition to rescue.
113          *
114          * @param edition
115          *            The edition to rescue
116          * @return This Sone rescuer
117          */
118         public SoneRescuer setEdition(long edition) {
119                 currentEdition = edition;
120                 return this;
121         }
122
123         /**
124          * Sets whether the last fetch was successful.
125          *
126          * @return {@code true} if the last fetch was successful, {@code false}
127          *         otherwise
128          */
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         @Override
152         protected void serviceRun() {
153                 while (!shouldStop()) {
154                         while (!shouldStop() && !fetching) {
155                                 sleep();
156                         }
157                         if (fetching) {
158                                 core.lockSone(sone);
159                                 FreenetURI soneUri = TO_FREENET_URI.apply(sone).setKeyType("SSK").setDocName("Sone-" + currentEdition).setMetaString(new String[] { "sone.xml" });
160                                 System.out.println("URI: " + soneUri);
161                                 Sone fetchedSone = soneDownloader.fetchSone(sone, soneUri, true);
162                                 System.out.println("Sone: " + fetchedSone);
163                                 lastFetchSuccessful = (fetchedSone != null);
164                                 if (lastFetchSuccessful) {
165                                         core.updateSone(fetchedSone, true);
166                                 }
167                                 fetching = false;
168                         }
169                 }
170         }
171
172 }