2 * jSite-remote - UpdateChecker.java -
3 * Copyright © 2008 David Roden
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.
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.
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.
20 package de.todesbaum.jsite.gui;
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;
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;
41 * Checks for newer versions of jSite.
43 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
45 public class UpdateChecker implements Runnable {
47 /** Counter for connection names. */
48 private static int counter = 0;
50 /** The edition for the update check URL. */
51 private static final int UPDATE_EDITION = 0;
53 /** The URL for update checks. */
54 private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE";
56 /** Object used for synchronization. */
57 private final Object syncObject = new Object();
59 /** Update listeners. */
60 private final List<UpdateListener> updateListeners = new ArrayList<UpdateListener>();
62 /** Whether the main thread should stop. */
63 private boolean shouldStop = false;
65 /** Current last found edition of update key. */
66 private int lastUpdateEdition = UPDATE_EDITION;
68 /** Last found version. */
69 private Version lastVersion = Main.getVersion();
71 /** The freenet interface. */
72 private final Freenet7Interface freenetInterface;
75 * Creates a new update checker that uses the given frame as its parent and
76 * communications via the given freenet interface.
78 * @param freenetInterface
79 * The freenet interface
81 public UpdateChecker(Freenet7Interface freenetInterface) {
82 this.freenetInterface = freenetInterface;
86 // EVENT LISTENER MANAGEMENT
89 public void addUpdateListener(UpdateListener updateListener) {
90 updateListeners.add(updateListener);
93 public void removeUpdateListener(UpdateListener updateListener) {
94 updateListeners.remove(updateListener);
97 protected void fireUpdateFound(Version foundVersion, long versionTimestamp) {
98 for (UpdateListener updateListener : updateListeners) {
99 updateListener.foundUpdateData(foundVersion, versionTimestamp);
107 public void start() {
108 new Thread(this).start();
112 synchronized (syncObject) {
114 syncObject.notifyAll();
122 private boolean shouldStop() {
123 synchronized (syncObject) {
128 private String constructUpdateKey(int edition) {
129 return UPDATE_KEY + "/jSite/" + edition + "/jSite.properties";
133 // INTERFACE Runnable
137 Connection connection = freenetInterface.getConnection("jSite-" + ++counter + "-UpdateChecker");
139 connection.connect();
140 } catch (IOException e1) {
141 e1.printStackTrace();
143 Client client = new Client(connection);
144 boolean checkNow = false;
145 int currentEdition = lastUpdateEdition;
146 while (!shouldStop()) {
148 System.out.println("Trying " + constructUpdateKey(currentEdition));
149 ClientGet clientGet = new ClientGet("get-update-key");
150 clientGet.setUri(constructUpdateKey(currentEdition));
151 clientGet.setPersistence(Persistence.CONNECTION);
152 clientGet.setReturnType(ReturnType.direct);
153 clientGet.setVerbosity(Verbosity.ALL);
155 client.execute(clientGet);
156 boolean stop = false;
158 Message message = client.readMessage();
159 System.out.println(message);
160 if ("GetFailed".equals(message.getName())) {
161 if ("27".equals(message.get("code"))) {
162 String editionString = message.get("redirecturi").split("/")[2];
163 int editionNumber = -1;
165 editionNumber = Integer.parseInt(editionString);
166 } catch (NumberFormatException nfe1) {
169 if (editionNumber != -1) {
170 System.out.println("Found new edition " + editionNumber);
171 currentEdition = editionNumber;
172 lastUpdateEdition = editionNumber;
178 if ("AllData".equals(message.getName())) {
179 System.out.println("Update data found.");
180 InputStream dataInputStream = null;
181 Properties properties = new Properties();
183 dataInputStream = message.getPayloadInputStream();
184 properties.load(dataInputStream);
186 Closer.close(dataInputStream);
189 String foundVersionString = properties.getProperty("jSite.Version");
190 if (foundVersionString != null) {
191 Version foundVersion = Version.parse(foundVersionString);
192 if (foundVersion != null) {
193 lastVersion = foundVersion;
194 String versionTimestampString = properties.getProperty("jSite.Date");
195 System.out.println(versionTimestampString);
196 long versionTimestamp = -1;
198 versionTimestamp = Long.parseLong(versionTimestampString);
199 } catch (NumberFormatException nfe1) {
202 fireUpdateFound(foundVersion, versionTimestamp);
210 } catch (IOException e) {
211 System.out.println("Got IOException: " + e.getMessage());
214 if (!checkNow && !shouldStop()) {
215 synchronized (syncObject) {
217 syncObject.wait(15 * 60 * 1000);
218 } catch (InterruptedException ie1) {