X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FUpdateChecker.java;h=e739a447d9dda36eca8f4e542769886caffdec96;hb=648e6f419534c315ade5a506e8ec8820872ee7c8;hp=0bd7bd2eac817e279e7d92d74b9945b07aa42f38;hpb=15ea1aae6f3f26c380ee412510f628df0d970015;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java b/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java
index 0bd7bd2..e739a44 100644
--- a/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java
+++ b/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java
@@ -1,5 +1,5 @@
/*
- * Sone - UpdateChecker.java - Copyright © 2011 David Roden
+ * Sone - UpdateChecker.java - Copyright © 2011â2016 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -17,6 +17,8 @@
package net.pterodactylus.sone.core;
+import static java.util.logging.Logger.getLogger;
+
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -26,12 +28,17 @@ import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.inject.Singleton;
+
+import net.pterodactylus.sone.core.FreenetInterface.Fetched;
+import net.pterodactylus.sone.core.event.UpdateFoundEvent;
import net.pterodactylus.sone.main.SonePlugin;
-import net.pterodactylus.util.collection.Pair;
import net.pterodactylus.util.io.Closer;
-import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.version.Version;
-import freenet.client.FetchResult;
+
+import com.google.common.eventbus.EventBus;
+import com.google.inject.Inject;
+
import freenet.keys.FreenetURI;
import freenet.support.api.Bucket;
@@ -40,31 +47,27 @@ import freenet.support.api.Bucket;
*
* @author David âBombeâ Roden
*/
+@Singleton
public class UpdateChecker {
/** The logger. */
- private static final Logger logger = Logging.getLogger(UpdateChecker.class);
-
- /** The key of the Sone homepage. */
- private static final String SONE_HOMEPAGE = "USK@nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI,DuQSUZiI~agF8c-6tjsFFGuZ8eICrzWCILB60nT8KKo,AQACAAE/sone/";
+ private static final Logger logger = getLogger(UpdateChecker.class.getName());
- /** The current latest known edition. */
- private static final int LATEST_EDITION = 48;
+ /** The event bus. */
+ private final EventBus eventBus;
/** The Freenet interface. */
private final FreenetInterface freenetInterface;
- /** The update listener manager. */
- private final UpdateListenerManager updateListenerManager = new UpdateListenerManager();
-
/** The current URI of the homepage. */
private FreenetURI currentUri;
/** The latest known edition. */
- private long latestEdition;
+ private long latestEdition = SonePlugin.getLatestEdition();
/** The current latest known version. */
- private Version currentLatestVersion = SonePlugin.VERSION;
+ private Version currentLatestVersion;
+ private final Version currentRunningVersion;
/** The release date of the latest version. */
private long latestVersionDate;
@@ -72,35 +75,17 @@ public class UpdateChecker {
/**
* Creates a new update checker.
*
+ * @param eventBus
+ * The event bus
* @param freenetInterface
* The freenet interface to use
*/
- public UpdateChecker(FreenetInterface freenetInterface) {
+ @Inject
+ public UpdateChecker(EventBus eventBus, FreenetInterface freenetInterface, Version currentVersion) {
+ this.eventBus = eventBus;
this.freenetInterface = freenetInterface;
- }
-
- //
- // EVENT LISTENER MANAGEMENT
- //
-
- /**
- * Adds the given listener to the list of registered listeners.
- *
- * @param updateListener
- * The listener to add
- */
- public void addUpdateListener(UpdateListener updateListener) {
- updateListenerManager.addListener(updateListener);
- }
-
- /**
- * Removes the given listener from the list of registered listeners.
- *
- * @param updateListener
- * The listener to remove
- */
- public void removeUpdateListener(UpdateListener updateListener) {
- updateListenerManager.removeListener(updateListener);
+ this.currentRunningVersion = currentVersion;
+ this.currentLatestVersion = currentVersion;
}
//
@@ -114,7 +99,7 @@ public class UpdateChecker {
* @return {@code true} if a new version was found
*/
public boolean hasLatestVersion() {
- return currentLatestVersion.compareTo(SonePlugin.VERSION) > 0;
+ return currentLatestVersion.compareTo(currentRunningVersion) > 0;
}
/**
@@ -156,7 +141,7 @@ public class UpdateChecker {
*/
public void start() {
try {
- currentUri = new FreenetURI(SONE_HOMEPAGE + LATEST_EDITION);
+ currentUri = new FreenetURI(SonePlugin.getHomepage());
} catch (MalformedURLException mue1) {
/* this can not really happen unless I screw up. */
logger.log(Level.SEVERE, "Sone Homepage URI invalid!", mue1);
@@ -166,19 +151,19 @@ public class UpdateChecker {
@Override
@SuppressWarnings("synthetic-access")
public void editionFound(FreenetURI uri, long edition, boolean newKnownGood, boolean newSlot) {
- logger.log(Level.FINEST, "Found update for %s: %d, %s, %s", new Object[] { uri, edition, newKnownGood, newSlot });
+ logger.log(Level.FINEST, String.format("Found update for %s: %d, %s, %s", uri, edition, newKnownGood, newSlot));
if (newKnownGood || newSlot) {
- Pair uriResult = freenetInterface.fetchUri(uri.setMetaString(new String[] { "sone.properties" }));
+ Fetched uriResult = freenetInterface.fetchUri(uri.setMetaString(new String[] { "sone.properties" }));
if (uriResult == null) {
- logger.log(Level.WARNING, "Could not fetch properties of latest homepage: %s", uri);
+ logger.log(Level.WARNING, String.format("Could not fetch properties of latest homepage: %s", uri));
return;
}
- Bucket resultBucket = uriResult.getRight().asBucket();
+ Bucket resultBucket = uriResult.getFetchResult().asBucket();
try {
parseProperties(resultBucket.getInputStream(), edition);
latestEdition = edition;
} catch (IOException ioe1) {
- logger.log(Level.WARNING, "Could not parse sone.properties of " + uri, ioe1);
+ logger.log(Level.WARNING, String.format("Could not parse sone.properties of %s!", uri), ioe1);
} finally {
resultBucket.free();
}
@@ -202,8 +187,7 @@ public class UpdateChecker {
* Parses the properties of the latest version and fires events, if
* necessary.
*
- * @see UpdateListener#updateFound(Version, long, long)
- * @see UpdateListenerManager#fireUpdateFound(Version, long, long)
+ * @see UpdateFoundEvent
* @param propertiesInputStream
* The input stream to parse
* @param edition
@@ -240,9 +224,22 @@ public class UpdateChecker {
if (version.compareTo(currentLatestVersion) > 0) {
currentLatestVersion = version;
latestVersionDate = releaseTime;
- logger.log(Level.INFO, "Found new version: %s (%tc)", new Object[] { version, new Date(releaseTime) });
- updateListenerManager.fireUpdateFound(version, releaseTime, edition);
+ boolean disruptive = disruptiveVersionBetweenCurrentAndFound(properties);
+ logger.log(Level.INFO, String.format("Found new version: %s (%tc%s)", version, new Date(releaseTime), disruptive ? ", disruptive" : ""));
+ eventBus.post(new UpdateFoundEvent(version, releaseTime, edition, disruptive));
+ }
+ }
+
+ private boolean disruptiveVersionBetweenCurrentAndFound(Properties properties) {
+ for (String key : properties.stringPropertyNames()) {
+ if (key.startsWith("DisruptiveVersion/")) {
+ Version disruptiveVersion = Version.parse(key.substring("DisruptiveVersion/".length()));
+ if (disruptiveVersion.compareTo(currentRunningVersion) > 0) {
+ return true;
+ }
+ }
}
+ return false;
}
}