From b2df567d715624e82045919ade3c7bffa9daf795 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 20 Sep 2010 17:54:44 +0000 Subject: [PATCH] Retire never used Snark-based bittorrent downloader. Cool idea, but never turned out to be worth the PITA. Maybe someday when there's a broadly used trackerless p2p technology, we'll take another crack at this sort of thing. --- etc/libs-incl.xml | 1 - .../threerings/getdown/data/Application.java | 13 -- .../threerings/getdown/launcher/Getdown.java | 25 +-- .../getdown/net/TorrentDownloader.java | 180 ------------------ 4 files changed, 1 insertion(+), 218 deletions(-) delete mode 100644 src/main/java/com/threerings/getdown/net/TorrentDownloader.java diff --git a/etc/libs-incl.xml b/etc/libs-incl.xml index 12d69a5..3f1d973 100644 --- a/etc/libs-incl.xml +++ b/etc/libs-incl.xml @@ -8,7 +8,6 @@ - diff --git a/src/main/java/com/threerings/getdown/data/Application.java b/src/main/java/com/threerings/getdown/data/Application.java index 12d43cd..a68a498 100644 --- a/src/main/java/com/threerings/getdown/data/Application.java +++ b/src/main/java/com/threerings/getdown/data/Application.java @@ -195,15 +195,6 @@ public class Application } } - /** - * Indicates whether or not we support downloading of our resources using the Bittorrent - * protocol. - */ - public boolean getUseTorrent () - { - return _useTorrent; - } - /** * Returns a resource that refers to the application configuration file itself. */ @@ -585,9 +576,6 @@ public class Application // look for custom arguments fillAssignmentListFromPairs("extra.txt", _jvmargs); - // determine whether or not we should be using bit torrent - _useTorrent = (cdata.get("torrent") != null) || (System.getProperty("torrent") != null); - // determine whether we want to allow offline operation (defaults to false) _allowOffline = Boolean.parseBoolean((String)cdata.get("allow_offline")); @@ -1451,7 +1439,6 @@ public class Application protected String _name; protected String _dockIconPath; protected boolean _windebug; - protected boolean _useTorrent; protected boolean _allowOffline; protected String _trackingURL; diff --git a/src/main/java/com/threerings/getdown/launcher/Getdown.java b/src/main/java/com/threerings/getdown/launcher/Getdown.java index 69bec4f..18ff35b 100644 --- a/src/main/java/com/threerings/getdown/launcher/Getdown.java +++ b/src/main/java/com/threerings/getdown/launcher/Getdown.java @@ -66,7 +66,6 @@ import com.threerings.getdown.data.Application; import com.threerings.getdown.data.Resource; import com.threerings.getdown.net.Downloader; import com.threerings.getdown.net.HTTPDownloader; -import com.threerings.getdown.net.TorrentDownloader; import com.threerings.getdown.tools.Patcher; import com.threerings.getdown.util.ConfigUtil; import com.threerings.getdown.util.LaunchUtil; @@ -683,27 +682,8 @@ public abstract class Getdown extends Thread protected int _lastCheck = -1; }; - // assume we're going to use an HTTP downloader - Downloader dl = new HTTPDownloader(resources, obs); - - // if torrent downloading is enabled and we are downloading the right set of resources (a - // single patch file or the entire app from scratch), then use a torrent downloader - // instead. Because many of our installers also bundle background.png, and might bundle - // more required files, we need to allow a 'fudge factor' threshhold for determining at - // which point it is faster to torrent, and at which point we should use HTTP. - if (_app.getUseTorrent()) { - int verifiedResources = _app.getAllResources().size() - resources.size(); - if (verifiedResources <= MAX_TORRENT_VERIFIED_RESOURCES) { - 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); - } - } - // start the download and wait for it to complete + Downloader dl = new HTTPDownloader(resources, obs); if (!dl.download()) { if (Thread.interrupted()) { throw new InterruptedException("m.applet_stopped"); @@ -1034,9 +1014,6 @@ public abstract class Getdown extends Thread /** Number of minutes to wait after startup before beginning any real heavy lifting. */ protected int _delay; - /** The maximum number of resources that can be already present for bittorrent to be used. */ - protected static final int MAX_TORRENT_VERIFIED_RESOURCES = 1; - protected static final int MAX_LOOPS = 5; protected static final long MIN_EXIST_TIME = 5000L; protected static final String PROXY_REGISTRY = diff --git a/src/main/java/com/threerings/getdown/net/TorrentDownloader.java b/src/main/java/com/threerings/getdown/net/TorrentDownloader.java deleted file mode 100644 index 3a77cb3..0000000 --- a/src/main/java/com/threerings/getdown/net/TorrentDownloader.java +++ /dev/null @@ -1,180 +0,0 @@ -// -// $Id$ -// -// Getdown - application installer, patcher and launcher -// Copyright (C) 2004-2010 Three Rings Design, Inc. -// -// Redistribution and use in source and binary forms, with or without modification, are permitted -// provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of -// conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of -// conditions and the following disclaimer in the documentation and/or other materials provided -// with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, -// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED -// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -package com.threerings.getdown.net; - -import java.io.IOException; -import java.util.HashMap; -import java.util.List; - -import org.klomp.snark.Snark; -import org.klomp.snark.SnarkShutdown; - -import com.threerings.getdown.data.Resource; - -import static com.threerings.getdown.Log.log; - -/** - * Implements downloading data using BitTorrent - */ -public class TorrentDownloader extends Downloader -{ - public TorrentDownloader (List resources, Observer obs) - { - super(resources, obs); - log.info("Using bittorrent to fetch files"); - for (Resource resource : resources) { - String url = resource.getRemote().toString() + ".torrent"; - Snark snark = new Snark(url, null, -1, null, null); - SnarkShutdown snarkStopper = new SnarkShutdown(snark, null); - Runtime.getRuntime().addShutdownHook(snarkStopper); - _torrentmap.put(resource, snark); - _stoppermap.put(resource, snarkStopper); - if (resource.getPath().equals("full")) { - _metaDownload = true; - break; - } - } - } - - // documentation inherited - @Override - 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 { - snark.setupNetwork(); - length = snark.meta.getTotalLength(); - } catch (IOException ioe) { - log.warning("Bittorrent failed, falling back to HTTP"); - SnarkShutdown stopper = _stoppermap.get(rsrc); - stopper.run(); - Runtime.getRuntime().removeShutdownHook(stopper); - fallback(); - if (_metaDownload && rsrc.getPath().equals("full")) { - length = 0; - } else { - length = _fallback.checkSize(rsrc); - } - _metaDownload = false; - } - return length; - } - - // documentation inherited - @Override - protected void doDownload(Resource rsrc) - throws IOException - { - 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(); - // Override the start time, since Snark allocates storage prior to - // doing any downloading - _start = System.currentTimeMillis(); - while (!snark.coordinator.completed()) { - long now = System.currentTimeMillis(); - if ((now - _lastUpdate) >= UPDATE_DELAY) { - _currentSize = snark.coordinator.getDownloaded(); - if ((_currentSize < SIZE_THRESHOLD && - (now - _start) >= TIME_THRESHOLD)) { - log.info("Torrenting too slow, falling back to HTTP."); - // The download isn't going as planned, abort; - snarkStopper.run(); - Runtime.getRuntime().removeShutdownHook(snarkStopper); - snarkStopper = null; - _stoppermap.remove(rsrc); - if (_metaDownload) { - _metaDownload = false; - } - fallback(); - return; - } - } - updateObserver(); - } - // Manually set completion, just to be extra-safe. - _currentSize = _totalSize; - updateObserver(); - - if (snarkStopper != null) { - snarkStopper.run(); - Runtime.getRuntime().removeShutdownHook(snarkStopper); - _stoppermap.remove(rsrc); - } - } - - /** - * If torrent downloading either bugs out or is too slow, switch to a - * different method by creating the fallback downloader. - */ - protected void fallback () - { - _fallback = new HTTPDownloader(_resources, _obs, _totalSize); - } - - /** Keeps a mapping of resource names to torrent downloaders */ - protected HashMap _torrentmap = - new HashMap(); - - /** Keeps a mapping of resource names to torrent stoppers */ - protected HashMap _stoppermap = - new HashMap(); - - /** If we fail, revert to using this HTTP download transport */ - protected HTTPDownloader _fallback = null; - - /** 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 - */ - protected static final long SIZE_THRESHOLD = 4000l; -}