From 30190669fcf2b6d0ed0814d296b8390fc3b3bfba Mon Sep 17 00:00:00 2001 From: Jeff Kim Date: Sat, 24 Feb 2024 20:42:44 +0900 Subject: [PATCH] Updated examples to use the latest APIs. --- example-hello/src/bin/hello_client.rs | 16 +++++----------- example-hello/src/bin/hello_service.rs | 20 ++++++++++++++------ example-hello/src/lib.rs | 3 +++ rsbinder-aidl/Cargo.toml | 2 +- rsbinder-tools/Cargo.toml | 2 +- rsbinder/Cargo.toml | 2 +- 6 files changed, 25 insertions(+), 20 deletions(-) diff --git a/example-hello/src/bin/hello_client.rs b/example-hello/src/bin/hello_client.rs index d12e298..7f15d74 100644 --- a/example-hello/src/bin/hello_client.rs +++ b/example-hello/src/bin/hello_client.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use env_logger::Env; use rsbinder::*; -use hub::{IServiceManager, IServiceCallback, BnServiceCallback}; +use hub::{IServiceCallback, BnServiceCallback}; use example_hello::*; struct MyServiceCallback { @@ -32,20 +32,17 @@ impl DeathRecipient for MyDeathRecipient { fn main() -> std::result::Result<(), Box> { env_logger::Builder::from_env(Env::default().default_filter_or("warn")).init(); - // Initialize ProcessState with binder path and max threads. - // The meaning of zero max threads is to use the default value. It is dependent on the kernel. - ProcessState::init(DEFAULT_BINDER_PATH, 0); - // Get binder service manager. - let hub = hub::default(); + // Initialize ProcessState with the default binder path and the default max threads. + ProcessState::init_default(); println!("list services:"); // This is an example of how to use service manager. - for name in hub.listServices(hub::DUMP_FLAG_PRIORITY_DEFAULT)? { + for name in hub::list_services(hub::DUMP_FLAG_PRIORITY_DEFAULT) { println!("{}", name); } let service_callback = BnServiceCallback::new_binder(MyServiceCallback{}); - hub.registerForNotifications(SERVICE_NAME, &service_callback)?; + hub::register_for_notifications(SERVICE_NAME, &service_callback)?; // Create a Hello proxy from binder service manager. let hello: rsbinder::Strong = hub::get_interface(SERVICE_NAME) @@ -58,8 +55,5 @@ fn main() -> std::result::Result<(), Box> { println!("Result: {echo}"); - // sleep 1 second - // std::thread::sleep(std::time::Duration::from_secs(1)); - Ok(ProcessState::join_thread_pool()?) } \ No newline at end of file diff --git a/example-hello/src/bin/hello_service.rs b/example-hello/src/bin/hello_service.rs index cfb41dd..535a3cf 100644 --- a/example-hello/src/bin/hello_service.rs +++ b/example-hello/src/bin/hello_service.rs @@ -3,20 +3,24 @@ use env_logger::Env; use rsbinder::*; -use hub::IServiceManager; use example_hello::*; +// Define the name of the service to be registered in the HUB(service manager). struct IHelloService; +// Implement the IHello interface for the IHelloService. impl Interface for IHelloService { + // Reimplement the dump method. This is optional. fn dump(&self, writer: &mut dyn std::io::Write, _args: &[String]) -> Result<()> { writeln!(writer, "Dump IHelloService")?; Ok(()) } } +// Implement the IHello interface for the IHelloService. impl IHello for IHelloService { + // Implement the echo method. fn echo(&self, echo: &str) -> rsbinder::status::Result { Ok(echo.to_owned()) } @@ -25,16 +29,20 @@ impl IHello for IHelloService { fn main() -> std::result::Result<(), Box> { env_logger::Builder::from_env(Env::default().default_filter_or("warn")).init(); - // Initialize ProcessState with binder path and max threads. - // The meaning of zero max threads is to use the default value. It is dependent on the kernel. - ProcessState::init(DEFAULT_BINDER_PATH, 0); + // Initialize ProcessState with the default binder path and the default max threads. + ProcessState::init_default(); + + // Start the thread pool. + // This is optional. If you don't call this, only one thread will be created to handle the binder transactions. + ProcessState::start_thread_pool(); // Create a binder service. let service = BnHello::new_binder(IHelloService{}); // Add the service to binder service manager. - let hub = hub::default(); - hub.addService(SERVICE_NAME, &service.as_binder(), false, hub::DUMP_FLAG_PRIORITY_DEFAULT)?; + hub::add_service(SERVICE_NAME, service.as_binder())?; + // Join the thread pool. + // This is a blocking call. It will return when the thread pool is terminated. Ok(ProcessState::join_thread_pool()?) } diff --git a/example-hello/src/lib.rs b/example-hello/src/lib.rs index b5e48cc..a5d4797 100644 --- a/example-hello/src/lib.rs +++ b/example-hello/src/lib.rs @@ -1,8 +1,11 @@ // Copyright 2022 Jeff Kim // SPDX-License-Identifier: Apache-2.0 +// Include the code hello.rs generated from AIDL. include!(concat!(env!("OUT_DIR"), "/hello.rs")); +// Set up to use the APIs provided in the code generated for Client and Service. pub use crate::hello::IHello::*; +// Define the name of the service to be registered in the HUB(service manager). pub const SERVICE_NAME: &str = "my.hello"; \ No newline at end of file diff --git a/rsbinder-aidl/Cargo.toml b/rsbinder-aidl/Cargo.toml index 4acc8aa..4062e6a 100644 --- a/rsbinder-aidl/Cargo.toml +++ b/rsbinder-aidl/Cargo.toml @@ -4,7 +4,7 @@ version = "0.2.0" edition = "2021" license = "Apache-2.0" description = "This is a AIDL compiler for rsbinder." -homepage = "https://github.com/hiking90/rsbinder" +homepage = "https://hiking90.github.io/rsbinder-book/" repository = "https://github.com/hiking90/rsbinder/rsbinder-aidl" readme = "README.md" rust-version = "1.71" diff --git a/rsbinder-tools/Cargo.toml b/rsbinder-tools/Cargo.toml index 987e52d..86a723a 100644 --- a/rsbinder-tools/Cargo.toml +++ b/rsbinder-tools/Cargo.toml @@ -4,7 +4,7 @@ version = "0.2.0" edition = "2021" license = "Apache-2.0" description = "This provides a few CLI binder tools for Linux." -homepage = "https://github.com/hiking90/rsbinder" +homepage = "https://hiking90.github.io/rsbinder-book/" repository = "https://github.com/hiking90/rsbinder/rsbinder-tools" readme = "README.md" diff --git a/rsbinder/Cargo.toml b/rsbinder/Cargo.toml index c80527d..c092ea5 100644 --- a/rsbinder/Cargo.toml +++ b/rsbinder/Cargo.toml @@ -4,7 +4,7 @@ version = "0.2.0" edition = "2021" license = "Apache-2.0" description = "This is a library for Linux Binder communication." -homepage = "https://github.com/hiking90/rsbinder" +homepage = "https://hiking90.github.io/rsbinder-book/" repository = "https://github.com/hiking90/rsbinder" readme = "README.md" rust-version = "1.71"