From 2aa41cd2b0fa2f19952e8d8dc77286e47ad8d8b1 Mon Sep 17 00:00:00 2001 From: Wook Song Date: Tue, 28 May 2024 08:57:24 +0900 Subject: [PATCH] App/Service: Revise the getIpAddress method This patch revises the getIpAddress method to return the proper localhost address corresponding to the emulator or the real target. Moreover, the platform type variables are eliminated. Signed-off-by: Wook Song --- .../ml/inference/offloading/MainService.kt | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainService.kt b/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainService.kt index ef8fe78..7169dab 100644 --- a/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainService.kt +++ b/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainService.kt @@ -232,19 +232,28 @@ class MainService : Service() { return result } - private fun getIpAddress(): String? { - val connectivityManager = applicationContext.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager + // TODO: Add an ApplicationContext Parameter + private fun getIpAddress(): String { + val connectivityManager = App.context().getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager val network = connectivityManager.activeNetwork - val linkProperties = connectivityManager.getLinkProperties(network) - val linkAddresses = linkProperties?.linkAddresses - linkAddresses?.forEach {linkAddress -> - val inetAddress = linkAddress.address - if (inetAddress is Inet4Address && !inetAddress.isLoopbackAddress && inetAddress.isSiteLocalAddress) { - return inetAddress.hostAddress + var inetAddress = if (isRunningOnEmulator) "10.0.2.2" else "localhost" + val linkProperties = connectivityManager.getLinkProperties(network) ?: return inetAddress + + for (linkAddress in linkProperties.linkAddresses) { + val address = linkAddress.address ?: continue + + when { + address !is Inet4Address -> continue + address.isLoopbackAddress -> continue + else -> { + inetAddress = address.hostAddress?:continue + + break + } } } - return "localhost" + return inetAddress } private fun findPort(): Int {