Now that Snark has proper exception handling, use it.

This commit is contained in:
Elizabeth Fong
2006-07-16 01:05:21 +00:00
parent cef42b0dd5
commit 7f5384629e
@@ -22,8 +22,11 @@ public class TorrentDownloader extends Downloader
for (Resource resource : resources) { for (Resource resource : resources) {
String url = resource.getRemote().toString() + ".torrent"; String url = resource.getRemote().toString() + ".torrent";
Snark snark = new Snark(url, null, -1, null, null); Snark snark = new Snark(url, null, -1, null, null);
snark.command_interpreter = false; SnarkShutdown snarkStopper = new SnarkShutdown(snark.storage,
snark.coordinator, snark.acceptor, snark.trackerclient, null);
Runtime.getRuntime().addShutdownHook(snarkStopper);
_torrentmap.put(resource, snark); _torrentmap.put(resource, snark);
_stoppermap.put(resource, snarkStopper);
} }
} }
@@ -36,11 +39,9 @@ public class TorrentDownloader extends Downloader
try { try {
snark.setupNetwork(); snark.setupNetwork();
length = snark.meta.getTotalLength(); length = snark.meta.getTotalLength();
} catch (Exception e) { } catch (IOException ioe) {
// Unfortunately, the snark library does System.exit(-1) right now
// instead of properly passing the exception up the chain.
Log.warning("Bittorrent failed, falling back to HTTP"); Log.warning("Bittorrent failed, falling back to HTTP");
snark.shutdown(); _stoppermap.get(rsrc).run();
_fallback = new HTTPDownloader(_resources, _obs); _fallback = new HTTPDownloader(_resources, _obs);
length = _fallback.checkSize(rsrc); length = _fallback.checkSize(rsrc);
} }
@@ -56,10 +57,8 @@ public class TorrentDownloader extends Downloader
return; return;
} }
Snark snark = _torrentmap.get(rsrc); Snark snark = _torrentmap.get(rsrc);
SnarkShutdown snarkStopper = _stoppermap.get(rsrc);
snark.collectPieces(); snark.collectPieces();
SnarkShutdown snarkhook = new SnarkShutdown(snark.storage,
snark.coordinator, snark.acceptor, snark.trackerclient, snark);
Runtime.getRuntime().addShutdownHook(snarkhook);
while (_currentSize != snark.meta.getTotalLength()) { while (_currentSize != snark.meta.getTotalLength()) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if ((now - _lastUpdate) >= UPDATE_DELAY) { if ((now - _lastUpdate) >= UPDATE_DELAY) {
@@ -67,7 +66,7 @@ public class TorrentDownloader extends Downloader
if (_currentSize < SIZE_THRESHOLD && if (_currentSize < SIZE_THRESHOLD &&
(now - _start) >= TIME_THRESHOLD) { (now - _start) >= TIME_THRESHOLD) {
// The download isn't going as planned, abort; // The download isn't going as planned, abort;
snark.shutdown(); snarkStopper.run();
_fallback = new HTTPDownloader(_resources, _obs); _fallback = new HTTPDownloader(_resources, _obs);
_fallback.doDownload(rsrc); _fallback.doDownload(rsrc);
return; return;
@@ -75,13 +74,17 @@ public class TorrentDownloader extends Downloader
} }
updateObserver(); updateObserver();
} }
snark.shutdown(); snarkStopper.run();
} }
/** Keeps a mapping of resource names to torrent downloaders */ /** Keeps a mapping of resource names to torrent downloaders */
protected HashMap<Resource, Snark> _torrentmap = protected HashMap<Resource, Snark> _torrentmap =
new HashMap<Resource, Snark>(); new HashMap<Resource, Snark>();
/** Keeps a mapping of resource names to torrent stoppers */
protected HashMap<Resource, SnarkShutdown> _stoppermap =
new HashMap<Resource, SnarkShutdown>();
/** If we fail, revert to using this HTTP download transport */ /** If we fail, revert to using this HTTP download transport */
protected HTTPDownloader _fallback = null; protected HTTPDownloader _fallback = null;