Nodes: auto-discover clawdis.internal
This commit is contained in:
@@ -27,7 +27,6 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
|
||||
val manualEnabled: StateFlow<Boolean> = runtime.manualEnabled
|
||||
val manualHost: StateFlow<String> = runtime.manualHost
|
||||
val manualPort: StateFlow<Int> = runtime.manualPort
|
||||
val discoveryDomain: StateFlow<String> = runtime.discoveryDomain
|
||||
|
||||
val chatMessages: StateFlow<List<NodeRuntime.ChatMessage>> = runtime.chatMessages
|
||||
val chatError: StateFlow<String?> = runtime.chatError
|
||||
@@ -57,10 +56,6 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
|
||||
runtime.setManualPort(value)
|
||||
}
|
||||
|
||||
fun setDiscoveryDomain(value: String) {
|
||||
runtime.setDiscoveryDomain(value)
|
||||
}
|
||||
|
||||
fun setWakeWords(words: List<String>) {
|
||||
runtime.setWakeWords(words)
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ class NodeRuntime(context: Context) {
|
||||
val camera = CameraCaptureManager(appContext)
|
||||
private val json = Json { ignoreUnknownKeys = true }
|
||||
|
||||
private val discovery = BridgeDiscovery(appContext, scope = scope, discoveryDomain = prefs.discoveryDomain)
|
||||
private val discovery = BridgeDiscovery(appContext, scope = scope)
|
||||
val bridges: StateFlow<List<BridgeEndpoint>> = discovery.bridges
|
||||
|
||||
private val _isConnected = MutableStateFlow(false)
|
||||
@@ -82,7 +82,6 @@ class NodeRuntime(context: Context) {
|
||||
val manualEnabled: StateFlow<Boolean> = prefs.manualEnabled
|
||||
val manualHost: StateFlow<String> = prefs.manualHost
|
||||
val manualPort: StateFlow<Int> = prefs.manualPort
|
||||
val discoveryDomain: StateFlow<String> = prefs.discoveryDomain
|
||||
val lastDiscoveredStableId: StateFlow<String> = prefs.lastDiscoveredStableId
|
||||
|
||||
private var didAutoConnect = false
|
||||
@@ -158,10 +157,6 @@ class NodeRuntime(context: Context) {
|
||||
prefs.setManualPort(value)
|
||||
}
|
||||
|
||||
fun setDiscoveryDomain(value: String) {
|
||||
prefs.setDiscoveryDomain(value)
|
||||
}
|
||||
|
||||
fun setWakeWords(words: List<String>) {
|
||||
prefs.setWakeWords(words)
|
||||
scheduleWakeWordsSyncIfNeeded()
|
||||
|
||||
@@ -50,9 +50,6 @@ class SecurePrefs(context: Context) {
|
||||
private val _manualPort = MutableStateFlow(prefs.getInt("bridge.manual.port", 18790))
|
||||
val manualPort: StateFlow<Int> = _manualPort
|
||||
|
||||
private val _discoveryDomain = MutableStateFlow(prefs.getString("bridge.discovery.domain", "")!!)
|
||||
val discoveryDomain: StateFlow<String> = _discoveryDomain
|
||||
|
||||
private val _lastDiscoveredStableId =
|
||||
MutableStateFlow(prefs.getString("bridge.lastDiscoveredStableId", "")!!)
|
||||
val lastDiscoveredStableId: StateFlow<String> = _lastDiscoveredStableId
|
||||
@@ -93,12 +90,6 @@ class SecurePrefs(context: Context) {
|
||||
_manualPort.value = value
|
||||
}
|
||||
|
||||
fun setDiscoveryDomain(value: String) {
|
||||
val trimmed = value.trim()
|
||||
prefs.edit().putString("bridge.discovery.domain", trimmed).apply()
|
||||
_discoveryDomain.value = trimmed
|
||||
}
|
||||
|
||||
fun loadBridgeToken(): String? {
|
||||
val key = "bridge.token.${_instanceId.value}"
|
||||
return prefs.getString(key, null)
|
||||
|
||||
@@ -13,7 +13,6 @@ import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.launch
|
||||
import org.xbill.DNS.Lookup
|
||||
import org.xbill.DNS.PTRRecord
|
||||
@@ -24,16 +23,16 @@ import org.xbill.DNS.Type
|
||||
class BridgeDiscovery(
|
||||
context: Context,
|
||||
private val scope: CoroutineScope,
|
||||
discoveryDomain: StateFlow<String>,
|
||||
) {
|
||||
private val nsd = context.getSystemService(NsdManager::class.java)
|
||||
private val serviceType = "_clawdis-bridge._tcp."
|
||||
private val wideAreaDomain = "clawdis.internal."
|
||||
|
||||
private val byId = ConcurrentHashMap<String, BridgeEndpoint>()
|
||||
private val localById = ConcurrentHashMap<String, BridgeEndpoint>()
|
||||
private val unicastById = ConcurrentHashMap<String, BridgeEndpoint>()
|
||||
private val _bridges = MutableStateFlow<List<BridgeEndpoint>>(emptyList())
|
||||
val bridges: StateFlow<List<BridgeEndpoint>> = _bridges.asStateFlow()
|
||||
|
||||
private var activeDomain: String = normalizeDomain(discoveryDomain.value)
|
||||
private var unicastJob: Job? = null
|
||||
|
||||
private val discoveryListener =
|
||||
@@ -50,36 +49,14 @@ class BridgeDiscovery(
|
||||
|
||||
override fun onServiceLost(serviceInfo: NsdServiceInfo) {
|
||||
val id = stableId(serviceInfo.serviceName, "local.")
|
||||
byId.remove(id)
|
||||
localById.remove(id)
|
||||
publish()
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
scope.launch {
|
||||
discoveryDomain.collect { raw ->
|
||||
val normalized = normalizeDomain(raw)
|
||||
if (normalized == activeDomain) return@collect
|
||||
activeDomain = normalized
|
||||
restartDiscovery()
|
||||
}
|
||||
}
|
||||
restartDiscovery()
|
||||
}
|
||||
|
||||
private fun restartDiscovery() {
|
||||
byId.clear()
|
||||
publish()
|
||||
|
||||
stopLocalDiscovery()
|
||||
unicastJob?.cancel()
|
||||
unicastJob = null
|
||||
|
||||
if (activeDomain == "local.") {
|
||||
startLocalDiscovery()
|
||||
} else {
|
||||
startUnicastDiscovery(activeDomain)
|
||||
}
|
||||
startLocalDiscovery()
|
||||
startUnicastDiscovery(wideAreaDomain)
|
||||
}
|
||||
|
||||
private fun startLocalDiscovery() {
|
||||
@@ -125,7 +102,7 @@ class BridgeDiscovery(
|
||||
|
||||
val displayName = txt(resolved, "displayName") ?: resolved.serviceName
|
||||
val id = stableId(resolved.serviceName, "local.")
|
||||
byId[id] = BridgeEndpoint(stableId = id, name = displayName, host = host, port = port)
|
||||
localById[id] = BridgeEndpoint(stableId = id, name = displayName, host = host, port = port)
|
||||
publish()
|
||||
}
|
||||
},
|
||||
@@ -133,7 +110,8 @@ class BridgeDiscovery(
|
||||
}
|
||||
|
||||
private fun publish() {
|
||||
_bridges.value = byId.values.sortedBy { it.name.lowercase() }
|
||||
_bridges.value =
|
||||
(localById.values + unicastById.values).sortedBy { it.name.lowercase() }
|
||||
}
|
||||
|
||||
private fun stableId(serviceName: String, domain: String): String {
|
||||
@@ -181,8 +159,8 @@ class BridgeDiscovery(
|
||||
next[id] = BridgeEndpoint(stableId = id, name = displayName, host = host, port = port)
|
||||
}
|
||||
|
||||
byId.clear()
|
||||
byId.putAll(next)
|
||||
unicastById.clear()
|
||||
unicastById.putAll(next)
|
||||
publish()
|
||||
}
|
||||
|
||||
@@ -197,12 +175,6 @@ class BridgeDiscovery(
|
||||
return normalizeName(stripTrailingDot(withoutSuffix))
|
||||
}
|
||||
|
||||
private fun normalizeDomain(raw: String): String {
|
||||
val trimmed = raw.trim().lowercase()
|
||||
if (trimmed.isEmpty() || trimmed == "local" || trimmed == "local.") return "local."
|
||||
return if (trimmed.endsWith(".")) trimmed else "$trimmed."
|
||||
}
|
||||
|
||||
private fun stripTrailingDot(raw: String): String {
|
||||
return raw.removeSuffix(".")
|
||||
}
|
||||
|
||||
@@ -51,7 +51,6 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
||||
val manualEnabled by viewModel.manualEnabled.collectAsState()
|
||||
val manualHost by viewModel.manualHost.collectAsState()
|
||||
val manualPort by viewModel.manualPort.collectAsState()
|
||||
val discoveryDomain by viewModel.discoveryDomain.collectAsState()
|
||||
val statusText by viewModel.statusText.collectAsState()
|
||||
val serverName by viewModel.serverName.collectAsState()
|
||||
val remoteAddress by viewModel.remoteAddress.collectAsState()
|
||||
@@ -182,15 +181,6 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
||||
Text(if (manualEnabled) "Manual Bridge Enabled" else "Manual Bridge Disabled")
|
||||
}
|
||||
}
|
||||
item {
|
||||
OutlinedTextField(
|
||||
value = discoveryDomain,
|
||||
onValueChange = viewModel::setDiscoveryDomain,
|
||||
label = { Text("Discovery Domain (leave empty for local.)") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
)
|
||||
}
|
||||
item {
|
||||
OutlinedTextField(
|
||||
value = manualHost,
|
||||
|
||||
Reference in New Issue
Block a user