Remove the UUIDSerialise copy from the server and make it use the one from common

This commit is contained in:
ProtoByter 2022-12-07 07:56:56 +00:00
parent 20b3b43394
commit 3c999daebc
6 changed files with 7 additions and 101 deletions

View File

@ -1,69 +0,0 @@
@file:OptIn(DelicateCoroutinesApi::class)
package org.muellerssoftware.openproximitychat.common
import kotlinx.coroutines.*
import org.bitlet.weupnp.GatewayDevice
import org.bitlet.weupnp.GatewayDiscover
data class MappedPort(val port: Int, val protocol: String)
object UPnPManager {
private var gateway: GatewayDevice? = null
private val mappedPorts = mutableMapOf<Int, MappedPort>()
private var discoveryJob: Job = Job()
init {
runBlocking {
discoveryJob = GlobalScope.launch {
startGatewayDiscover()
}
}
}
fun startGatewayDiscover() {
val gatewayDiscoverer = GatewayDiscover()
for (i in 0..9) {
Logging.info("Searching for UPnP gateway... (Attempt ${i + 1})")
gatewayDiscoverer.discover()
gateway = GatewayDevice()
if (gatewayDiscoverer.validGateway != null) {
gateway = gatewayDiscoverer.validGateway
break
}
}
if (gateway == null) {
Logging.error("No UPnP gateway found!")
}
else {
Logging.info("UPnP gateway found: ${gateway!!.friendlyName}")
val ports = mappedPorts.toMutableMap()
mappedPorts.clear()
ports.map {
mapPort(it.key, it.value.port, it.value.protocol)
}
}
}
fun mapPort(port: Int, protocol: String = "TCP") {
mapPort(port, port, protocol)
}
fun mapPort(internalPort: Int, externalPort: Int, protocol: String = "TCP") {
if (gateway == null) {
mappedPorts[internalPort] = MappedPort(externalPort, protocol)
}
val succeeded = gateway?.addPortMapping(externalPort, internalPort, gateway!!.localAddress.hostAddress, "TCP", "OpenProximityChat mapped port")
if (succeeded == true) {
mappedPorts[internalPort] = MappedPort(externalPort, protocol)
}
}
fun unMapAll() {
mappedPorts.map {
gateway?.deletePortMapping(it.value.port, it.value.protocol)
}
}
}

View File

@ -25,7 +25,6 @@ object VoiceServer {
var handshook = mutableMapOf<UUID, InetAddress>()
init {
UPnPManager.mapPort(localAddress.port)
runBlocking {
startServer()
@ -75,7 +74,6 @@ object VoiceServer {
fun exit() {
Logging.info("VoiceServer exiting...")
UPnPManager.unMapAll()
if (recieverJob.isActive) {
runBlocking {
recieverJob.cancelAndJoin()

View File

@ -1,4 +1,7 @@
@file:UseSerializers(UUIDAsStringSerializer::class, InstantAsStringSerializer::class)
@file:UseSerializers(
org.muellerssoftware.openproximitychat.common.UUIDSerializer::class,
InstantAsStringSerializer::class
)
package org.muellerssoftware.openproximitychat.tracker

View File

@ -6,6 +6,7 @@ import io.ktor.server.application.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import kotlinx.coroutines.selects.select
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update
import org.muellerssoftware.openproximitychat.common.Logging
@ -35,7 +36,7 @@ fun Application.puncher() {
}
transaction {
Clients.update({ Clients.id eq uuid }) {
Clients.update({ Clients.id eq uuid and (Clients.port eq null) }) {
it[ip] = address.toJavaAddress().toString()
}.run {
if (this == 0) {

View File

@ -1,4 +1,4 @@
@file:UseSerializers(UUIDAsStringSerializer::class)
@file:UseSerializers(org.muellerssoftware.openproximitychat.common.UUIDSerializer::class)
package org.muellerssoftware.openproximitychat.tracker

View File

@ -1,27 +0,0 @@
package org.muellerssoftware.openproximitychat.tracker
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.util.*
@OptIn(ExperimentalSerializationApi::class)
@Serializer(forClass = UUID::class)
object UUIDAsStringSerializer: KSerializer<UUID> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("UUIDAsStringSerializer", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: UUID) {
val string = value.toString()
encoder.encodeString(string)
}
override fun deserialize(decoder: Decoder): UUID {
val string = decoder.decodeString()
return UUID.fromString(string)
}
}