Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network/Util: Extract network util functions from MainService #111

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}