e28f49dc3ddddb97a71c3639efa0013fcf55339f
[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
25 import freenet.keys.FreenetURI;
26
27 import com.google.common.base.Optional;
28
29 /**
30  * The Sone rescuer downloads older editions of a Sone and updates the currently
31  * stored Sone with it.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class SoneRescuer extends AbstractService {
36
37         /** The core. */
38         private final Core core;
39
40         /** The Sone downloader. */
41         private final SoneDownloader soneDownloader;
42
43         /** The Sone being rescued. */
44         private final Sone sone;
45
46         /** Whether the rescuer is currently fetching a Sone. */
47         private volatile boolean fetching;
48
49         /** The currently tried edition. */
50         private volatile long currentEdition;
51
52         /** Whether the last fetch was successful. */
53         private volatile boolean lastFetchSuccessful = true;
54
55         /**
56          * Creates a new Sone rescuer.
57          *
58          * @param core
59          *            The core
60          * @param soneDownloader
61          *            The Sone downloader
62          * @param sone
63          *            The Sone to rescue
64          */
65         public SoneRescuer(Core core, SoneDownloader soneDownloader, Sone sone) {
66                 super("Sone Rescuer for " + sone.getName());
67                 this.core = core;
68                 this.soneDownloader = soneDownloader;
69                 this.sone = sone;
70                 currentEdition = TO_FREENET_URI.apply(sone).getEdition();
71         }
72
73         //
74         // ACCESSORS
75         //
76
77         /**
78          * Returns whether the Sone rescuer is currently fetching a Sone.
79          *
80          * @return {@code true} if the Sone rescuer is currently fetching a Sone
81          */
82         public boolean isFetching() {
83                 return fetching;
84         }
85
86         /**
87          * Returns the edition that is currently being downloaded.
88          *
89          * @return The edition that is currently being downloaded
90          */
91         public long getCurrentEdition() {
92                 return currentEdition;
93         }
94
95         /**
96          * Returns whether the Sone rescuer can download a next edition.
97          *
98          * @return {@code true} if the Sone rescuer can download a next edition,
99          *         {@code false} if the last edition was already tried
100          */
101         public boolean hasNextEdition() {
102                 return currentEdition > 0;
103         }
104
105         /**
106          * Returns the next edition the Sone rescuer can download.
107          *
108          * @return The next edition the Sone rescuer can download
109          */
110         public long getNextEdition() {
111                 return currentEdition - 1;
112         }
113
114         /**
115          * Sets the edition to rescue.
116          *
117          * @param edition
118          *            The edition to rescue
119          * @return This Sone rescuer
120          */
121         public SoneRescuer setEdition(long edition) {
122                 currentEdition = edition;
123                 return this;
124         }
125
126         /**
127          * Sets whether the last fetch was successful.
128          *
129          * @return {@code true} if the last fetch was successful, {@code false}
130          *         otherwise
131          */
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         @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 = TO_FREENET_URI.apply(sone).setKeyType("SSK").setDocName("Sone-" + currentEdition).setMetaString(new String[] { "sone.xml" });
163                                 System.out.println("URI: " + soneUri);
164                                 Optional<Sone> fetchedSone = soneDownloader.fetchSone(sone, soneUri, true);
165                                 System.out.println("Sone: " + fetchedSone);
166                                 lastFetchSuccessful = fetchedSone.isPresent();
167                                 if (lastFetchSuccessful) {
168                                         core.updateSone(fetchedSone.get(), true);
169                                 }
170                                 fetching = false;
171                         }
172                 }
173         }
174
175 }