-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WebSocketClient dynamic host/port (#164)
* WebSocketClient dynamic host/port - make dependants on webSocketclient use a provider - provider listens to settings repository changes and updates instance cached - implementation for test connection on trusted node setup * - proper implementation of test connection + adjustements on updating settings * - added TODOs for short term changes coming
- Loading branch information
Showing
17 changed files
with
193 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
...src/commonMain/kotlin/network/bisq/mobile/client/service/mediation/MediationApiGateway.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...ain/src/commonMain/kotlin/network/bisq/mobile/client/websocket/WebSocketClientProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package network.bisq.mobile.client.websocket | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.withTimeout | ||
import network.bisq.mobile.client.websocket.messages.WebSocketRestApiRequest | ||
import network.bisq.mobile.domain.data.BackgroundDispatcher | ||
import network.bisq.mobile.domain.data.repository.SettingsRepository | ||
import network.bisq.mobile.domain.utils.Logging | ||
import kotlin.concurrent.Volatile | ||
import kotlin.uuid.ExperimentalUuidApi | ||
import kotlin.uuid.Uuid | ||
|
||
/** | ||
* Provider to handle dynamic host/port changes | ||
*/ | ||
class WebSocketClientProvider( | ||
defaultHost: String, | ||
defaultPort: Int, | ||
private val settingsRepository: SettingsRepository, | ||
private val clientFactory: (String, Int) -> WebSocketClient) : Logging { | ||
|
||
companion object { | ||
fun parseUri(uri: String): Pair<String,Int> { | ||
uri.split("//")[1].split(":").let { | ||
return Pair(it[0], it[1].toInt()) | ||
} | ||
} | ||
} | ||
|
||
private val backgroundScope = CoroutineScope(BackgroundDispatcher) | ||
|
||
@Volatile | ||
private var currentClient: WebSocketClient? = null | ||
|
||
init { | ||
// Listen to changes in WebSocket configuration and update the client | ||
// TODO we might need to replicate this for changes in settings to reconnect to channels | ||
backgroundScope.launch { | ||
try { | ||
settingsRepository.data.collect { newSettings -> | ||
var host = defaultHost | ||
var port = defaultPort | ||
newSettings?.bisqApiUrl?.takeIf { it.isNotBlank() }?.let { url -> | ||
log.d { "new bisq url $url "} | ||
parseUri(url).apply { | ||
host = first | ||
port = second | ||
} | ||
} | ||
// only update if there was actually a change | ||
if (currentClient == null || currentClient!!.host != host || currentClient!!.port != port) { | ||
if (currentClient?.isConnected == true) { | ||
currentClient?.disconnect() | ||
} | ||
log.d { "Websocket client updated with url $host:$port" } | ||
currentClient = createClient(host, port) | ||
} | ||
} | ||
} catch (e: Exception) { | ||
log.e(e) { "Error updating WebSocket client with new settings." } | ||
} | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalUuidApi::class) | ||
suspend fun testClient(host: String, port: Int): Boolean { | ||
val client = createClient(host, port) | ||
val url = "ws://$host:$port" | ||
return try { | ||
// if connection is refused, catch will execute returning false | ||
client.connect() | ||
return client.isConnected | ||
} catch (e: Exception) { | ||
log.e("Error testing connection to $url: ${e.message}") | ||
false | ||
} finally { | ||
client.disconnect() // Ensure the client is closed to free resources | ||
} | ||
} | ||
|
||
private fun createClient(host: String, port: Int): WebSocketClient { | ||
return clientFactory(host, port) | ||
} | ||
|
||
fun get(): WebSocketClient { | ||
if (currentClient == null) { | ||
runBlocking { | ||
settingsRepository.fetch() | ||
} | ||
} | ||
return currentClient!! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.