From 5d16fd7140c4629a3dcf395bf5ce71ead914bc22 Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Mon, 24 Apr 2017 20:16:15 +0200 Subject: [PATCH] Added rTorrent support for resuming/pausing. --- .../connect/clients/ClientDelegate.kt | 12 ++++++ .../org/transdroid/connect/clients/Feature.kt | 8 +++- .../connect/clients/rtorrent/Rtorrent.kt | 38 ++++++++++++------- .../connect/clients/rtorrent/Service.kt | 16 ++++++-- .../clients/rtorrent/RtorrentLiveTest.kt | 15 ++++++++ .../clients/rtorrent/RtorrentMockTest.kt | 26 ++++++++++++- 6 files changed, 95 insertions(+), 20 deletions(-) diff --git a/connect/src/main/java/org/transdroid/connect/clients/ClientDelegate.kt b/connect/src/main/java/org/transdroid/connect/clients/ClientDelegate.kt index 86d86846..e30260db 100644 --- a/connect/src/main/java/org/transdroid/connect/clients/ClientDelegate.kt +++ b/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) } + override fun resume(torrent: Torrent): Single { + 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 { + 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 { if (client.supports(Feature.STARTING_STOPPING)) return (actual as Feature.StartingStopping).start(torrent) diff --git a/connect/src/main/java/org/transdroid/connect/clients/Feature.kt b/connect/src/main/java/org/transdroid/connect/clients/Feature.kt index 16c530ac..964de2ad 100644 --- a/connect/src/main/java/org/transdroid/connect/clients/Feature.kt +++ b/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 + + fun pause(torrent: Torrent): Single + + } interface ForceStarting { diff --git a/connect/src/main/java/org/transdroid/connect/clients/rtorrent/Rtorrent.kt b/connect/src/main/java/org/transdroid/connect/clients/rtorrent/Rtorrent.kt index 8dc604f4..a883fef1 100644 --- a/connect/src/main/java/org/transdroid/connect/clients/rtorrent/Rtorrent.kt +++ b/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 { - 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 { - 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 { + return service.start(configuration.endpoint, torrent.uniqueId) + .map { torrent.mimicResume() } + } + + override fun pause(torrent: Torrent): Single { + 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 { diff --git a/connect/src/main/java/org/transdroid/connect/clients/rtorrent/Service.kt b/connect/src/main/java/org/transdroid/connect/clients/rtorrent/Service.kt index 8dc67d72..66674eec 100644 --- a/connect/src/main/java/org/transdroid/connect/clients/rtorrent/Service.kt +++ b/connect/src/main/java/org/transdroid/connect/clients/rtorrent/Service.kt @@ -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 @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 + + @XmlRpc("d.open") + @POST("{endpoint}") + fun open(@Path("endpoint") endpoint: String?, @Body hash: String): Single + + @XmlRpc("d.close") + @POST("{endpoint}") + fun close(@Path("endpoint") endpoint: String?, @Body hash: String): Single @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 @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 @XmlRpc("network.xmlrpc.size_limit.set") @POST("{endpoint}") diff --git a/connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentLiveTest.kt b/connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentLiveTest.kt index 21799148..b622e17f 100644 --- a/connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentLiveTest.kt +++ b/connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentLiveTest.kt @@ -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 { .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()) diff --git a/connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentMockTest.kt b/connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentMockTest.kt index 33a0111d..002a6520 100644 --- a/connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentMockTest.kt +++ b/connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentMockTest.kt @@ -64,28 +64,52 @@ class RtorrentMockTest { fun addByFile() { server.enqueue(mock("0.9.6")) server.enqueue(mock("0")) + server.enqueue(mock("0")) rtorrent.addByFile(MockTorrent.torrentFile) .test() .assertNoErrors() server.takeRequest() server.takeRequest() + server.takeRequest() } @Test fun start() { + server.enqueue(mock("0")) server.enqueue(mock("0")) rtorrent.start(MockTorrent.downloading) .test() .assertValue { it.canStop } - server.takeRequest() + //server.takeRequest() + //server.takeRequest() } @Test fun stop() { + server.enqueue(mock("0")) server.enqueue(mock("0")) rtorrent.stop(MockTorrent.seeding) .test() .assertValue { it.canStart } + //server.takeRequest() + //server.takeRequest() + } + + @Test + fun resume() { + server.enqueue(mock("0")) + rtorrent.resume(MockTorrent.downloading) + .test() + .assertValue { it.canPause } + server.takeRequest() + } + + @Test + fun pause() { + server.enqueue(mock("0")) + rtorrent.pause(MockTorrent.seeding) + .test() + .assertValue { it.canResume } server.takeRequest() }