Skip to content

Commit

Permalink
App/Service: Find available port using serverSocket
Browse files Browse the repository at this point in the history
This patch adds initPort function to find availble port for tensor_query_server

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 committed Apr 22, 2024
1 parent ed95754 commit c388ec9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions ml_inference_offloading/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat
import org.nnsuite.nnstreamer.NNStreamer
import java.net.ServerSocket
import kotlin.concurrent.thread


class MainService : Service() {
Expand All @@ -40,6 +42,7 @@ class MainService : Service() {
private lateinit var serviceLooper : Looper
private lateinit var handlerThread: HandlerThread
private var initialized = false
private var serverPort = 0

private fun startForeground() {
// Get NotificationManager
Expand Down Expand Up @@ -78,6 +81,7 @@ class MainService : Service() {

override fun onCreate() {
initNNStreamer()
initPort()
handlerThread = HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND).apply {
start()
}
Expand Down Expand Up @@ -125,4 +129,18 @@ class MainService : Service() {
}
}
}

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

0 comments on commit c388ec9

Please sign in to comment.