fix async message processing

This commit is contained in:
xis 2023-10-13 15:19:28 +02:00
parent aecc8c5973
commit 9d4ac85a81
5 changed files with 23 additions and 53 deletions

View file

@ -13,6 +13,8 @@ import org.jupnp.DefaultUpnpServiceConfiguration
import org.jupnp.UpnpService
import org.jupnp.UpnpServiceConfiguration
import org.jupnp.UpnpServiceImpl
import org.jupnp.model.message.StreamRequestMessage
import org.jupnp.model.message.StreamResponseMessage
import org.jupnp.model.meta.LocalDevice
import org.jupnp.protocol.ProtocolFactory
import org.jupnp.protocol.ProtocolFactoryImpl
@ -35,7 +37,7 @@ class DlnaService(
private val serverInfoProvider: ServerInfoProvider,
) {
private val addressesToBind: List<InetAddress> = listOf(serverInfoProvider.address!!)
var upnpService: UpnpService = MyUpnpService(MyUpnpServiceConfiguration())
var upnpService = MyUpnpService(MyUpnpServiceConfiguration())
fun start() {
upnpService.startup()
@ -52,13 +54,19 @@ class DlnaService(
upnpService.shutdown()
}
fun processRequest(requestMsg: StreamRequestMessage): StreamResponseMessage {
logger.debug { "Processing $requestMsg" }
return with(upnpService.protocolFactory.createReceivingSync(requestMsg)) {
run()
outputMessage
}.also {
logger.debug { "Response: ${it.operation.statusCode} ${it.body}" }
}
}
inner class MyUpnpService(
configuration: UpnpServiceConfiguration
) : UpnpServiceImpl(configuration) {
init {
protocolFactory = createProtocolFactory()
}
override fun createRegistry(pf: ProtocolFactory): Registry {
return RegistryImplWithOverrides(this)
}

View file

@ -2,9 +2,9 @@ package net.schowek.nextclouddlna.controller
import jakarta.servlet.http.HttpServletRequest
import mu.KLogging
import net.schowek.nextclouddlna.DlnaService
import net.schowek.nextclouddlna.dlna.media.MediaServer
import net.schowek.nextclouddlna.upnp.StreamRequestMapper
import net.schowek.nextclouddlna.upnp.UpnpStreamProcessor
import net.schowek.nextclouddlna.dlna.StreamRequestMapper
import org.springframework.core.io.InputStreamResource
import org.springframework.core.io.Resource
import org.springframework.http.HttpHeaders
@ -21,7 +21,7 @@ import org.springframework.web.bind.annotation.ResponseBody
@Controller
class UpnpController(
private val streamRequestMapper: StreamRequestMapper,
private val upnpStreamProcessor: UpnpStreamProcessor
private val dlnaService: DlnaService
) {
@RequestMapping(
method = [GET, HEAD], value = ["/dev/{uid}/icon.png"],
@ -50,7 +50,7 @@ class UpnpController(
request: HttpServletRequest
): ResponseEntity<Any> {
logger.info { "Upnp ${request.method} request from ${request.remoteAddr}: ${request.requestURI}" }
return with(upnpStreamProcessor.processMessage(streamRequestMapper.map(request))) {
return with(dlnaService.processRequest(streamRequestMapper.map(request))) {
ResponseEntity(
body,
HttpHeaders().also { h -> headers.entries.forEach { e -> h.add(e.key, e.value.joinToString { it }) } },

View file

@ -1,4 +1,4 @@
package net.schowek.nextclouddlna.upnp
package net.schowek.nextclouddlna.dlna
import jakarta.servlet.http.HttpServletRequest
import mu.KLogging

View file

@ -16,6 +16,8 @@ import org.jupnp.support.model.item.Item
import org.springframework.stereotype.Component
import java.util.*
import java.util.concurrent.TimeUnit.NANOSECONDS
import kotlin.math.max
import kotlin.math.min
@Component
@ -89,12 +91,12 @@ class ContentDirectoryService(
val didl = DIDLContent()
if (containers.size > firstResult) {
val from = firstResult.toInt()
val to = Math.min((firstResult + maxResults).toInt(), containers.size)
val to = min((firstResult + maxResults).toInt(), containers.size)
didl.containers = containers.subList(from, to)
}
if (didl.containers.size < maxResults) {
val from = Math.max(firstResult - containers.size, 0).toInt()
val to = Math.min(items.size, from + (maxResults - didl.containers.size).toInt())
val from = max(firstResult - containers.size, 0).toInt()
val to = min(items.size, from + (maxResults - didl.containers.size).toInt())
didl.items = items.subList(from, to)
}
return BrowseResult(

View file

@ -1,40 +0,0 @@
package net.schowek.nextclouddlna.upnp
import mu.KLogging
import net.schowek.nextclouddlna.DlnaService
import org.jupnp.model.message.StreamRequestMessage
import org.jupnp.model.message.StreamResponseMessage
import org.jupnp.model.message.UpnpResponse
import org.jupnp.protocol.ProtocolFactory
import org.jupnp.transport.spi.UpnpStream
import org.springframework.stereotype.Component
@Component
class UpnpStreamProcessor(
private val dlna: DlnaService
) {
private var upnpStream: UpnpStreamImpl? = null
fun processMessage(requestMsg: StreamRequestMessage): StreamResponseMessage {
logger.debug { "Processing $requestMsg" }
var response = getUpnpStream().process(requestMsg)
if (response == null) {
response = StreamResponseMessage(UpnpResponse.Status.NOT_FOUND)
}
return response
}
private fun getUpnpStream(): UpnpStreamImpl {
if (upnpStream == null) {
upnpStream = UpnpStreamImpl(dlna.upnpService.protocolFactory)
}
return upnpStream!!
}
inner class UpnpStreamImpl(protocolFactory: ProtocolFactory) : UpnpStream(protocolFactory) {
override fun run() {
}
}
companion object : KLogging()
}