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

App: Seperate pipeline pause and close #35

Merged
merged 1 commit into from
Jun 21, 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 @@ -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)
}
}
}
10 changes: 8 additions & 2 deletions ml_inference_offloading/src/main/res/layout/models.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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>