Browse Source

Added rTorrent addByFile support.

rewrite-connect
Eric Kok 8 years ago
parent
commit
721032e5ce
  1. 3
      connect/build.gradle
  2. 6
      connect/src/main/java/org/transdroid/connect/clients/rtorrent/LoadRawStart.kt
  3. 27
      connect/src/main/java/org/transdroid/connect/clients/rtorrent/Rtorrent.kt
  4. 10
      connect/src/main/java/org/transdroid/connect/clients/rtorrent/Service.kt
  5. 11
      connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentLiveTest.kt
  6. 35
      connect/src/test/java/org/transdroid/connect/clients/rtorrent/RtorrentMockTest.kt
  7. 4
      connect/src/test/java/org/transdroid/connect/mock/MockTorrent.kt
  8. BIN
      connect/src/test/resources/test/ubuntu.torrent

3
connect/build.gradle

@ -1,4 +1,3 @@
apply plugin: 'java'
apply plugin: 'kotlin' apply plugin: 'kotlin'
dependencies { dependencies {
@ -6,7 +5,7 @@ dependencies {
compile 'com.squareup.okhttp3:logging-interceptor:3.7.0' compile 'com.squareup.okhttp3:logging-interceptor:3.7.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0' compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
compile 'com.burgstaller:okhttp-digest:1.10' compile 'com.burgstaller:okhttp-digest:1.10'
compile 'com.github.erickok:retrofit-xmlrpc:6e2c623763' compile 'com.github.erickok:retrofit-xmlrpc:77b5e30e43'
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
testCompile 'com.google.truth:truth:0.31' testCompile 'com.google.truth:truth:0.31'

6
connect/src/main/java/org/transdroid/connect/clients/rtorrent/LoadRawStart.kt

@ -0,0 +1,6 @@
package org.transdroid.connect.clients.rtorrent
data class LoadRawStart(
val endpoint: String,
val bytes: ByteArray
)

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

@ -3,7 +3,6 @@ package org.transdroid.connect.clients.rtorrent
import io.reactivex.Completable import io.reactivex.Completable
import io.reactivex.Flowable import io.reactivex.Flowable
import io.reactivex.Single import io.reactivex.Single
import nl.nl2312.xmlrpc.Nothing
import nl.nl2312.xmlrpc.XmlRpcConverterFactory import nl.nl2312.xmlrpc.XmlRpcConverterFactory
import org.transdroid.connect.Configuration import org.transdroid.connect.Configuration
import org.transdroid.connect.clients.Feature import org.transdroid.connect.clients.Feature
@ -13,6 +12,7 @@ import org.transdroid.connect.util.OkHttpBuilder
import org.transdroid.connect.util.flatten import org.transdroid.connect.util.flatten
import retrofit2.Retrofit import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import java.io.InputStream
import java.util.* import java.util.*
class Rtorrent(private val configuration: Configuration) : class Rtorrent(private val configuration: Configuration) :
@ -20,10 +20,13 @@ class Rtorrent(private val configuration: Configuration) :
Feature.Listing, Feature.Listing,
Feature.StartingStopping, Feature.StartingStopping,
Feature.ResumingPausing, Feature.ResumingPausing,
//Feature.AddByFile, Feature.AddByFile,
Feature.AddByUrl, Feature.AddByUrl,
Feature.AddByMagnet { Feature.AddByMagnet {
private val xmlrpcSizeMinimum = 2 * 1024 * 1024
private val xmlrpcSizePadding = 1280
private val service: Service = Retrofit.Builder() private val service: Service = Retrofit.Builder()
.baseUrl(configuration.baseUrl) .baseUrl(configuration.baseUrl)
.client(OkHttpBuilder.build(configuration)) .client(OkHttpBuilder.build(configuration))
@ -61,7 +64,7 @@ class Rtorrent(private val configuration: Configuration) :
.build().create(Service::class.java) .build().create(Service::class.java)
override fun clientVersion(): Single<String> { override fun clientVersion(): Single<String> {
return service.clientVersion(configuration.endpoint, Nothing.NOTHING) return service.clientVersion(configuration.endpoint)
.cache() // Cached, as it is often used but 'never' changes .cache() // Cached, as it is often used but 'never' changes
} }
@ -139,7 +142,7 @@ class Rtorrent(private val configuration: Configuration) :
override fun addByUrl(url: String): Completable { override fun addByUrl(url: String): Completable {
return clientVersion().asVersionInt().flatMapCompletable { integer -> return clientVersion().asVersionInt().flatMapCompletable { 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)
@ -149,7 +152,7 @@ class Rtorrent(private val configuration: Configuration) :
override fun addByMagnet(magnet: String): Completable { override fun addByMagnet(magnet: String): Completable {
return clientVersion().asVersionInt().flatMapCompletable { integer -> return clientVersion().asVersionInt().flatMapCompletable { 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)
@ -157,6 +160,20 @@ class Rtorrent(private val configuration: Configuration) :
} }
} }
override fun addByFile(file: InputStream): Completable {
return clientVersion().asVersionInt().flatMapCompletable { 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) }
} else {
service.networkSizeLimitSet(configuration.endpoint, size)
.flatMapCompletable { service.loadRawStart(configuration.endpoint, bytes) }
}
}
}
private fun torrentStatus(state: Long, complete: Long, active: Long, checking: Long): TorrentStatus { private fun torrentStatus(state: Long, complete: Long, active: Long, checking: Long): TorrentStatus {
if (state == 0L) { if (state == 0L) {
return TorrentStatus.QUEUED return TorrentStatus.QUEUED

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

@ -13,7 +13,7 @@ internal interface Service {
@XmlRpc("system.client_version") @XmlRpc("system.client_version")
@POST("{endpoint}") @POST("{endpoint}")
fun clientVersion(@Path("endpoint") endpoint: String?, @Body nothing: Nothing): Single<String> fun clientVersion(@Path("endpoint") endpoint: String?, @Body nothing: Nothing = Nothing.NOTHING): Single<String>
@XmlRpc("d.multicall2") @XmlRpc("d.multicall2")
@POST("{endpoint}") @POST("{endpoint}")
@ -31,4 +31,12 @@ internal interface Service {
@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): Completable
@XmlRpc("load.raw_start")
@POST("{endpoint}")
fun loadRawStart(@Path("endpoint") endpoint: String?, @Body vararg args: Any): Completable
@XmlRpc("network.xmlrpc.size_limit.set")
@POST("{endpoint}")
fun networkSizeLimitSet(@Path("endpoint") endpoint: String?, @Body vararg args: Any): Single<Int>
} }

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

@ -8,6 +8,7 @@ import org.transdroid.connect.clients.Client
import org.transdroid.connect.clients.ClientSpec import org.transdroid.connect.clients.ClientSpec
import org.transdroid.connect.clients.Feature import org.transdroid.connect.clients.Feature
import org.transdroid.connect.clients.UnsupportedFeatureException import org.transdroid.connect.clients.UnsupportedFeatureException
import org.transdroid.connect.mock.MockTorrent
import org.transdroid.connect.model.Torrent import org.transdroid.connect.model.Torrent
class RtorrentLiveTest { class RtorrentLiveTest {
@ -17,8 +18,7 @@ class RtorrentLiveTest {
@Before @Before
fun setUp() { fun setUp() {
rtorrent = Configuration(Client.RTORRENT, rtorrent = Configuration(Client.RTORRENT,
"http://localhost:8008/", "http://localhost:8008/", "RPC2",
"RPC2",
loggingEnabled = true) loggingEnabled = true)
.createClient() .createClient()
} }
@ -64,6 +64,13 @@ class RtorrentLiveTest {
.assertNoErrors() .assertNoErrors()
} }
@Test
fun addByFile() {
rtorrent.addByFile(MockTorrent.torrentFile)
.test()
.assertNoErrors()
}
@Test @Test
fun start() { fun start() {
rtorrent.start(firstLiveTorrent()) rtorrent.start(firstLiveTorrent())

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

@ -6,6 +6,8 @@ import org.junit.Before
import org.junit.Test import org.junit.Test
import org.transdroid.connect.Configuration import org.transdroid.connect.Configuration
import org.transdroid.connect.clients.Client import org.transdroid.connect.clients.Client
import org.transdroid.connect.mock.MockTorrent
import java.io.File
class RtorrentMockTest { class RtorrentMockTest {
@ -20,7 +22,7 @@ class RtorrentMockTest {
@Test @Test
fun clientVersion() { fun clientVersion() {
server.enqueue(mock("<param><value><string>0.9.6</string></value></param>")) server.enqueue(mock("<string>0.9.6</string>"))
rtorrent.clientVersion() rtorrent.clientVersion()
.test() .test()
.assertValue("0.9.6") .assertValue("0.9.6")
@ -29,7 +31,7 @@ class RtorrentMockTest {
@Test @Test
fun torrents() { fun torrents() {
server.enqueue(mock("<param><value><array><data><value><array><data><value><string>59066769B9AD42DA2E508611C33D7C4480B3857B</string></value><value><string>ubuntu-17.04-desktop-amd64.iso</string></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>1609039872</i8></value><value><i8>1609039872</i8></value><value><i8>1492077159</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><string></string></value><value><string></string></value><value><string></string></value><value><string></string></value><value><string></string></value><value><string></string></value><value><i8>0</i8></value><value><i8>0</i8></value></data></array></value></data></array></value></param>")) server.enqueue(mock("<array><data><value><array><data><value><string>59066769B9AD42DA2E508611C33D7C4480B3857B</string></value><value><string>ubuntu-17.04-desktop-amd64.iso</string></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>1609039872</i8></value><value><i8>1609039872</i8></value><value><i8>1492077159</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><i8>0</i8></value><value><string></string></value><value><string></string></value><value><string></string></value><value><string></string></value><value><string></string></value><value><string></string></value><value><i8>0</i8></value><value><i8>0</i8></value></data></array></value></data></array>"))
rtorrent.torrents() rtorrent.torrents()
.test() .test()
.assertValue { it.hash == "59066769B9AD42DA2E508611C33D7C4480B3857B" } .assertValue { it.hash == "59066769B9AD42DA2E508611C33D7C4480B3857B" }
@ -38,9 +40,9 @@ class RtorrentMockTest {
@Test @Test
fun addByUrl() { fun addByUrl() {
server.enqueue(mock("<param><value><string>0.9.6</string></value></param>")) server.enqueue(mock("<string>0.9.6</string>"))
server.enqueue(mock("<param><value><i4>0</i4></value></param>")) server.enqueue(mock("<i4>0</i4>"))
rtorrent.addByUrl("http://releases.ubuntu.com/17.04/ubuntu-17.04-desktop-amd64.iso.torrent") rtorrent.addByUrl(MockTorrent.torrentUrl)
.test() .test()
.assertNoErrors() .assertNoErrors()
server.takeRequest() server.takeRequest()
@ -49,9 +51,20 @@ class RtorrentMockTest {
@Test @Test
fun addByMagnet() { fun addByMagnet() {
server.enqueue(mock("<param><value><string>0.9.6</string></value></param>")) server.enqueue(mock("<string>0.9.6</string>"))
server.enqueue(mock("<param><value><i4>0</i4></value></param>")) server.enqueue(mock("<i4>0</i4>"))
rtorrent.addByMagnet("http://torrent.ubuntu.com:6969/file?info_hash=%04%03%FBG%28%BDx%8F%BC%B6%7E%87%D6%FE%B2A%EF8%C7Z") rtorrent.addByMagnet(MockTorrent.magnetUrl)
.test()
.assertNoErrors()
server.takeRequest()
server.takeRequest()
}
@Test
fun addByFile() {
server.enqueue(mock("<string>0.9.6</string>"))
server.enqueue(mock("<i4>0</i4>"))
rtorrent.addByFile(MockTorrent.torrentFile)
.test() .test()
.assertNoErrors() .assertNoErrors()
server.takeRequest() server.takeRequest()
@ -60,7 +73,7 @@ class RtorrentMockTest {
@Test @Test
fun start() { fun start() {
server.enqueue(mock("<param><value><i4>0</i4></value></param>")) server.enqueue(mock("<i4>0</i4>"))
rtorrent.start(MockTorrent.downloading) rtorrent.start(MockTorrent.downloading)
.test() .test()
.assertValue { it.canStop } .assertValue { it.canStop }
@ -69,7 +82,7 @@ class RtorrentMockTest {
@Test @Test
fun stop() { fun stop() {
server.enqueue(mock("<param><value><i4>0</i4></value></param>")) server.enqueue(mock("<i4>0</i4>"))
rtorrent.stop(MockTorrent.seeding) rtorrent.stop(MockTorrent.seeding)
.test() .test()
.assertValue { it.canStart } .assertValue { it.canStart }
@ -82,7 +95,7 @@ class RtorrentMockTest {
.setBody("<?xml version=\"1.0\"?>\n" + .setBody("<?xml version=\"1.0\"?>\n" +
"<methodResponse>\n" + "<methodResponse>\n" +
" <params>\n" + " <params>\n" +
" {$params}\n" + " <param><value>{$params}</value></param>\n" +
" </params>\n" + " </params>\n" +
"</methodResponse>") "</methodResponse>")
} }

4
connect/src/test/java/org/transdroid/connect/clients/rtorrent/MockTorrent.kt → connect/src/test/java/org/transdroid/connect/mock/MockTorrent.kt

@ -1,13 +1,15 @@
package org.transdroid.connect.clients.rtorrent package org.transdroid.connect.mock
import org.transdroid.connect.model.Torrent import org.transdroid.connect.model.Torrent
import org.transdroid.connect.model.TorrentStatus import org.transdroid.connect.model.TorrentStatus
import java.io.File
import java.util.* import java.util.*
object MockTorrent { object MockTorrent {
val torrentUrl = "http://releases.ubuntu.com/17.04/ubuntu-17.04-desktop-amd64.iso.torrent" val torrentUrl = "http://releases.ubuntu.com/17.04/ubuntu-17.04-desktop-amd64.iso.torrent"
val magnetUrl = "http://torrent.ubuntu.com:6969/file?info_hash=%04%03%FBG%28%BDx%8F%BC%B6%7E%87%D6%FE%B2A%EF8%C7Z" val magnetUrl = "http://torrent.ubuntu.com:6969/file?info_hash=%04%03%FBG%28%BDx%8F%BC%B6%7E%87%D6%FE%B2A%EF8%C7Z"
val torrentFile = File("connect/src/test/resources/test/ubuntu.torrent").inputStream()
val downloading = Torrent(0, "59066769B9AD42DA2E508611C33D7C4480B3857B", "ubuntu-17.04-desktop-amd64.iso", TorrentStatus.DOWNLOADING, val downloading = Torrent(0, "59066769B9AD42DA2E508611C33D7C4480B3857B", "ubuntu-17.04-desktop-amd64.iso", TorrentStatus.DOWNLOADING,
"/downloads/", 1000000, 200000, 20, 20, 2, 50, null, 804519936, 160903987, 1609039872, 0.5F, 0.8F, "distros", Date(1492681983), null, null) "/downloads/", 1000000, 200000, 20, 20, 2, 50, null, 804519936, 160903987, 1609039872, 0.5F, 0.8F, "distros", Date(1492681983), null, null)

BIN
connect/src/test/resources/test/ubuntu.torrent

Binary file not shown.
Loading…
Cancel
Save