Skip to content

Commit

Permalink
master - Fixed bug caused by extraneous TorrentPausedAlert.
Browse files Browse the repository at this point in the history
- Also removed DHT lock that I thought was needed m(o___O)m
  • Loading branch information
masterwok committed Aug 7, 2018
1 parent 3bb1cdd commit f7d3ddc
Showing 1 changed file with 13 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import java.io.File
import java.lang.ref.WeakReference
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLDecoder
import java.security.InvalidParameterException


Expand Down Expand Up @@ -44,7 +43,6 @@ class TorrentSession(

private val sessionParams = SessionParams(torrentSessionOptions.settingsPack)
private val alertListener = TorrentSessionAlertListener(this)
private val dhtLock = Object()

init {
if (!hasValidTorrentUri) {
Expand All @@ -54,6 +52,10 @@ class TorrentSession(
addListener(alertListener)
}

private fun isTorrentPaused(torrentHandle: TorrentHandle): Boolean =
torrentHandle.status().flags().and_(TorrentFlags.PAUSED).nonZero()
|| isPaused

private val hasValidTorrentUri: Boolean
get() {
val path = torrentUri.toString()
Expand Down Expand Up @@ -88,8 +90,6 @@ class TorrentSession(
}

when (alert.type()) {
AlertType.DHT_BOOTSTRAP -> torrentSession.get()?.onDhtBootstrap()
AlertType.DHT_STATS -> torrentSession.get()?.onDhtStats()
AlertType.METADATA_RECEIVED -> torrentSession.get()?.onMetadataReceived(alert as MetadataReceivedAlert)
AlertType.METADATA_FAILED -> torrentSession.get()?.onMetadataFailed(alert as MetadataFailedAlert)
AlertType.PIECE_FINISHED -> torrentSession.get()?.onPieceFinished(alert as PieceFinishedAlert)
Expand All @@ -114,20 +114,6 @@ class TorrentSession(

private fun isDhtReady() = stats().dhtNodes() >= torrentSessionOptions.dhtNodeMinimum

private fun onDhtStats() {
synchronized(dhtLock) {
if (isDhtReady()) {
dhtLock.notify()
}
}
}

private fun onDhtBootstrap() {
synchronized(dhtLock) {
dhtLock.notify()
}
}

private fun onMetadataReceived(metadataReceivedAlert: MetadataReceivedAlert) {
val torrentHandle = metadataReceivedAlert.handle()

Expand Down Expand Up @@ -222,6 +208,14 @@ class TorrentSession(
private fun onTorrentPaused(torrentPausedAlert: TorrentPausedAlert) {
val torrentHandle = torrentPausedAlert.handle()

// When the user pauses the session before the magnet is added, and then
// resumes the session, an extraneous TorrentPausedAlert occurs after the
// TorrentResumedAlert. Guard against this case as the session is not
// really paused at this point.
if (!isTorrentPaused(torrentHandle)) {
return
}

listener?.onTorrentPaused(
torrentHandle
, createSessionStatus(torrentHandle)
Expand Down Expand Up @@ -282,20 +276,6 @@ class TorrentSession(
)
}

private fun downloadUsingMagnetUri(
magnetUrl: String
, downloadLocation: File
) = synchronized(dhtLock) {
// We must wait for DHT to start
if (!isDhtReady()) {
dhtLock.wait()
}

download(
URLDecoder.decode(magnetUrl, "utf-8")
, downloadLocation
)
}

/**
* Download a torrent from the [torrentUrl] to the [downloadLocation] destination.
Expand Down Expand Up @@ -365,7 +345,7 @@ class TorrentSession(
}

if (path.startsWith("magnet")) {
downloadUsingMagnetUri(
download(
path
, torrentSessionOptions.downloadLocation
)
Expand Down

0 comments on commit f7d3ddc

Please sign in to comment.