Browse Source

Added rTorrent support for resuming/pausing.

rewrite-connect
Eric Kok 7 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 @@ -24,6 +24,18 @@ internal class ClientDelegate(private val client: Client, private val actual: An
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> {
if (client.supports(Feature.STARTING_STOPPING))
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<*>) { @@ -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 {

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

@ -129,49 +129,59 @@ class Rtorrent(private val configuration: Configuration) : @@ -129,49 +129,59 @@ class Rtorrent(private val configuration: Configuration) :
}
override fun start(torrent: Torrent): Single<Torrent> {
return service.start(
configuration.endpoint,
torrent.uniqueId).toSingle { torrent.mimicStart() }
return service.open(configuration.endpoint, torrent.uniqueId)
.flatMap { service.start(configuration.endpoint, torrent.uniqueId) }
.map { torrent.mimicStart() }
}
override fun stop(torrent: Torrent): Single<Torrent> {
return service.stop(
configuration.endpoint,
torrent.uniqueId).toSingle { torrent.mimicStop() }
return service.stop(configuration.endpoint, torrent.uniqueId)
.flatMap { service.close(configuration.endpoint, torrent.uniqueId) }
.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 {
return clientVersion().asVersionInt().flatMapCompletable { integer ->
return clientVersion().asVersionInt().flatMap { integer ->
if (integer >= 904) {
service.loadStart(configuration.endpoint, "", url)
} else {
service.loadStart(configuration.endpoint, url)
}
}
}.toCompletable()
}
override fun addByMagnet(magnet: String): Completable {
return clientVersion().asVersionInt().flatMapCompletable { integer ->
return clientVersion().asVersionInt().flatMap { integer ->
if (integer >= 904) {
service.loadStart(configuration.endpoint, "", magnet)
} else {
service.loadStart(configuration.endpoint, magnet)
}
}
}.toCompletable()
}
override fun addByFile(file: InputStream): Completable {
return clientVersion().asVersionInt().flatMapCompletable { integer ->
return clientVersion().asVersionInt().flatMap { integer ->
val bytes = file.readBytes()
val size = Math.max(bytes.size, xmlrpcSizeMinimum) + xmlrpcSizePadding
if (integer >= 904) {
service.networkSizeLimitSet(configuration.endpoint, "", size)
.flatMapCompletable { service.loadRawStart(configuration.endpoint, "", bytes) }
.flatMap { service.loadRawStart(configuration.endpoint, "", bytes) }
} else {
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 {

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

@ -21,19 +21,27 @@ internal interface Service { @@ -21,19 +21,27 @@ internal interface Service {
@XmlRpc("d.start")
@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")
@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")
@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")
@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")
@POST("{endpoint}")

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

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package org.transdroid.connect.clients.rtorrent
import com.burgstaller.okhttp.digest.Credentials
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
@ -85,6 +86,20 @@ class RtorrentLiveTest { @@ -85,6 +86,20 @@ class RtorrentLiveTest {
.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)
fun forceStart() {
rtorrent.forceStart(firstLiveTorrent())

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

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

Loading…
Cancel
Save