Skip to content

Commit

Permalink
App: Seperate pipeline pause and close
Browse files Browse the repository at this point in the history
This patch seperates pipeline pause an close.
Also, now we can see pipeline status in main activity.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 committed Jun 14, 2024
1 parent d56a9b7 commit 9f249a8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,23 @@ class MainActivity : ComponentActivity() {
fun bind(info: ModelInfo) {
val start = itemView.findViewById<Button>(R.id.start)
val stop = itemView.findViewById<Button>(R.id.stop)
val port = itemView.findViewById<TextView>(R.id.port)
val close = itemView.findViewById<Button>(R.id.close)
val status = itemView.findViewById<TextView>(R.id.status)

start.setOnClickListener(View.OnClickListener {
val serverPort = mService?.startServer(info.name, info.filter)
port.text = "Listening on port: " + serverPort.toString();
mService?.startServer(info.name, info.filter)
mService?.getPort(info.name)?.let { serverPort ->
if (serverPort < 0) status.text = "Failed to start the server"
else status.text = "Listening on port: " + serverPort.toString();
}
})
stop.setOnClickListener(View.OnClickListener {
mService?.stopServer(info.name)
status.text = resources.getString(R.string.server_paused)
})
close.setOnClickListener(View.OnClickListener {
mService?.closeServer(info.name)
status.text = resources.getString(R.string.server_unknown)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import java.net.Inet4Address
import java.net.ServerSocket
import kotlin.concurrent.thread

// todo: Define DTO with generality and extensibility
data class ServerInfo(
val pipeline: Pipeline,
val port: Int,
var status: Pipeline.State
)

class MainService : Service() {
private inner class MainHandler(looper: Looper) : Handler(looper) {
Expand Down Expand Up @@ -73,8 +79,7 @@ class MainService : Service() {
private lateinit var serviceLooper : Looper
private lateinit var handlerThread: HandlerThread
private var initialized = false
private var port = -1
private var serverInfo = mutableMapOf<String,Pipeline>()
private var serverInfoMap = mutableMapOf<String,ServerInfo>()

private fun startForeground() {
// Get NotificationManager
Expand Down Expand Up @@ -145,8 +150,8 @@ class MainService : Service() {
}

override fun onDestroy() {
serverInfo.values.forEach { pipeline ->
pipeline.close()
serverInfoMap.values.forEach { status ->
status.pipeline.close()
}
Toast.makeText(this, "The MainService has been gone", Toast.LENGTH_SHORT).show()
}
Expand Down Expand Up @@ -228,22 +233,37 @@ class MainService : Service() {
return port
}

fun startServer(name:String, filter: String): Int {
val hostAddress = getIpAddress()
if (!isPortAvailable(port)) {
port = findPort()
}
fun getPort(name: String): Int {
return serverInfoMap[name]?.port ?: -1
}

val desc = "tensor_query_serversrc host=" + hostAddress + " port=" + port.toString() +
" ! " + filter + " ! tensor_query_serversink async=false"
val tensorQueryServer = Pipeline(desc, null)
serverInfo[name] = tensorQueryServer
tensorQueryServer.start()
fun startServer(name:String, filter: String) {
if (!serverInfoMap.containsKey(name)) {
val hostAddress = getIpAddress()
val port = findPort()
val desc = "tensor_query_serversrc host=" + hostAddress + " port=" + port.toString() +
" ! " + filter + " ! tensor_query_serversink async=false"
val tensorQueryServer = Pipeline(desc, null)
serverInfoMap[name] = ServerInfo(tensorQueryServer, port, Pipeline.State.UNKNOWN)
}

return port
serverInfoMap[name]?.let { modelStatus ->
modelStatus.pipeline.start()
modelStatus.status = Pipeline.State.PLAYING
}
}

fun stopServer(name:String) {
serverInfo[name]?.close()
serverInfoMap[name]?.let { modelStatus ->
modelStatus.pipeline.stop()
modelStatus.status = Pipeline.State.PAUSED
}
}

fun closeServer(name:String) {
serverInfoMap[name]?.let { modelStatus ->
modelStatus.pipeline.close()
serverInfoMap.remove(name)
}
}
}
12 changes: 9 additions & 3 deletions ml_inference_offloading/src/main/res/layout/models.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
android:text="Stop" />
<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close" />
<TextView
android:id="@+id/port"
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="wrap_content"
android:text="@string/server_unknown"/>
</LinearLayout>
4 changes: 3 additions & 1 deletion ml_inference_offloading/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<resources>
<string name="app_name">ML Inference Offloading</string>
<string name="title_activity_main">MainActivity</string>
</resources>
<string name="server_paused">Server is paused</string>
<string name="server_unknown">There is no tensor query server</string>
</resources>

0 comments on commit 9f249a8

Please sign in to comment.