diff --git a/src/main/kotlin/net/schowek/nextclouddlna/dlna/DllnaService.kt b/src/main/kotlin/net/schowek/nextclouddlna/DllnaService.kt similarity index 80% rename from src/main/kotlin/net/schowek/nextclouddlna/dlna/DllnaService.kt rename to src/main/kotlin/net/schowek/nextclouddlna/DllnaService.kt index 27bd6a1..b1fe068 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/dlna/DllnaService.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/DllnaService.kt @@ -1,6 +1,8 @@ -package net.schowek.nextclouddlna.dlna +package net.schowek.nextclouddlna +import jakarta.annotation.PreDestroy import mu.KLogging +import net.schowek.nextclouddlna.dlna.RegistryImplWithOverrides import net.schowek.nextclouddlna.dlna.media.MediaServer import net.schowek.nextclouddlna.dlna.transport.ApacheStreamClient import net.schowek.nextclouddlna.dlna.transport.ApacheStreamClientConfiguration @@ -20,6 +22,8 @@ import org.jupnp.transport.impl.NetworkAddressFactoryImpl import org.jupnp.transport.spi.NetworkAddressFactory import org.jupnp.transport.spi.StreamClient import org.jupnp.transport.spi.StreamServer +import org.springframework.context.event.ContextRefreshedEvent +import org.springframework.context.event.EventListener import org.springframework.stereotype.Component import java.net.InetAddress import java.net.NetworkInterface @@ -27,15 +31,25 @@ import java.net.NetworkInterface @Component class DlnaService( - private val serverInfoProvider: ServerInfoProvider, - private val mediaServer: MediaServer + private val mediaServer: MediaServer, + serverInfoProvider: ServerInfoProvider, ) { - // Named this way cos NetworkAddressFactoryImpl has a bindAddresses field. private val addressesToBind: List = listOf(serverInfoProvider.address!!) + var upnpService: UpnpService = MyUpnpService(MyUpnpServiceConfiguration()) - fun start() = MyUpnpService(MyUpnpServiceConfiguration()).also { - it.startup() - it.registry.addDevice(mediaServer.device) + fun start() { + upnpService.startup() + upnpService.registry.addDevice(mediaServer.device) + } + + @EventListener + fun handleContextRefresh(event: ContextRefreshedEvent) { + start() + } + + @PreDestroy + fun destroy() { + upnpService.shutdown() } inner class MyUpnpService( @@ -79,6 +93,8 @@ class DlnaService( return addressesToBind.contains(address) } } + + companion object : KLogging() } diff --git a/src/main/kotlin/net/schowek/nextclouddlna/NextcloudDLNA.kt b/src/main/kotlin/net/schowek/nextclouddlna/NextcloudDLNA.kt deleted file mode 100644 index bde07e1..0000000 --- a/src/main/kotlin/net/schowek/nextclouddlna/NextcloudDLNA.kt +++ /dev/null @@ -1,20 +0,0 @@ -package net.schowek.nextclouddlna - -import jakarta.annotation.PreDestroy -import net.schowek.nextclouddlna.dlna.DlnaService -import org.jupnp.UpnpService -import org.springframework.stereotype.Component - - -@Component -class NextcloudDLNA( - dlnaService: DlnaService -) { - val upnpService: UpnpService = dlnaService.start() - - @PreDestroy - fun destroy() { - upnpService.shutdown() - } -} - diff --git a/src/main/kotlin/net/schowek/nextclouddlna/controller/DLNAController.kt b/src/main/kotlin/net/schowek/nextclouddlna/controller/DLNAController.kt index 4b814e0..6a0f329 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/controller/DLNAController.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/controller/DLNAController.kt @@ -3,16 +3,17 @@ package net.schowek.nextclouddlna.controller import UpnpStreamProcessor import jakarta.servlet.http.HttpServletRequest import mu.KLogging -import net.schowek.nextclouddlna.NextcloudDLNA +import net.schowek.nextclouddlna.DlnaService import net.schowek.nextclouddlna.dlna.media.MediaServer 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.util.MultiValueMap +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,7 +22,7 @@ import org.springframework.web.bind.annotation.ResponseBody @Controller class DLNAController( - private val dlna: NextcloudDLNA, + private val dlna: DlnaService, private val streamRequestMapper: StreamRequestMapper ) { @RequestMapping( @@ -77,5 +78,12 @@ 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 index ee602ed..54289f0 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/controller/UpnpStreamProcessor.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/controller/UpnpStreamProcessor.kt @@ -1,5 +1,5 @@ import mu.KLogging -import net.schowek.nextclouddlna.NextcloudDLNA +import net.schowek.nextclouddlna.DlnaService import org.jupnp.model.message.StreamRequestMessage import org.jupnp.model.message.StreamResponseMessage import org.jupnp.model.message.UpnpResponse @@ -7,8 +7,8 @@ import org.jupnp.transport.spi.UpnpStream class UpnpStreamProcessor( - dlna: NextcloudDLNA -) : UpnpStream(dlna.upnpService.protocolFactory) { + dlna: DlnaService +) : UpnpStream(dlna.upnpService!!.protocolFactory) { fun processMessage(requestMsg: StreamRequestMessage): StreamResponseMessage { logger.debug { "Processing $requestMsg" } diff --git a/src/main/kotlin/net/schowek/nextclouddlna/dlna/RegistryWithOverrides.kt b/src/main/kotlin/net/schowek/nextclouddlna/dlna/RegistryWithOverrides.kt index 6320c7b..ce2fa9c 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/dlna/RegistryWithOverrides.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/dlna/RegistryWithOverrides.kt @@ -1,11 +1,13 @@ package net.schowek.nextclouddlna.dlna +import mu.KLogging import net.schowek.nextclouddlna.dlna.media.MediaServer import net.schowek.nextclouddlna.dlna.media.MediaServer.Companion.ICON_FILENAME import org.jupnp.UpnpService import org.jupnp.model.resource.IconResource import org.jupnp.model.resource.Resource import org.jupnp.registry.RegistryImpl +import org.jupnp.registry.RegistryMaintainer import java.io.IOException import java.net.URI @@ -31,4 +33,12 @@ class RegistryImplWithOverrides( icon } else super.getResource(pathQuery) } -} \ No newline at end of file + + fun maintain() { + logger.info { "REGISTRY MAINTAIN" } + Thread(registryMaintainer).start() + logger.info { "REGISTRY MAINTAIN DONE" } + } + + companion object : KLogging() +}