Skip to content

Commit

Permalink
Updated examples to use the latest APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Kim committed Feb 24, 2024
1 parent 1ba5343 commit 3019066
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
16 changes: 5 additions & 11 deletions example-hello/src/bin/hello_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,20 +32,17 @@ impl DeathRecipient for MyDeathRecipient {
fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
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<dyn IHello> = hub::get_interface(SERVICE_NAME)
Expand All @@ -58,8 +55,5 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {

println!("Result: {echo}");

// sleep 1 second
// std::thread::sleep(std::time::Duration::from_secs(1));

Ok(ProcessState::join_thread_pool()?)
}
20 changes: 14 additions & 6 deletions example-hello/src/bin/hello_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
Ok(echo.to_owned())
}
Expand All @@ -25,16 +29,20 @@ impl IHello for IHelloService {
fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
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()?)
}
3 changes: 3 additions & 0 deletions example-hello/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Copyright 2022 Jeff Kim <[email protected]>
// 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";
2 changes: 1 addition & 1 deletion rsbinder-aidl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion rsbinder-tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion rsbinder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3019066

Please sign in to comment.