e4829ac71c2d5e1a15819248aac4aa6f9156f332
[jSite.git] / src / de / todesbaum / jsite / gui / UpdateChecker.java
1 /*
2  * jSite-remote - UpdateChecker.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package de.todesbaum.jsite.gui;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Properties;
27
28 import de.todesbaum.jsite.application.Freenet7Interface;
29 import de.todesbaum.jsite.main.Main;
30 import de.todesbaum.jsite.main.Version;
31 import de.todesbaum.util.freenet.fcp2.Client;
32 import de.todesbaum.util.freenet.fcp2.ClientGet;
33 import de.todesbaum.util.freenet.fcp2.Connection;
34 import de.todesbaum.util.freenet.fcp2.Message;
35 import de.todesbaum.util.freenet.fcp2.Persistence;
36 import de.todesbaum.util.freenet.fcp2.ReturnType;
37 import de.todesbaum.util.freenet.fcp2.Verbosity;
38 import de.todesbaum.util.io.Closer;
39
40 /**
41  * Checks for newer versions of jSite.
42  *
43  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
44  */
45 public class UpdateChecker implements Runnable {
46
47         /** Counter for connection names. */
48         private static int counter = 0;
49
50         /** The edition for the update check URL. */
51         private static final int UPDATE_EDITION = 0;
52
53         /** The URL for update checks. */
54         private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE";
55
56         /** Object used for synchronization. */
57         private final Object syncObject = new Object();
58
59         /** Update listeners. */
60         private final List<UpdateListener> updateListeners = new ArrayList<UpdateListener>();
61
62         /** Whether the main thread should stop. */
63         private boolean shouldStop = false;
64
65         /** Current last found edition of update key. */
66         private int lastUpdateEdition = UPDATE_EDITION;
67
68         /** Last found version. */
69         private Version lastVersion = Main.getVersion();
70
71         /** The freenet interface. */
72         private final Freenet7Interface freenetInterface;
73
74         /**
75          * Creates a new update checker that uses the given frame as its parent and
76          * communications via the given freenet interface.
77          *
78          * @param freenetInterface
79          *            The freenet interface
80          */
81         public UpdateChecker(Freenet7Interface freenetInterface) {
82                 this.freenetInterface = freenetInterface;
83         }
84
85         //
86         // EVENT LISTENER MANAGEMENT
87         //
88
89         public void addUpdateListener(UpdateListener updateListener) {
90                 updateListeners.add(updateListener);
91         }
92
93         public void removeUpdateListener(UpdateListener updateListener) {
94                 updateListeners.remove(updateListener);
95         }
96
97         protected void fireUpdateFound(Version foundVersion, long versionTimestamp) {
98                 for (UpdateListener updateListener : updateListeners) {
99                         updateListener.foundUpdateData(foundVersion, versionTimestamp);
100                 }
101         }
102
103         //
104         // ACCESSORS
105         //
106
107         /**
108          * Returns the latest version that was found.
109          *
110          * @return The latest found version
111          */
112         public Version getLatestVersion() {
113                 return lastVersion;
114         }
115
116         //
117         // ACTIONS
118         //
119
120         public void start() {
121                 new Thread(this).start();
122         }
123
124         public void stop() {
125                 synchronized (syncObject) {
126                         shouldStop = true;
127                         syncObject.notifyAll();
128                 }
129         }
130
131         //
132         // PRIVATE METHODS
133         //
134
135         private boolean shouldStop() {
136                 synchronized (syncObject) {
137                         return shouldStop;
138                 }
139         }
140
141         private String constructUpdateKey(int edition) {
142                 return UPDATE_KEY + "/jSite/" + edition + "/jSite.properties";
143         }
144
145         //
146         // INTERFACE Runnable
147         //
148
149         public void run() {
150                 Connection connection = freenetInterface.getConnection("jSite-" + ++counter + "-UpdateChecker");
151                 try {
152                         connection.connect();
153                 } catch (IOException e1) {
154                         e1.printStackTrace();
155                 }
156                 Client client = new Client(connection);
157                 boolean checkNow = false;
158                 int currentEdition = lastUpdateEdition;
159                 while (!shouldStop()) {
160                         checkNow = false;
161                         System.out.println("Trying " + constructUpdateKey(currentEdition));
162                         ClientGet clientGet = new ClientGet("get-update-key");
163                         clientGet.setUri(constructUpdateKey(currentEdition));
164                         clientGet.setPersistence(Persistence.CONNECTION);
165                         clientGet.setReturnType(ReturnType.direct);
166                         clientGet.setVerbosity(Verbosity.ALL);
167                         try {
168                                 client.execute(clientGet);
169                                 boolean stop = false;
170                                 while (!stop) {
171                                         Message message = client.readMessage();
172                                         System.out.println(message);
173                                         if ("GetFailed".equals(message.getName())) {
174                                                 if ("27".equals(message.get("code"))) {
175                                                         String editionString = message.get("redirecturi").split("/")[2];
176                                                         int editionNumber = -1;
177                                                         try {
178                                                                 editionNumber = Integer.parseInt(editionString);
179                                                         } catch (NumberFormatException nfe1) {
180                                                                 /* ignore. */
181                                                         }
182                                                         if (editionNumber != -1) {
183                                                                 System.out.println("Found new edition " + editionNumber);
184                                                                 currentEdition = editionNumber;
185                                                                 lastUpdateEdition = editionNumber;
186                                                                 checkNow = true;
187                                                                 break;
188                                                         }
189                                                 }
190                                         }
191                                         if ("AllData".equals(message.getName())) {
192                                                 System.out.println("Update data found.");
193                                                 InputStream dataInputStream = null;
194                                                 Properties properties = new Properties();
195                                                 try {
196                                                         dataInputStream = message.getPayloadInputStream();
197                                                         properties.load(dataInputStream);
198                                                 } finally {
199                                                         Closer.close(dataInputStream);
200                                                 }
201
202                                                 String foundVersionString = properties.getProperty("jSite.Version");
203                                                 if (foundVersionString != null) {
204                                                         Version foundVersion = Version.parse(foundVersionString);
205                                                         if (foundVersion != null) {
206                                                                 lastVersion = foundVersion;
207                                                                 String versionTimestampString = properties.getProperty("jSite.Date");
208                                                                 System.out.println(versionTimestampString);
209                                                                 long versionTimestamp = -1;
210                                                                 try {
211                                                                         versionTimestamp = Long.parseLong(versionTimestampString);
212                                                                 } catch (NumberFormatException nfe1) {
213                                                                         /* ignore. */
214                                                                 }
215                                                                 fireUpdateFound(foundVersion, versionTimestamp);
216                                                                 stop = true;
217                                                                 checkNow = true;
218                                                                 ++currentEdition;
219                                                         }
220                                                 }
221                                         }
222                                 }
223                         } catch (IOException e) {
224                                 System.out.println("Got IOException: " + e.getMessage());
225                                 e.printStackTrace();
226                         }
227                         if (!checkNow && !shouldStop()) {
228                                 synchronized (syncObject) {
229                                         try {
230                                                 syncObject.wait(15 * 60 * 1000);
231                                         } catch (InterruptedException ie1) {
232                                                 /* ignore. */
233                                         }
234                                 }
235                         }
236                 }
237         }
238 }