|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 Frederik Ar. Mikkelsen & NoobLance |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +package lavalink.server.io |
| 24 | + |
| 25 | +import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager |
| 26 | +import lavalink.server.player.Player |
| 27 | +import lavalink.server.util.Ws |
| 28 | +import org.json.JSONObject |
| 29 | +import org.slf4j.LoggerFactory |
| 30 | +import org.springframework.web.socket.WebSocketSession |
| 31 | +import space.npstr.magma.MagmaApi |
| 32 | +import space.npstr.magma.MagmaMember |
| 33 | +import space.npstr.magma.events.api.MagmaEvent |
| 34 | +import space.npstr.magma.events.api.WebSocketClosed |
| 35 | +import java.util.* |
| 36 | +import java.util.concurrent.ConcurrentHashMap |
| 37 | +import java.util.concurrent.Executors |
| 38 | +import java.util.concurrent.ScheduledExecutorService |
| 39 | +import java.util.concurrent.TimeUnit |
| 40 | +import java.util.function.Consumer |
| 41 | +import java.util.function.Supplier |
| 42 | + |
| 43 | +class SocketContext internal constructor(audioPlayerManagerSupplier: Supplier<AudioPlayerManager>, val session: WebSocketSession, |
| 44 | + socketServer: SocketServer, val userId: String) { |
| 45 | + |
| 46 | + val audioPlayerManager: AudioPlayerManager = audioPlayerManagerSupplier.get() |
| 47 | + internal val magma: MagmaApi = MagmaApi.of { socketServer.getAudioSendFactory(it) } |
| 48 | + //guildId <-> Player |
| 49 | + val players = ConcurrentHashMap<String, Player>() |
| 50 | + private val statsExecutor: ScheduledExecutorService |
| 51 | + val playerUpdateService: ScheduledExecutorService |
| 52 | + |
| 53 | + val playingPlayers: List<Player> |
| 54 | + get() { |
| 55 | + val newList = LinkedList<Player>() |
| 56 | + players.values.forEach { player -> if (player.isPlaying) newList.add(player) } |
| 57 | + return newList |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + init { |
| 62 | + magma.eventStream.subscribe { this.handleMagmaEvent(it) } |
| 63 | + |
| 64 | + statsExecutor = Executors.newSingleThreadScheduledExecutor() |
| 65 | + statsExecutor.scheduleAtFixedRate(StatsTask(this, socketServer), 0, 1, TimeUnit.MINUTES) |
| 66 | + |
| 67 | + playerUpdateService = Executors.newScheduledThreadPool(2) { r -> |
| 68 | + val thread = Thread(r) |
| 69 | + thread.name = "player-update" |
| 70 | + thread.isDaemon = true |
| 71 | + thread |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + internal fun getPlayer(guildId: String): Player { |
| 76 | + return players.computeIfAbsent(guildId |
| 77 | + ) { _ -> Player(this, guildId, audioPlayerManager) } |
| 78 | + } |
| 79 | + |
| 80 | + internal fun getPlayers(): Map<String, Player> { |
| 81 | + return players |
| 82 | + } |
| 83 | + |
| 84 | + private fun handleMagmaEvent(magmaEvent: MagmaEvent) { |
| 85 | + if (magmaEvent is WebSocketClosed) { |
| 86 | + val out = JSONObject() |
| 87 | + out.put("op", "event") |
| 88 | + out.put("type", "WebSocketClosedEvent") |
| 89 | + out.put("guildId", magmaEvent.member.guildId) |
| 90 | + out.put("reason", magmaEvent.reason) |
| 91 | + out.put("code", magmaEvent.closeCode) |
| 92 | + out.put("byRemote", magmaEvent.isByRemote) |
| 93 | + |
| 94 | + Ws.send(session, out) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + internal fun shutdown() { |
| 99 | + log.info("Shutting down " + playingPlayers.size + " playing players.") |
| 100 | + statsExecutor.shutdown() |
| 101 | + audioPlayerManager.shutdown() |
| 102 | + playerUpdateService.shutdown() |
| 103 | + players.keys.forEach { guildId -> |
| 104 | + val member = MagmaMember.builder() |
| 105 | + .userId(userId) |
| 106 | + .guildId(guildId) |
| 107 | + .build() |
| 108 | + magma.removeSendHandler(member) |
| 109 | + magma.closeConnection(member) |
| 110 | + } |
| 111 | + |
| 112 | + players.values.forEach(Consumer<Player> { it.stop() }) |
| 113 | + magma.shutdown() |
| 114 | + } |
| 115 | + |
| 116 | + companion object { |
| 117 | + |
| 118 | + private val log = LoggerFactory.getLogger(SocketContext::class.java) |
| 119 | + } |
| 120 | +} |
0 commit comments