2005-03-23 jrandom
* Added more intelligent version checking in news.xml, in case we have a version newer than the one specified.
This commit is contained in:
@ -4,6 +4,7 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.DataHelper;
|
||||
@ -99,7 +100,7 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener {
|
||||
}
|
||||
|
||||
private static final String VERSION_STRING = "version=\"" + RouterVersion.VERSION + "\"";
|
||||
|
||||
private static final String VERSION_PREFIX = "version=\"";
|
||||
private void checkForUpdates() {
|
||||
File news = new File(NEWS_FILE);
|
||||
if (!news.exists()) return;
|
||||
@ -108,6 +109,26 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener {
|
||||
in = new FileInputStream(news);
|
||||
StringBuffer buf = new StringBuffer(128);
|
||||
while (DataHelper.readLine(in, buf)) {
|
||||
int index = buf.indexOf(VERSION_PREFIX);
|
||||
if (index == -1) {
|
||||
// skip
|
||||
} else {
|
||||
int end = buf.indexOf("\"", index + VERSION_PREFIX.length());
|
||||
if (end > index) {
|
||||
String ver = buf.substring(index+VERSION_PREFIX.length(), end);
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Found version: [" + ver + "]");
|
||||
if (needsUpdate(ver)) {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Our version is out of date, update!");
|
||||
break;
|
||||
} else {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Our version is current");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (buf.indexOf(VERSION_STRING) != -1) {
|
||||
if (_log.shouldLog(Log.DEBUG))
|
||||
_log.debug("Our version found, no need to update: " + buf.toString());
|
||||
@ -152,6 +173,54 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean needsUpdate(String version) {
|
||||
StringTokenizer newTok = new StringTokenizer(sanitize(version), ".");
|
||||
StringTokenizer ourTok = new StringTokenizer(sanitize(RouterVersion.VERSION), ".");
|
||||
|
||||
while (newTok.hasMoreTokens() && ourTok.hasMoreTokens()) {
|
||||
String newVer = newTok.nextToken();
|
||||
String oldVer = ourTok.nextToken();
|
||||
switch (compare(newVer, oldVer)) {
|
||||
case -1: // newVer is smaller
|
||||
return false;
|
||||
case 0: // eq
|
||||
break;
|
||||
case 1: // newVer is larger
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (newTok.hasMoreTokens() && !ourTok.hasMoreTokens())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static final String VALID = "0123456789.";
|
||||
private static final String sanitize(String str) {
|
||||
StringBuffer buf = new StringBuffer(str);
|
||||
for (int i = 0; i < buf.length(); i++) {
|
||||
if (VALID.indexOf(buf.charAt(i)) == -1) {
|
||||
buf.deleteCharAt(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
private static final int compare(String lhs, String rhs) {
|
||||
try {
|
||||
int left = Integer.parseInt(lhs);
|
||||
int right = Integer.parseInt(rhs);
|
||||
if (left < right)
|
||||
return -1;
|
||||
else if (left == right)
|
||||
return 0;
|
||||
else
|
||||
return 1;
|
||||
} catch (NumberFormatException nfe) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void attemptFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt, int numRetries, Exception cause) {
|
||||
// ignore
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
$Id: history.txt,v 1.177 2005/03/23 20:54:23 connelly Exp $
|
||||
$Id: history.txt,v 1.178 2005/03/23 21:38:10 jrandom Exp $
|
||||
|
||||
2005-03-23 jrandom
|
||||
* Added more intelligent version checking in news.xml, in case we have a
|
||||
version newer than the one specified.
|
||||
|
||||
2005-03-23 jrandom
|
||||
* Added support for Transfer-Encoding: chunked to the EepGet, so that the
|
||||
|
@ -15,9 +15,9 @@ import net.i2p.CoreVersion;
|
||||
*
|
||||
*/
|
||||
public class RouterVersion {
|
||||
public final static String ID = "$Revision: 1.170 $ $Date: 2005/03/23 20:19:53 $";
|
||||
public final static String ID = "$Revision: 1.171 $ $Date: 2005/03/23 21:38:11 $";
|
||||
public final static String VERSION = "0.5.0.3";
|
||||
public final static long BUILD = 4;
|
||||
public final static long BUILD = 5;
|
||||
public static void main(String args[]) {
|
||||
System.out.println("I2P Router version: " + VERSION);
|
||||
System.out.println("Router ID: " + RouterVersion.ID);
|
||||
|
Reference in New Issue
Block a user