Skip to content

Commit

Permalink
Network/Util: Extract network util functions from MainService
Browse files Browse the repository at this point in the history
This patch extracts network realted util functions from MainService.

Signed-off-by: Yelin Jeong <yelini.jeong@samsung.com>
  • Loading branch information
niley7464 authored and wooksong committed Sep 25, 2024
1 parent 1ce8455 commit 48b47d9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import ai.nnstreamer.ml.inference.offloading.data.OffloadingService
import ai.nnstreamer.ml.inference.offloading.data.OffloadingServiceRepositoryImpl
import ai.nnstreamer.ml.inference.offloading.data.PreferencesDataStoreImpl
import ai.nnstreamer.ml.inference.offloading.network.NsdRegistrationListener
import ai.nnstreamer.ml.inference.offloading.network.findPort
import ai.nnstreamer.ml.inference.offloading.network.getIpAddress
import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
Expand All @@ -13,7 +15,6 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ServiceInfo
import android.net.ConnectivityManager
import android.net.nsd.NsdManager
import android.net.nsd.NsdManager.RegistrationListener
import android.net.nsd.NsdServiceInfo
Expand All @@ -36,10 +37,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.nnsuite.nnstreamer.NNStreamer
import org.nnsuite.nnstreamer.Pipeline
import java.net.Inet4Address
import java.net.ServerSocket
import javax.inject.Inject
import kotlin.concurrent.thread

/**
* Enum class representing different types of messages that can be sent.
Expand Down Expand Up @@ -318,49 +316,8 @@ class MainService : Service() {
}
}

// TODO: Add an ApplicationContext Parameter
private fun getIpAddress(): String {
val connectivityManager =
App.context().getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
val network = connectivityManager.activeNetwork
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 inetAddress
}

private fun findPort(): Int {
var port = -1
val portFinder = thread() {
try {
val serverSocket = ServerSocket(0)
Log.i(TAG, "listening on port: " + serverSocket.localPort)
port = serverSocket.localPort
serverSocket.close()
} catch (e: Exception) {
Log.e(TAG, e.toString())
}
}
portFinder.join()
return port
}

private fun loadModels() {
val hostAddress = getIpAddress()
val hostAddress = getIpAddress(isRunningOnEmulator)
val models = modelsRepository.getAllModelsStream()

CoroutineScope(Dispatchers.IO).launch {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ai.nnstreamer.ml.inference.offloading.network

import ai.nnstreamer.ml.inference.offloading.App
import android.app.Service
import android.net.ConnectivityManager
import android.util.Log
import java.net.Inet4Address
import java.net.ServerSocket
import kotlin.concurrent.thread

const val TAG = "NetworkUtil"

/**
* Returns the IP address based on whether the app is running on an emulator or not.
*
* @param isRunningOnEmulator Indicates whether the app is running on an emulator.
* @return The IP address as a string.
*/
fun getIpAddress(isRunningOnEmulator: Boolean): String {
val connectivityManager =
App.context().getSystemService(Service.CONNECTIVITY_SERVICE) as ConnectivityManager
val network = connectivityManager.activeNetwork
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 inetAddress
}

/**
* Finds an available port for the server socket.
*
* @return The available port number as an integer.
*/
fun findPort(): Int {
var port = -1
val portFinder = thread() {
try {
val serverSocket = ServerSocket(0)
Log.i(TAG, "listening on port: " + serverSocket.localPort)
port = serverSocket.localPort
serverSocket.close()
} catch (e: Exception) {
Log.e(TAG, e.toString())
}
}
portFinder.join()
return port
}

0 comments on commit 48b47d9

Please sign in to comment.