From ae36f9138dd8e153d69c897e434381a2db70489c Mon Sep 17 00:00:00 2001 From: Elizabeth Fong Date: Sun, 16 Jul 2006 09:49:54 +0000 Subject: [PATCH] wire things so both patches and full installs try bittorrent first --- etc/getdown.pro | 2 +- .../threerings/getdown/data/Application.java | 39 ++++++++++++++++--- .../threerings/getdown/launcher/Getdown.java | 15 ++++--- .../getdown/launcher/TorrentDownloader.java | 39 +++++++++++++++++-- 4 files changed, 79 insertions(+), 16 deletions(-) diff --git a/etc/getdown.pro b/etc/getdown.pro index 6d2b8be..a8fb173 100644 --- a/etc/getdown.pro +++ b/etc/getdown.pro @@ -9,7 +9,7 @@ -injars lib/jRegistryKey.jar(!META-INF/*) -injars lib/samskivert.jar(!META-INF/*,!**/velocity/**,!**/xml/**) -injars lib/commons-io.jar(!META-INF/*) -# -injars lib/snark.jar(!META-INF/*) +-injars lib/snark.jar(!META-INF/*) -libraryjars /lib/rt.jar -dontskipnonpubliclibraryclasses diff --git a/src/java/com/threerings/getdown/data/Application.java b/src/java/com/threerings/getdown/data/Application.java index 8edad56..2671d01 100644 --- a/src/java/com/threerings/getdown/data/Application.java +++ b/src/java/com/threerings/getdown/data/Application.java @@ -181,6 +181,18 @@ public class Application return _resources; } + /** + * Returns a list of all the {@link Resource} objects used by + * this application. + */ + public List getAllResources () + { + List allResources = new ArrayList(); + allResources.addAll(getCodeResources()); + allResources.addAll(getActiveResources()); + return allResources; + } + /** * Returns a list of all auxiliary resource groups defined by the * application. An auxiliary resource group is a collection of resource @@ -266,6 +278,24 @@ public class Application } } + /** + * Returns a resource that can be used to download an archive containing + * all files belonging to the application. + */ + public Resource getFullResource () + { + String file = "full"; + try { + URL remote = new URL(createVAppBase(_targetVersion), file); + return new Resource(file, remote, getLocalPath(file), false); + } catch (Exception e) { + Log.warning("Failed to create full resource path [file=" + file + + ", appbase=" + _appbase + ", tvers=" + _targetVersion + + ", error=" + e + "]."); + return null; + } + } + /** * Instructs the application to parse its getdown.txt * configuration and prepare itself for operation. The application @@ -757,10 +787,8 @@ public class Application */ public List verifyResources (ProgressObserver obs) { - ArrayList rsrcs = new ArrayList(); - ArrayList failures = new ArrayList(); - rsrcs.addAll(getCodeResources()); - rsrcs.addAll(getActiveResources()); + List rsrcs = getAllResources(); + List failures = new ArrayList(); // total up the file size of the resources to validate long totalSize = 0L; @@ -806,8 +834,7 @@ public class Application */ public void clearValidationMarkers () { - clearValidationMarkers(getCodeResources().iterator()); - clearValidationMarkers(getActiveResources().iterator()); + clearValidationMarkers(getAllResources().iterator()); } /** diff --git a/src/java/com/threerings/getdown/launcher/Getdown.java b/src/java/com/threerings/getdown/launcher/Getdown.java index edafe09..78c5cb4 100644 --- a/src/java/com/threerings/getdown/launcher/Getdown.java +++ b/src/java/com/threerings/getdown/launcher/Getdown.java @@ -504,13 +504,18 @@ public abstract class Getdown extends Thread } }; - // Torrent downloading is disabled by default until the kinks are out Downloader dl; -// if (false) { -// dl = new TorrentDownloader(resources, obs); -// } else { + if (resources.equals(_app.getAllResources())) { + ArrayList full = new ArrayList(); + full.add(_app.getFullResource()); + full.addAll(resources); + dl = new TorrentDownloader(full, obs); + } else if (resources.size() == 1 && + resources.get(0).getPath().startsWith("patch")) { + dl = new TorrentDownloader(resources, obs); + } else { dl = new HTTPDownloader(resources, obs); -// } + } dl.start(); // now wait for it to complete diff --git a/src/java/com/threerings/getdown/launcher/TorrentDownloader.java b/src/java/com/threerings/getdown/launcher/TorrentDownloader.java index 8893401..79947d7 100644 --- a/src/java/com/threerings/getdown/launcher/TorrentDownloader.java +++ b/src/java/com/threerings/getdown/launcher/TorrentDownloader.java @@ -27,6 +27,10 @@ public class TorrentDownloader extends Downloader Runtime.getRuntime().addShutdownHook(snarkStopper); _torrentmap.put(resource, snark); _stoppermap.put(resource, snarkStopper); + if (resource.getPath().equals("full")) { + _metaDownload = true; + break; + } } } @@ -34,6 +38,12 @@ public class TorrentDownloader extends Downloader protected long checkSize(Resource rsrc) throws IOException { + if (_metaDownload && !rsrc.getPath().equals("full")) { + return 0; + } + if (_fallback != null) { + return _fallback.checkSize(rsrc); + } Snark snark = _torrentmap.get(rsrc); long length = -1; try { @@ -41,9 +51,16 @@ public class TorrentDownloader extends Downloader length = snark.meta.getTotalLength(); } catch (IOException ioe) { Log.warning("Bittorrent failed, falling back to HTTP"); - _stoppermap.get(rsrc).run(); + SnarkShutdown stopper = _stoppermap.get(rsrc); + stopper.run(); + Runtime.getRuntime().removeShutdownHook(stopper); _fallback = new HTTPDownloader(_resources, _obs); - length = _fallback.checkSize(rsrc); + if (_metaDownload && rsrc.getPath().equals("full")) { + length = 0; + } else { + length = _fallback.checkSize(rsrc); + } + _metaDownload = false; } return length; } @@ -52,10 +69,17 @@ public class TorrentDownloader extends Downloader protected void doDownload(Resource rsrc) throws IOException { - if (_fallback != null) { - _fallback.doDownload(rsrc); + if (_metaDownload && !rsrc.getPath().equals("full")) { return; } + if (_fallback != null) { + if (rsrc.getPath().equals("full")) { + return; + } else { + _fallback.doDownload(rsrc); + return; + } + } Snark snark = _torrentmap.get(rsrc); SnarkShutdown snarkStopper = _stoppermap.get(rsrc); snark.collectPieces(); @@ -67,6 +91,7 @@ public class TorrentDownloader extends Downloader (now - _start) >= TIME_THRESHOLD) { // The download isn't going as planned, abort; snarkStopper.run(); + Runtime.getRuntime().removeShutdownHook(snarkStopper); _fallback = new HTTPDownloader(_resources, _obs); _fallback.doDownload(rsrc); return; @@ -91,6 +116,12 @@ public class TorrentDownloader extends Downloader /** The length of time before we check for adequate progress*/ protected static final long TIME_THRESHOLD = 60 * 1000l; + /** + * Whether we are downloading an artificially-generated metafile + * representing all of the {@link Resource}s at the end of the file. + */ + protected boolean _metaDownload = false; + /** * The minimum amount of data that must be downloaded within the * initial period in order to continue using BitTorrent