Skip to content

Commit

Permalink
Merge pull request #4 from Novage/refactor/improve-error-handling
Browse files Browse the repository at this point in the history
Refactor: improve error handling when start P2P
  • Loading branch information
mrlika authored Jan 17, 2025
2 parents 1b61244 + d120563 commit bf60a5d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 6 additions & 1 deletion p2pml/src/main/java/com/novage/p2pml/P2PMediaLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class P2PMediaLoader(
engineStateManager,
customEngineImplementationPath,
onServerStarted = { onServerStarted() },
onServerError = { onP2PReadyErrorCallback.onError(it) },
onManifestChanged = { onManifestChanged() },
).apply { start(serverPort) }
}
Expand Down Expand Up @@ -235,6 +236,10 @@ class P2PMediaLoader(
Utils.getUrl(serverPort, CORE_FILE_URL)
}

webViewManager!!.loadWebView(urlPath)
try {
webViewManager!!.loadWebView(urlPath)
} catch (e: Exception) {
onP2PReadyErrorCallback.onError(e.message ?: "Unknown error")
}
}
}
8 changes: 6 additions & 2 deletions p2pml/src/main/java/com/novage/p2pml/server/ServerModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal class ServerModule(
private val p2pEngineStateManager: P2PStateManager,
private val customEngineImplementationPath: String? = null,
private val onServerStarted: () -> Unit,
private val onServerError: (String) -> Unit,
private val onManifestChanged: suspend () -> Unit,
) {
private var httpClient: OkHttpClient? = null
Expand All @@ -31,12 +32,15 @@ internal class ServerModule(
fun start(port: Int = 8080) {
if (server != null) return

server =
embeddedServer(CIO, port) {
try {
server = embeddedServer(CIO, port) {
configureCORS(this)
configureRouting(this)
subscribeToServerStarted(this)
}.start(wait = false)
} catch (e: Exception) {
onServerError(e.message ?: "Failed to start server on port $port")
}
}

private fun stopServer() {
Expand Down

0 comments on commit bf60a5d

Please sign in to comment.