Back to Comprehensive Rust

AIDL Server

src/android/aidl/example-service/server.md

latest1.5 KB
Original Source
<!-- Copyright 2024 Google LLC SPDX-License-Identifier: CC-BY-4.0 -->

AIDL Server

Finally, we can create a server which exposes the service:

birthday_service/src/server.rs:

rust,ignore
# // Copyright 2024 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#include ../birthday_service/src/server.rs:main}}

birthday_service/Android.bp:

javascript
{{#include ../birthday_service/Android.bp:birthday_server}}
<details>

The process for taking a user-defined service implementation (in this case, the BirthdayService type, which implements the IBirthdayService) and starting it as a Binder service has multiple steps. This may appear more complicated than students are used to if they've used Binder from C++ or another language. Explain to students why each step is necessary.

  1. Create an instance of your service type (BirthdayService).
  2. Wrap the service object in the corresponding Bn* type (BnBirthdayService in this case). This type is generated by Binder and provides common Binder functionality, similar to the BnBinder base class in C++. Since Rust doesn't have inheritance, we use composition, putting our BirthdayService within the generated BnBinderService.
  3. Call add_service, giving it a service identifier and your service object (the BnBirthdayService object in the example).
  4. Call join_thread_pool to add the current thread to Binder's thread pool and start listening for connections.
</details>