Skip to content

Commit

Permalink
Refactor port handling to use DEFAULT_SERVICE_PORT
Browse files Browse the repository at this point in the history
Updated `startServer` function and related classes to accept a port parameter with a default value. Deprecated `SERVICE_PORT` constant in favor of `DEFAULT_SERVICE_PORT`.

Deleted unused release APK and baseline profiles.
  • Loading branch information
kdroidFilter committed Nov 8, 2024
1 parent 6c50c40 commit 90c1040
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 19 deletions.
Binary file not shown.
Binary file not shown.
Binary file removed kmplog-client/release/kmplog-client-release.apk
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.kdroid.kmplog.client.core.data.network
import com.kdroid.kmplog.client.core.domain.repository.SettingsPreferencesRepository
import com.kdroid.kmplog.core.LogMessage
import com.kdroid.kmplog.core.SERVICE_PATH
import com.kdroid.kmplog.core.SERVICE_PORT
import com.kdroid.kmplog.core.DEFAULT_SERVICE_PORT
import io.ktor.client.*
import io.ktor.client.plugins.websocket.*
import io.ktor.http.*
Expand Down Expand Up @@ -43,7 +43,7 @@ class WebSocketManager(
CoroutineScope(Dispatchers.Default).launch {
val isAutoDetection = settingsRepository.getAutomaticDetectionState()
val host = if (isAutoDetection) getIpService() else settingsRepository.getCustomIpAddress("localhost")
val port = if (isAutoDetection) SERVICE_PORT else settingsRepository.getCustomPort().toInt()
val port = if (isAutoDetection) DEFAULT_SERVICE_PORT else settingsRepository.getCustomPort().toInt()

while (true) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.kdroid.kmplog.client.features.screens.settings.SettingsEvent
import com.kdroid.kmplog.client.features.screens.settings.SettingsState
import com.kdroid.kmplog.client.kmplog_client.generated.resources.*
import com.kdroid.kmplog.core.SERVICE_PORT
import com.kdroid.kmplog.core.DEFAULT_SERVICE_PORT
import org.jetbrains.compose.resources.stringResource
import org.jetbrains.jewel.foundation.theme.JewelTheme
import org.jetbrains.jewel.foundation.theme.LocalTextStyle
Expand Down Expand Up @@ -89,7 +87,7 @@ actual fun SettingsScreen(state: SettingsState, onEvent: (SettingsEvent) -> Unit
SettingsTextFieldRow(
label = stringResource(Res.string.port),
state = portState,
placeholder = SERVICE_PORT.toString(),
placeholder = DEFAULT_SERVICE_PORT.toString(),
enabled = checkboxState != ToggleableState.On,
modifier = Modifier.fillMaxWidth(),
fieldWidth = 80.dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const val MAX_MSG_LENGTH = 100
const val SERVICE_TYPE = "_websocket._tcp.local."
const val ANDROID_SERVICE_TYPE = "_websocket._tcp."
const val SERVICE_NAME = "KmpLog"
const val SERVICE_PORT = 8180
const val DEFAULT_SERVICE_PORT = 8180
const val SERVICE_PATH = "/log"
const val SERICE_DESCRIPTION = "KmpLog Websocket Service"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ actual fun publishMdnsService() {
val serviceInfo = NsdServiceInfo().apply {
serviceName = SERVICE_NAME
serviceType = ANDROID_SERVICE_TYPE
port = SERVICE_PORT
port = DEFAULT_SERVICE_PORT
}

val registrationListener = object : NsdManager.RegistrationListener {
Expand Down
6 changes: 3 additions & 3 deletions kmplog/src/commonMain/kotlin/com/kdroid/kmplog/Log.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ object Log {
isDevelopmentMode = isDevelopment
}

fun enableBroadcastingMode() {
fun enableBroadcastingMode(port: Int = DEFAULT_SERVICE_PORT) {
isBroadcastingMode = true
if (!isServerRunning) {
startServer()
startServer(port)
publishMdnsService()
}
isServerRunning = true
Expand All @@ -52,7 +52,7 @@ fun printAndGetLocalLog(priority: Int, tag: String, message: String) : LogMessag

expect fun printAndSendLog(priority: Int, tag: String, msg: String)

expect fun startServer()
expect fun startServer(port : Int)

expect fun Log.v(tag: String, msg: String)
expect fun Log.d(tag: String, msg: String)
Expand Down
2 changes: 1 addition & 1 deletion kmplog/src/jsMain/kotlin/com/kdroid/kmplog/Log.js.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ actual fun Log.println(priority: Int, tag: String, msg: String) {

//Not Available on JS
actual fun printAndSendLog(priority: Int, tag: String, msg: String) {}
actual fun startServer() {}
actual fun startServer(port : Int) {}
actual fun publishMdnsService() {}
//TODO
actual suspend fun sendMessageToWebSocket(logMessage: LogMessage) {}
6 changes: 3 additions & 3 deletions kmplog/src/jvmMain/kotlin/com/kdroid/kmplog/Log.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.kdroid.kmplog

import com.kdroid.kmplog.core.SERICE_DESCRIPTION
import com.kdroid.kmplog.core.SERVICE_NAME
import com.kdroid.kmplog.core.SERVICE_PORT
import com.kdroid.kmplog.core.DEFAULT_SERVICE_PORT
import com.kdroid.kmplog.core.SERVICE_TYPE
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -24,10 +24,10 @@ actual fun publishMdnsService() {
val ipAddress = getLocalIpAddress()
val jmdns = JmDNS.create(InetAddress.getByName(ipAddress))

val serviceInfo = ServiceInfo.create(SERVICE_TYPE, SERVICE_NAME, SERVICE_PORT, "description=$SERICE_DESCRIPTION")
val serviceInfo = ServiceInfo.create(SERVICE_TYPE, SERVICE_NAME, DEFAULT_SERVICE_PORT, "description=$SERICE_DESCRIPTION")

jmdns.registerService(serviceInfo)
println("Service mDNS enregistré : $SERVICE_NAME sur $ipAddress:$SERVICE_PORT")
println("Service mDNS enregistré : $SERVICE_NAME sur $ipAddress:$DEFAULT_SERVICE_PORT")


} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package com.kdroid.kmplog

import com.kdroid.kmplog.core.LogMessage
import com.kdroid.kmplog.core.SERVICE_PORT
import io.ktor.serialization.kotlinx.protobuf.*
import io.ktor.server.application.*
import io.ktor.server.cio.*
Expand Down Expand Up @@ -32,9 +31,10 @@ internal var webSocketChannel: SendChannel<Frame>? = null
internal val connections = mutableSetOf<DefaultWebSocketServerSession>()
internal val mutex = Mutex()

actual fun startServer() {
@OptIn(ExperimentalSerializationApi::class)
actual fun startServer(port : Int) {
GlobalScope.launch {
embeddedServer(CIO, port = SERVICE_PORT, host = "0.0.0.0") {
embeddedServer(CIO, port = port, host = "0.0.0.0") {

install(CORS) {
anyHost()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.kdroid.kmplog.core.LogMessage

//Wasm platform cannot start server
//TODO Implement server in the client side for wasm platform
actual fun startServer() {}
actual fun startServer(port : Int) {}
actual fun publishMdnsService() {}


Expand Down

0 comments on commit 90c1040

Please sign in to comment.