Browse Source

Added rTorrent support for resuming/pausing.

rewrite-connect
Eric Kok 8 years ago
parent
commit
5d16fd7140
  1. 12
      connect/src/main/java/org/transdroid/connect/clients/ClientDelegate.kt
  2. 8
      connect/src/main/java/org/transdroid/connect/clients/Feature.kt
  3. 38
      connect/src/main/java/org/transdroid/connect/clients/rtorrent/Rtorrent.kt
  4. 16
      connect/src/main/java/org/transdroid/connect/clients/rtorrent/Service.kt
  5. 15
      connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentLiveTest.kt
  6. 26
      connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentMockTest.kt

12
connect/src/main/java/org/transdroid/connect/clients/ClientDelegate.kt

@ -24,6 +24,18 @@ internal class ClientDelegate(private val client: Client, private val actual: An
throw UnsupportedFeatureException(client, Feature.VERSION) throw UnsupportedFeatureException(client, Feature.VERSION)
} }
override fun resume(torrent: Torrent): Single<Torrent> {
if (client.supports(Feature.RESUMING_PAUSING))
return (actual as Feature.ResumingPausing).resume(torrent)
throw UnsupportedFeatureException(client, Feature.RESUMING_PAUSING)
}
override fun pause(torrent: Torrent): Single<Torrent> {
if (client.supports(Feature.RESUMING_PAUSING))
return (actual as Feature.ResumingPausing).pause(torrent)
throw UnsupportedFeatureException(client, Feature.RESUMING_PAUSING)
}
override fun start(torrent: Torrent): Single<Torrent> { override fun start(torrent: Torrent): Single<Torrent> {
if (client.supports(Feature.STARTING_STOPPING)) if (client.supports(Feature.STARTING_STOPPING))
return (actual as Feature.StartingStopping).start(torrent) return (actual as Feature.StartingStopping).start(torrent)

8
connect/src/main/java/org/transdroid/connect/clients/Feature.kt

@ -41,7 +41,13 @@ enum class Feature constructor(val type: KClass<*>) {
} }
interface ResumingPausing interface ResumingPausing {
fun resume(torrent: Torrent): Single<Torrent>
fun pause(torrent: Torrent): Single<Torrent>
}
interface ForceStarting { interface ForceStarting {

38
connect/src/main/java/org/transdroid/connect/clients/rtorrent/Rtorrent.kt

@ -129,49 +129,59 @@ class Rtorrent(private val configuration: Configuration) :
} }
override fun start(torrent: Torrent): Single<Torrent> { override fun start(torrent: Torrent): Single<Torrent> {
return service.start( return service.open(configuration.endpoint, torrent.uniqueId)
configuration.endpoint, .flatMap { service.start(configuration.endpoint, torrent.uniqueId) }
torrent.uniqueId).toSingle { torrent.mimicStart() } .map { torrent.mimicStart() }
} }
override fun stop(torrent: Torrent): Single<Torrent> { override fun stop(torrent: Torrent): Single<Torrent> {
return service.stop( return service.stop(configuration.endpoint, torrent.uniqueId)
configuration.endpoint, .flatMap { service.close(configuration.endpoint, torrent.uniqueId) }
torrent.uniqueId).toSingle { torrent.mimicStop() } .map { torrent.mimicStop() }
}
override fun resume(torrent: Torrent): Single<Torrent> {
return service.start(configuration.endpoint, torrent.uniqueId)
.map { torrent.mimicResume() }
}
override fun pause(torrent: Torrent): Single<Torrent> {
return service.stop(configuration.endpoint, torrent.uniqueId)
.map { torrent.mimicPause() }
} }
override fun addByUrl(url: String): Completable { override fun addByUrl(url: String): Completable {
return clientVersion().asVersionInt().flatMapCompletable { integer -> return clientVersion().asVersionInt().flatMap { integer ->
if (integer >= 904) { if (integer >= 904) {
service.loadStart(configuration.endpoint, "", url) service.loadStart(configuration.endpoint, "", url)
} else { } else {
service.loadStart(configuration.endpoint, url) service.loadStart(configuration.endpoint, url)
} }
} }.toCompletable()
} }
override fun addByMagnet(magnet: String): Completable { override fun addByMagnet(magnet: String): Completable {
return clientVersion().asVersionInt().flatMapCompletable { integer -> return clientVersion().asVersionInt().flatMap { integer ->
if (integer >= 904) { if (integer >= 904) {
service.loadStart(configuration.endpoint, "", magnet) service.loadStart(configuration.endpoint, "", magnet)
} else { } else {
service.loadStart(configuration.endpoint, magnet) service.loadStart(configuration.endpoint, magnet)
} }
} }.toCompletable()
} }
override fun addByFile(file: InputStream): Completable { override fun addByFile(file: InputStream): Completable {
return clientVersion().asVersionInt().flatMapCompletable { integer -> return clientVersion().asVersionInt().flatMap { integer ->
val bytes = file.readBytes() val bytes = file.readBytes()
val size = Math.max(bytes.size, xmlrpcSizeMinimum) + xmlrpcSizePadding val size = Math.max(bytes.size, xmlrpcSizeMinimum) + xmlrpcSizePadding
if (integer >= 904) { if (integer >= 904) {
service.networkSizeLimitSet(configuration.endpoint, "", size) service.networkSizeLimitSet(configuration.endpoint, "", size)
.flatMapCompletable { service.loadRawStart(configuration.endpoint, "", bytes) } .flatMap { service.loadRawStart(configuration.endpoint, "", bytes) }
} else { } else {
service.networkSizeLimitSet(configuration.endpoint, size) service.networkSizeLimitSet(configuration.endpoint, size)
.flatMapCompletable { service.loadRawStart(configuration.endpoint, bytes) } .flatMap { service.loadRawStart(configuration.endpoint, bytes) }
} }
} }.toCompletable()
} }
private fun torrentStatus(state: Long, complete: Long, active: Long, checking: Long): TorrentStatus { private fun torrentStatus(state: Long, complete: Long, active: Long, checking: Long): TorrentStatus {

16
connect/src/main/java/org/transdroid/connect/clients/rtorrent/Service.kt

@ -21,19 +21,27 @@ internal interface Service {
@XmlRpc("d.start") @XmlRpc("d.start")
@POST("{endpoint}") @POST("{endpoint}")
fun start(@Path("endpoint") endpoint: String?, @Body hash: String): Completable fun start(@Path("endpoint") endpoint: String?, @Body hash: String): Single<Int>
@XmlRpc("d.stop") @XmlRpc("d.stop")
@POST("{endpoint}") @POST("{endpoint}")
fun stop(@Path("endpoint") endpoint: String?, @Body hash: String): Completable fun stop(@Path("endpoint") endpoint: String?, @Body hash: String): Single<Int>
@XmlRpc("d.open")
@POST("{endpoint}")
fun open(@Path("endpoint") endpoint: String?, @Body hash: String): Single<Int>
@XmlRpc("d.close")
@POST("{endpoint}")
fun close(@Path("endpoint") endpoint: String?, @Body hash: String): Single<Int>
@XmlRpc("load.start") @XmlRpc("load.start")
@POST("{endpoint}") @POST("{endpoint}")
fun loadStart(@Path("endpoint") endpoint: String?, @Body vararg args: String): Completable fun loadStart(@Path("endpoint") endpoint: String?, @Body vararg args: String): Single<Int>
@XmlRpc("load.raw_start") @XmlRpc("load.raw_start")
@POST("{endpoint}") @POST("{endpoint}")
fun loadRawStart(@Path("endpoint") endpoint: String?, @Body vararg args: Any): Completable fun loadRawStart(@Path("endpoint") endpoint: String?, @Body vararg args: Any): Single<Int>
@XmlRpc("network.xmlrpc.size_limit.set") @XmlRpc("network.xmlrpc.size_limit.set")
@POST("{endpoint}") @POST("{endpoint}")

15
connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentLiveTest.kt

@ -1,5 +1,6 @@
package org.transdroid.connect.clients.rtorrent package org.transdroid.connect.clients.rtorrent
import com.burgstaller.okhttp.digest.Credentials
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -85,6 +86,20 @@ class RtorrentLiveTest {
.assertValue({ it.canStart }) .assertValue({ it.canStart })
} }
@Test
fun resume() {
rtorrent.resume(firstLiveTorrent())
.test()
.assertValue({ it.canPause })
}
@Test
fun pause() {
rtorrent.pause(firstLiveTorrent())
.test()
.assertValue({ it.canResume })
}
@Test(expected = UnsupportedFeatureException::class) @Test(expected = UnsupportedFeatureException::class)
fun forceStart() { fun forceStart() {
rtorrent.forceStart(firstLiveTorrent()) rtorrent.forceStart(firstLiveTorrent())

26
connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentMockTest.kt

@ -64,28 +64,52 @@ class RtorrentMockTest {
fun addByFile() { fun addByFile() {
server.enqueue(mock("<string>0.9.6</string>")) server.enqueue(mock("<string>0.9.6</string>"))
server.enqueue(mock("<i4>0</i4>")) server.enqueue(mock("<i4>0</i4>"))
server.enqueue(mock("<i4>0</i4>"))
rtorrent.addByFile(MockTorrent.torrentFile) rtorrent.addByFile(MockTorrent.torrentFile)
.test() .test()
.assertNoErrors() .assertNoErrors()
server.takeRequest() server.takeRequest()
server.takeRequest() server.takeRequest()
server.takeRequest()
} }
@Test @Test
fun start() { fun start() {
server.enqueue(mock("<i4>0</i4>"))
server.enqueue(mock("<i4>0</i4>")) server.enqueue(mock("<i4>0</i4>"))
rtorrent.start(MockTorrent.downloading) rtorrent.start(MockTorrent.downloading)
.test() .test()
.assertValue { it.canStop } .assertValue { it.canStop }
server.takeRequest() //server.takeRequest()
//server.takeRequest()
} }
@Test @Test
fun stop() { fun stop() {
server.enqueue(mock("<i4>0</i4>"))
server.enqueue(mock("<i4>0</i4>")) server.enqueue(mock("<i4>0</i4>"))
rtorrent.stop(MockTorrent.seeding) rtorrent.stop(MockTorrent.seeding)
.test() .test()
.assertValue { it.canStart } .assertValue { it.canStart }
//server.takeRequest()
//server.takeRequest()
}
@Test
fun resume() {
server.enqueue(mock("<i4>0</i4>"))
rtorrent.resume(MockTorrent.downloading)
.test()
.assertValue { it.canPause }
server.takeRequest()
}
@Test
fun pause() {
server.enqueue(mock("<i4>0</i4>"))
rtorrent.pause(MockTorrent.seeding)
.test()
.assertValue { it.canResume }
server.takeRequest() server.takeRequest()
} }

Loading…
Cancel
Save