From 70ed4185fddeb453b015e964fc76106532184389 Mon Sep 17 00:00:00 2001 From: xis Date: Thu, 12 Oct 2023 19:09:25 +0200 Subject: [PATCH] UpnpService as bean --- .../net/schowek/nextclouddlna/DllnaService.kt | 24 +++-------- .../{DLNAController.kt => UpnpController.kt} | 23 ++++------- .../controller/UpnpStreamProcessor.kt | 26 ------------ .../nextcloud/content/ContentTreeProvider.kt | 5 --- .../StreamRequestMapper.kt | 2 +- .../nextclouddlna/upnp/UpnpStreamProcessor.kt | 40 +++++++++++++++++++ 6 files changed, 54 insertions(+), 66 deletions(-) rename src/main/kotlin/net/schowek/nextclouddlna/controller/{DLNAController.kt => UpnpController.kt} (80%) delete mode 100644 src/main/kotlin/net/schowek/nextclouddlna/controller/UpnpStreamProcessor.kt rename src/main/kotlin/net/schowek/nextclouddlna/{controller => upnp}/StreamRequestMapper.kt (98%) create mode 100644 src/main/kotlin/net/schowek/nextclouddlna/upnp/UpnpStreamProcessor.kt diff --git a/src/main/kotlin/net/schowek/nextclouddlna/DllnaService.kt b/src/main/kotlin/net/schowek/nextclouddlna/DllnaService.kt index b1fe068..3a796f1 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/DllnaService.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/DllnaService.kt @@ -32,7 +32,7 @@ import java.net.NetworkInterface @Component class DlnaService( private val mediaServer: MediaServer, - serverInfoProvider: ServerInfoProvider, + private val serverInfoProvider: ServerInfoProvider, ) { private val addressesToBind: List = listOf(serverInfoProvider.address!!) var upnpService: UpnpService = MyUpnpService(MyUpnpServiceConfiguration()) @@ -55,16 +55,16 @@ class DlnaService( inner class MyUpnpService( configuration: UpnpServiceConfiguration ) : UpnpServiceImpl(configuration) { + init { + protocolFactory = createProtocolFactory() + } + override fun createRegistry(pf: ProtocolFactory): Registry { 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<*> { return ApacheStreamClient( ApacheStreamClientConfiguration(syncProtocolExecutorService) @@ -96,15 +96,3 @@ class DlnaService( 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() -} \ No newline at end of file diff --git a/src/main/kotlin/net/schowek/nextclouddlna/controller/DLNAController.kt b/src/main/kotlin/net/schowek/nextclouddlna/controller/UpnpController.kt similarity index 80% rename from src/main/kotlin/net/schowek/nextclouddlna/controller/DLNAController.kt rename to src/main/kotlin/net/schowek/nextclouddlna/controller/UpnpController.kt index 6a0f329..73ee314 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/controller/DLNAController.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/controller/UpnpController.kt @@ -1,19 +1,17 @@ package net.schowek.nextclouddlna.controller -import UpnpStreamProcessor 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 org.springframework.core.io.InputStreamResource import org.springframework.core.io.Resource import org.springframework.http.HttpHeaders -import org.springframework.http.HttpStatus import org.springframework.http.HttpStatusCode import org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE import org.springframework.http.ResponseEntity 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.RequestMapping import org.springframework.web.bind.annotation.RequestMethod.* @@ -21,9 +19,9 @@ import org.springframework.web.bind.annotation.ResponseBody @Controller -class DLNAController( - private val dlna: DlnaService, - private val streamRequestMapper: StreamRequestMapper +class UpnpController( + private val streamRequestMapper: StreamRequestMapper, + private val upnpStreamProcessor: UpnpStreamProcessor ) { @RequestMapping( method = [GET, HEAD], value = ["/dev/{uid}/icon.png"], @@ -51,7 +49,7 @@ class DLNAController( request: HttpServletRequest ): ResponseEntity { 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( r.body, HttpHeaders().also { h -> r.headers.entries.forEach { h.add(it.key, it.value.joinToString { it }) } }, @@ -70,7 +68,7 @@ class DLNAController( request: HttpServletRequest ): ResponseEntity { 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( r.body, 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 { - logger.info { "STARTING!" } - dlna.start(); - return ResponseEntity(HttpStatus.OK) - } - companion object : KLogging() } diff --git a/src/main/kotlin/net/schowek/nextclouddlna/controller/UpnpStreamProcessor.kt b/src/main/kotlin/net/schowek/nextclouddlna/controller/UpnpStreamProcessor.kt deleted file mode 100644 index 54289f0..0000000 --- a/src/main/kotlin/net/schowek/nextclouddlna/controller/UpnpStreamProcessor.kt +++ /dev/null @@ -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() -} \ No newline at end of file diff --git a/src/main/kotlin/net/schowek/nextclouddlna/nextcloud/content/ContentTreeProvider.kt b/src/main/kotlin/net/schowek/nextclouddlna/nextcloud/content/ContentTreeProvider.kt index 80d7136..17b2094 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/nextcloud/content/ContentTreeProvider.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/nextcloud/content/ContentTreeProvider.kt @@ -16,11 +16,6 @@ class ContentTreeProvider( private var tree = buildContentTree() private var lastMTime = 0L - init { - rebuildTree() - } - - @PostConstruct @Scheduled(fixedDelay = 1000 * 60, initialDelay = 1000 * 60) final fun rebuildTree() { val maxMtime: Long = mediaDB.maxMtime() diff --git a/src/main/kotlin/net/schowek/nextclouddlna/controller/StreamRequestMapper.kt b/src/main/kotlin/net/schowek/nextclouddlna/upnp/StreamRequestMapper.kt similarity index 98% rename from src/main/kotlin/net/schowek/nextclouddlna/controller/StreamRequestMapper.kt rename to src/main/kotlin/net/schowek/nextclouddlna/upnp/StreamRequestMapper.kt index 4524fd8..c64d3be 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/controller/StreamRequestMapper.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/upnp/StreamRequestMapper.kt @@ -1,4 +1,4 @@ -package net.schowek.nextclouddlna.controller +package net.schowek.nextclouddlna.upnp import jakarta.servlet.http.HttpServletRequest import mu.KLogging diff --git a/src/main/kotlin/net/schowek/nextclouddlna/upnp/UpnpStreamProcessor.kt b/src/main/kotlin/net/schowek/nextclouddlna/upnp/UpnpStreamProcessor.kt new file mode 100644 index 0000000..30db158 --- /dev/null +++ b/src/main/kotlin/net/schowek/nextclouddlna/upnp/UpnpStreamProcessor.kt @@ -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() +} \ No newline at end of file