kotlin 代码使用AIDL 技术实现一个服务端可以接收来自多个不同的APP客户端的请求,要区分来自哪一个客户端app的什么类型请求,把请求加入任务栈,任务栈用一个单独的类TaskStack来管理,任务和注册的回调消息绑定。在一个子线程中while循环取出任务栈中的任务,有一个Dispach类来管理任务的分发,在Dispach类中按照请求的优先级同步或者异步的将任务分发执行,执行完成后通过Dispach把消息回调给发请求的那个客户端app,要求线程安全,给出详细的每一个类的代码
时间: 2024-03-27 09:37:31 浏览: 54
首先是服务端的AIDL接口定义文件,包含了两个方法:register和request,register用于客户端注册,request用于客户端发送请求。
```aidl
interface ITaskService {
void register(in String packageName, in ITaskCallback callback);
void request(in String packageName, in String requestType, in int priority);
}
interface ITaskCallback {
void onTaskFinished(in String result);
}
```
下面是服务端的实现代码,TaskStack类用于管理任务栈,Dispatch类用于任务的分发,分为同步和异步两种方式。在register方法中,将客户端的包名和回调接口存储到Map中,request方法中将请求加入到任务栈中。
```kotlin
class TaskStack {
private val tasks: MutableList<Task> = mutableListOf()
private val lock = ReentrantLock()
private val condition = lock.newCondition()
fun addTask(task: Task) {
lock.lock()
try {
tasks.add(task)
condition.signal()
} finally {
lock.unlock()
}
}
fun getNextTask(): Task? {
lock.lock()
try {
while (tasks.isEmpty()) {
condition.await()
}
return tasks.removeAt(0)
} finally {
lock.unlock()
}
}
}
class Dispatch(private val taskStack: TaskStack, private val callbackMap: MutableMap<String, ITaskCallback>) {
private val lock = ReentrantLock()
fun start() {
while (true) {
val task = taskStack.getNextTask()
val callback = callbackMap[task.packageName]
if (callback != null) {
if (task.isSync) {
syncDispatch(task, callback)
} else {
asyncDispatch(task, callback)
}
}
}
}
private fun syncDispatch(task: Task, callback: ITaskCallback) {
lock.lock()
try {
val result = executeTask(task)
callback.onTaskFinished(result)
} finally {
lock.unlock()
}
}
private fun asyncDispatch(task: Task, callback: ITaskCallback) {
GlobalScope.launch {
val result = executeTask(task)
withContext(Dispatchers.Main) {
callback.onTaskFinished(result)
}
}
}
private fun executeTask(task: Task): String {
// do something
}
}
class TaskService : Service() {
private val callbackMap: MutableMap<String, ITaskCallback> = mutableMapOf()
private val taskStack: TaskStack = TaskStack()
private val dispatch: Dispatch = Dispatch(taskStack, callbackMap)
private val binder = object : ITaskService.Stub() {
override fun register(packageName: String?, callback: ITaskCallback?) {
if (packageName != null && callback != null) {
callbackMap[packageName] = callback
}
}
override fun request(packageName: String?, requestType: String?, priority: Int) {
if (packageName != null && requestType != null) {
val task = Task(packageName, requestType, priority)
taskStack.addTask(task)
}
}
}
override fun onBind(intent: Intent?): IBinder? {
return binder
}
override fun onCreate() {
super.onCreate()
dispatch.start()
}
}
data class Task(val packageName: String, val requestType: String, val priority: Int, val isSync: Boolean = true)
```
客户端代码如下,先通过bindService绑定服务,然后通过service注册和发送请求。回调接口ITaskCallback在onTaskFinished方法中处理来自服务端的结果。
```kotlin
class TaskClientActivity : AppCompatActivity() {
private var taskService: ITaskService? = null
private var taskCallback: ITaskCallback? = null
private val connection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName, service: IBinder) {
taskService = ITaskService.Stub.asInterface(service)
}
override fun onServiceDisconnected(name: ComponentName) {
taskService = null
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_task_client)
val intent = Intent()
intent.setComponent(ComponentName("com.example.taskserver", "com.example.taskserver.TaskService"))
bindService(intent, connection, Context.BIND_AUTO_CREATE)
taskCallback = object : ITaskCallback.Stub() {
override fun onTaskFinished(result: String?) {
// handle result
}
}
val packageName = packageName
taskService?.register(packageName, taskCallback)
taskService?.request(packageName, "requestType", 1)
}
override fun onDestroy() {
super.onDestroy()
if (taskCallback != null) {
taskService?.unregister(packageName, taskCallback)
}
unbindService(connection)
}
}
```
阅读全文