Skip to content

A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.

feat(hostbridge): add connection server loop

ExTv751581e4f531
diff --git a/app/src/main/java/com/excp/podroid/engine/hostbridge/HostRequestServer.kt b/app/src/main/java/com/excp/podroid/engine/hostbridge/HostRequestServer.kt
new file mode 100644
index 0000000..279053b
--- /dev/null
+++ b/app/src/main/java/com/excp/podroid/engine/hostbridge/HostRequestServer.kt
@@ -0,0 +1,65 @@
+/*
+ * Podroid - Rootless Podman for Android
+ * Copyright (C) 2024-2026 Podroid contributors
+ *
+ * Owns the guest-bridge connection for one VM session. Opens a HostTransport
+ * (retrying until the guest daemon is up), then loops: read request line ->
+ * dispatch -> write response line. On EOF/error it closes and reconnects while
+ * the session is active. start()/stop() are driven by VM Running/terminal.
+ */
+package com.excp.podroid.engine.hostbridge
+
+import android.util.Log
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.isActive
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+
+class HostRequestServer(
+ private val openTransport: () -> HostTransport?,
+ private val dispatcher: HostRequestDispatcher,
+ private val scope: CoroutineScope,
+) {
+ companion object {
+ private const val TAG = "HostRequestServer"
+ private const val RETRY_MS = 500L
+ }
+
+ private var job: Job? = null
+
+ fun start() {
+ if (job?.isActive == true) return
+ job = scope.launch(Dispatchers.IO) { runLoop() }
+ }
+
+ fun stop() {
+ job?.cancel()
+ job = null
+ }
+
+ private suspend fun runLoop() {
+ while (scope.isActive) {
+ val transport = openTransport()
+ if (transport == null) {
+ delay(RETRY_MS)
+ continue
+ }
+ Log.d(TAG, "host bridge connected")
+ try {
+ while (scope.isActive) {
+ val req = withContext(Dispatchers.IO) { transport.readRequest() } ?: break
+ val resp = dispatcher.handle(req)
+ withContext(Dispatchers.IO) { transport.writeResponse(resp) }
+ }
+ } catch (e: Exception) {
+ Log.w(TAG, "host bridge loop error: ${e.message}")
+ } finally {
+ transport.close()
+ }
+ delay(RETRY_MS) // brief pause before reconnecting
+ }
+ }
+}