UpnpService as bean

This commit is contained in:
xis 2023-10-12 19:09:25 +02:00
parent 742686d55f
commit 70ed4185fd
6 changed files with 54 additions and 66 deletions

View file

@ -32,7 +32,7 @@ import java.net.NetworkInterface
@Component @Component
class DlnaService( class DlnaService(
private val mediaServer: MediaServer, private val mediaServer: MediaServer,
serverInfoProvider: ServerInfoProvider, private val serverInfoProvider: ServerInfoProvider,
) { ) {
private val addressesToBind: List<InetAddress> = listOf(serverInfoProvider.address!!) private val addressesToBind: List<InetAddress> = listOf(serverInfoProvider.address!!)
var upnpService: UpnpService = MyUpnpService(MyUpnpServiceConfiguration()) var upnpService: UpnpService = MyUpnpService(MyUpnpServiceConfiguration())
@ -55,16 +55,16 @@ class DlnaService(
inner class MyUpnpService( inner class MyUpnpService(
configuration: UpnpServiceConfiguration configuration: UpnpServiceConfiguration
) : UpnpServiceImpl(configuration) { ) : UpnpServiceImpl(configuration) {
init {
protocolFactory = createProtocolFactory()
}
override fun createRegistry(pf: ProtocolFactory): Registry { override fun createRegistry(pf: ProtocolFactory): Registry {
return RegistryImplWithOverrides(this) return RegistryImplWithOverrides(this)
} }
override fun createProtocolFactory(): ProtocolFactory? {
return MyProtocolFactory(this)
}
} }
private inner class MyUpnpServiceConfiguration : DefaultUpnpServiceConfiguration(8080) { private inner class MyUpnpServiceConfiguration : DefaultUpnpServiceConfiguration(serverInfoProvider.port) {
override fun createStreamClient(): StreamClient<*> { override fun createStreamClient(): StreamClient<*> {
return ApacheStreamClient( return ApacheStreamClient(
ApacheStreamClientConfiguration(syncProtocolExecutorService) ApacheStreamClientConfiguration(syncProtocolExecutorService)
@ -96,15 +96,3 @@ class DlnaService(
companion object : KLogging() companion object : KLogging()
} }
class MyProtocolFactory(
upnpService: UpnpService
) : ProtocolFactoryImpl(upnpService) {
override fun createSendingNotificationAlive(localDevice: LocalDevice): SendingNotificationAlive {
logger.info { "SENDING ALIVE $localDevice" }
return SendingNotificationAlive(upnpService, localDevice)
}
companion object : KLogging()
}

View file

@ -1,19 +1,17 @@
package net.schowek.nextclouddlna.controller package net.schowek.nextclouddlna.controller
import UpnpStreamProcessor
import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletRequest
import mu.KLogging import mu.KLogging
import net.schowek.nextclouddlna.DlnaService
import net.schowek.nextclouddlna.dlna.media.MediaServer import net.schowek.nextclouddlna.dlna.media.MediaServer
import net.schowek.nextclouddlna.upnp.StreamRequestMapper
import net.schowek.nextclouddlna.upnp.UpnpStreamProcessor
import org.springframework.core.io.InputStreamResource import org.springframework.core.io.InputStreamResource
import org.springframework.core.io.Resource import org.springframework.core.io.Resource
import org.springframework.http.HttpHeaders import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode import org.springframework.http.HttpStatusCode
import org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE import org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod.* import org.springframework.web.bind.annotation.RequestMethod.*
@ -21,9 +19,9 @@ import org.springframework.web.bind.annotation.ResponseBody
@Controller @Controller
class DLNAController( class UpnpController(
private val dlna: DlnaService, private val streamRequestMapper: StreamRequestMapper,
private val streamRequestMapper: StreamRequestMapper private val upnpStreamProcessor: UpnpStreamProcessor
) { ) {
@RequestMapping( @RequestMapping(
method = [GET, HEAD], value = ["/dev/{uid}/icon.png"], method = [GET, HEAD], value = ["/dev/{uid}/icon.png"],
@ -51,7 +49,7 @@ class DLNAController(
request: HttpServletRequest request: HttpServletRequest
): ResponseEntity<Any> { ): ResponseEntity<Any> {
logger.info { "GET request from ${request.remoteAddr}: ${request.requestURI}" } logger.info { "GET request from ${request.remoteAddr}: ${request.requestURI}" }
val r = UpnpStreamProcessor(dlna).processMessage(streamRequestMapper.map(request)) val r = upnpStreamProcessor.processMessage(streamRequestMapper.map(request))
return ResponseEntity( return ResponseEntity(
r.body, r.body,
HttpHeaders().also { h -> r.headers.entries.forEach { h.add(it.key, it.value.joinToString { it }) } }, HttpHeaders().also { h -> r.headers.entries.forEach { h.add(it.key, it.value.joinToString { it }) } },
@ -70,7 +68,7 @@ class DLNAController(
request: HttpServletRequest request: HttpServletRequest
): ResponseEntity<Any> { ): ResponseEntity<Any> {
logger.info { "POST request from ${request.remoteAddr}: ${request.requestURI}" } logger.info { "POST request from ${request.remoteAddr}: ${request.requestURI}" }
val r = UpnpStreamProcessor(dlna).processMessage(streamRequestMapper.map(request)) val r = upnpStreamProcessor.processMessage(streamRequestMapper.map(request))
return ResponseEntity( return ResponseEntity(
r.body, r.body,
HttpHeaders().also { h -> r.headers.entries.forEach { h.add(it.key, it.value.joinToString { it }) } }, HttpHeaders().also { h -> r.headers.entries.forEach { h.add(it.key, it.value.joinToString { it }) } },
@ -78,12 +76,5 @@ class DLNAController(
) )
} }
@GetMapping("/alive")
fun sendAlive(): ResponseEntity<String> {
logger.info { "STARTING!" }
dlna.start();
return ResponseEntity(HttpStatus.OK)
}
companion object : KLogging() companion object : KLogging()
} }

View file

@ -1,26 +0,0 @@
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.transport.spi.UpnpStream
class UpnpStreamProcessor(
dlna: DlnaService
) : UpnpStream(dlna.upnpService!!.protocolFactory) {
fun processMessage(requestMsg: StreamRequestMessage): StreamResponseMessage {
logger.debug { "Processing $requestMsg" }
var response = super.process(requestMsg)
if (response == null) {
response = StreamResponseMessage(UpnpResponse.Status.NOT_FOUND)
}
return response
}
override fun run() {
}
companion object : KLogging()
}

View file

@ -16,11 +16,6 @@ class ContentTreeProvider(
private var tree = buildContentTree() private var tree = buildContentTree()
private var lastMTime = 0L private var lastMTime = 0L
init {
rebuildTree()
}
@PostConstruct
@Scheduled(fixedDelay = 1000 * 60, initialDelay = 1000 * 60) @Scheduled(fixedDelay = 1000 * 60, initialDelay = 1000 * 60)
final fun rebuildTree() { final fun rebuildTree() {
val maxMtime: Long = mediaDB.maxMtime() val maxMtime: Long = mediaDB.maxMtime()

View file

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

View file

@ -0,0 +1,40 @@
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()
}