2020-12-21 03:13:03 +01:00
|
|
|
package jp.juggler.subwaytooter.streaming
|
|
|
|
|
|
|
|
import jp.juggler.subwaytooter.api.TootParser
|
|
|
|
import jp.juggler.subwaytooter.api.entity.TootInstance
|
|
|
|
import jp.juggler.subwaytooter.table.SavedAccount
|
2023-01-13 13:22:25 +01:00
|
|
|
import jp.juggler.util.log.LogCategory
|
2020-12-21 03:13:03 +01:00
|
|
|
import java.util.concurrent.ConcurrentHashMap
|
|
|
|
|
|
|
|
// ストリーミング接続をacct単位でグルーピングする
|
|
|
|
class StreamGroupAcct(
|
|
|
|
private val manager: StreamManager,
|
2020-12-21 21:16:33 +01:00
|
|
|
val account: SavedAccount,
|
2023-01-13 13:22:25 +01:00
|
|
|
var ti: TootInstance,
|
2020-12-21 03:13:03 +01:00
|
|
|
) {
|
2021-06-20 15:12:25 +02:00
|
|
|
companion object {
|
2020-12-27 08:10:58 +01:00
|
|
|
private val log = LogCategory("StreamGroupAcct")
|
|
|
|
}
|
2021-06-20 15:12:25 +02:00
|
|
|
|
2020-12-21 21:16:33 +01:00
|
|
|
val parser: TootParser = TootParser(manager.appState.context, linkHelper = account)
|
2020-12-21 03:13:03 +01:00
|
|
|
|
2020-12-26 22:03:58 +01:00
|
|
|
val keyGroups = ConcurrentHashMap<StreamSpec, StreamGroup>()
|
2020-12-21 03:13:03 +01:00
|
|
|
|
2020-12-21 21:16:33 +01:00
|
|
|
// 接続を束ねない場合に使われる
|
|
|
|
private val connections = ConcurrentHashMap<StreamSpec, StreamConnection>()
|
2020-12-21 03:13:03 +01:00
|
|
|
|
2020-12-21 21:16:33 +01:00
|
|
|
// 接続を束ねる場合に使われる
|
2020-12-21 03:13:03 +01:00
|
|
|
private var mergedConnection: StreamConnection? = null
|
|
|
|
|
2020-12-21 21:16:33 +01:00
|
|
|
// カラムIDから出力先へのマップ
|
|
|
|
@Volatile
|
2020-12-26 22:03:58 +01:00
|
|
|
private var destinations = ConcurrentHashMap<Int, StreamRelation>()
|
2020-12-21 21:16:33 +01:00
|
|
|
|
2020-12-26 22:03:58 +01:00
|
|
|
fun addSpec(dst: StreamRelation) {
|
2020-12-21 21:16:33 +01:00
|
|
|
val spec = dst.spec
|
|
|
|
var group = keyGroups[spec]
|
2020-12-21 03:13:03 +01:00
|
|
|
if (group == null) {
|
2020-12-26 22:03:58 +01:00
|
|
|
group = StreamGroup(spec)
|
2020-12-21 21:16:33 +01:00
|
|
|
keyGroups[spec] = group
|
2020-12-21 03:13:03 +01:00
|
|
|
}
|
2021-06-20 15:12:25 +02:00
|
|
|
group.destinations[dst.columnInternalId] = dst
|
2020-12-21 03:13:03 +01:00
|
|
|
}
|
|
|
|
|
2021-06-20 15:12:25 +02:00
|
|
|
private fun updateDestinations() {
|
2020-12-27 08:10:58 +01:00
|
|
|
destinations = ConcurrentHashMap<Int, StreamRelation>().apply {
|
|
|
|
keyGroups.values.forEach { group ->
|
|
|
|
group.destinations.values.forEach { relation ->
|
|
|
|
put(relation.columnInternalId, relation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// マージではなく、作られてデータを追加された直後に呼ばれる
|
|
|
|
fun initialize() {
|
|
|
|
updateDestinations()
|
|
|
|
}
|
|
|
|
|
2020-12-21 03:13:03 +01:00
|
|
|
fun merge(newServer: StreamGroupAcct) {
|
|
|
|
|
|
|
|
// 新スペックの値をコピー
|
|
|
|
this.ti = newServer.ti
|
|
|
|
|
2020-12-21 21:16:33 +01:00
|
|
|
newServer.keyGroups.entries.forEach {
|
|
|
|
keyGroups[it.key] = it.value
|
2020-12-21 03:13:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 新グループにないグループを削除
|
2020-12-21 21:16:33 +01:00
|
|
|
keyGroups.entries.toList().forEach {
|
|
|
|
if (!newServer.keyGroups.containsKey(it.key)) keyGroups.remove(it.key)
|
2020-12-21 03:13:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// グループにない接続を破棄
|
|
|
|
connections.entries.toList().forEach {
|
2020-12-21 21:16:33 +01:00
|
|
|
if (!keyGroups.containsKey(it.key)) {
|
2020-12-21 03:13:03 +01:00
|
|
|
it.value.dispose()
|
|
|
|
connections.remove(it.key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 08:10:58 +01:00
|
|
|
updateDestinations()
|
2020-12-21 03:13:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// このオブジェクトはもう使われなくなる
|
|
|
|
fun dispose() {
|
|
|
|
connections.values.forEach { it.dispose() }
|
|
|
|
connections.clear()
|
|
|
|
|
|
|
|
mergedConnection?.dispose()
|
|
|
|
mergedConnection = null
|
|
|
|
|
2020-12-21 21:16:33 +01:00
|
|
|
keyGroups.clear()
|
2020-12-21 03:13:03 +01:00
|
|
|
}
|
|
|
|
|
2021-06-20 15:12:25 +02:00
|
|
|
private fun findConnection(spec: StreamSpec): StreamConnection? =
|
|
|
|
when (val mergedConnection = this.mergedConnection) {
|
|
|
|
null -> when (val conn = connections[spec]) {
|
|
|
|
null -> {
|
2020-12-27 08:10:58 +01:00
|
|
|
log.w("findConnection: missing connection for ${spec.name}")
|
|
|
|
null
|
|
|
|
}
|
|
|
|
else -> conn
|
|
|
|
}
|
2021-06-20 15:12:25 +02:00
|
|
|
else -> mergedConnection
|
2020-12-27 08:10:58 +01:00
|
|
|
}
|
2020-12-21 03:13:03 +01:00
|
|
|
|
|
|
|
// ストリーミング接続インジケータ
|
2020-12-27 08:10:58 +01:00
|
|
|
fun getStreamStatus(columnInternalId: Int): StreamStatus =
|
2021-06-20 15:12:25 +02:00
|
|
|
when (val spec = destinations[columnInternalId]?.spec) {
|
|
|
|
null -> {
|
2020-12-27 08:10:58 +01:00
|
|
|
log.w("getStreamStatus: missing destination for ${account.acct.pretty}")
|
|
|
|
StreamStatus.Missing
|
|
|
|
}
|
2021-06-20 15:12:25 +02:00
|
|
|
else -> findConnection(spec)?.getStreamStatus(spec) ?: StreamStatus.Missing
|
2020-12-27 08:10:58 +01:00
|
|
|
}
|
2020-12-21 03:13:03 +01:00
|
|
|
|
|
|
|
suspend fun updateConnection() {
|
|
|
|
|
2020-12-21 21:16:33 +01:00
|
|
|
val multiplex = if (account.isMastodon) {
|
2020-12-21 03:13:03 +01:00
|
|
|
ti.versionGE(TootInstance.VERSION_3_3_0_rc1)
|
|
|
|
} else {
|
2020-12-21 21:16:33 +01:00
|
|
|
account.misskeyVersion >= 11
|
2020-12-21 03:13:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (multiplex) {
|
|
|
|
connections.values.forEach { it.dispose() }
|
|
|
|
connections.clear()
|
|
|
|
|
2021-06-20 15:12:25 +02:00
|
|
|
if (destinations.isEmpty()) {
|
2020-12-21 03:13:03 +01:00
|
|
|
mergedConnection?.dispose()
|
|
|
|
mergedConnection = null
|
2021-06-20 15:12:25 +02:00
|
|
|
} else {
|
2020-12-21 03:13:03 +01:00
|
|
|
if (mergedConnection == null) {
|
|
|
|
mergedConnection = StreamConnection(
|
|
|
|
manager,
|
|
|
|
this,
|
2020-12-21 21:16:33 +01:00
|
|
|
spec = null,
|
2021-06-20 15:12:25 +02:00
|
|
|
name = "[${account.acct.pretty}:multiplex]",
|
2020-12-21 03:13:03 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
mergedConnection?.updateConnection()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mergedConnection?.dispose()
|
|
|
|
mergedConnection = null
|
|
|
|
|
2020-12-21 21:16:33 +01:00
|
|
|
keyGroups.entries.forEach { pair ->
|
2021-06-20 15:12:25 +02:00
|
|
|
val (streamKey, group) = pair
|
2020-12-21 03:13:03 +01:00
|
|
|
var conn = connections[streamKey]
|
|
|
|
if (conn == null) {
|
|
|
|
conn = StreamConnection(
|
|
|
|
manager,
|
|
|
|
this,
|
2020-12-21 21:16:33 +01:00
|
|
|
spec = group.spec,
|
|
|
|
"[${account.acct.pretty}:${group.spec.name}]",
|
2020-12-21 03:13:03 +01:00
|
|
|
)
|
|
|
|
connections[streamKey] = conn
|
|
|
|
}
|
|
|
|
conn.updateConnection()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 15:12:25 +02:00
|
|
|
fun getConnection(internalId: Int) =
|
|
|
|
mergedConnection ?: destinations[internalId]?.spec?.let { connections[it] }
|
2020-12-21 03:13:03 +01:00
|
|
|
}
|