Skip to content

Commit

Permalink
handle client failure state during initialization
Browse files Browse the repository at this point in the history
Signed-off-by: Walker Crouse <Walker.Crouse@bjss.com>
  • Loading branch information
Walker Crouse authored and Walker Crouse committed Jan 28, 2024
1 parent a94af65 commit bc05415
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
10 changes: 9 additions & 1 deletion zeroconf/src/avahi/avahi_util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Utilities related to Avahi

use avahi_sys::{avahi_address_snprint, avahi_strerror, AvahiAddress};
use avahi_sys::{avahi_address_snprint, avahi_strerror, AvahiAddress, AvahiClient};
use libc::c_char;
use std::ffi::CStr;

Expand Down Expand Up @@ -38,6 +38,14 @@ pub fn get_error<'a>(code: i32) -> &'a str {
}
}

/// Returns the last error message associated with the specified `*mut AvahiClient`.
///
/// # Safety
/// This function is unsafe because of internal Avahi calls.
pub unsafe fn get_last_error<'a>(client: *mut AvahiClient) -> &'a str {
get_error(avahi_sys::avahi_client_errno(client))
}

/// Converts the specified [`NetworkInterface`] to the Avahi expected value.
///
/// [`NetworkInterface`]: ../../enum.NetworkInterface.html
Expand Down
13 changes: 7 additions & 6 deletions zeroconf/src/avahi/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,15 @@ unsafe extern "C" fn client_callback(
state: AvahiClientState,
userdata: *mut c_void,
) {
let context = AvahiBrowserContext::from_raw(userdata);

match state {
avahi_sys::AvahiClientState_AVAHI_CLIENT_S_RUNNING => {
create_browser(client, AvahiBrowserContext::from_raw(userdata))
.unwrap_or_else(|e| panic!("failed to create browser: {}", e))
}
_ => {
// TODO: handle other states
avahi_sys::AvahiClientState_AVAHI_CLIENT_S_RUNNING => create_browser(client, context)
.unwrap_or_else(|e| panic!("failed to create browser: {}", e)),
avahi_sys::AvahiClientState_AVAHI_CLIENT_FAILURE => {
context.invoke_callback(Err(avahi_util::get_last_error(client).into()))
}
_ => {}
}
}

Expand Down
15 changes: 8 additions & 7 deletions zeroconf/src/avahi/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl AvahiServiceContext {
if let Some(f) = &self.registered_callback {
f(result, self.user_context.clone());
} else {
panic!("attempted to invoke service callback but none was set");
warn!("attempted to invoke service callback but none was set");
}
}
}
Expand All @@ -183,14 +183,15 @@ unsafe extern "C" fn client_callback(
state: AvahiClientState,
userdata: *mut c_void,
) {
let context = AvahiServiceContext::from_raw(userdata);

match state {
avahi_sys::AvahiClientState_AVAHI_CLIENT_S_RUNNING => {
create_service(client, AvahiServiceContext::from_raw(userdata))
.unwrap_or_else(|e| panic!("failed to create service: {}", e))
}
_ => {
// TODO: handle other states
avahi_sys::AvahiClientState_AVAHI_CLIENT_S_RUNNING => create_service(client, context)
.unwrap_or_else(|e| panic!("failed to create service: {}", e)),
avahi_sys::AvahiClientState_AVAHI_CLIENT_FAILURE => {
context.invoke_callback(Err(avahi_util::get_last_error(client).into()))
}
_ => {}
}
}

Expand Down
4 changes: 3 additions & 1 deletion zeroconf/src/tests/service_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ fn service_register_is_browsable() {
service.set_context(Box::new(context.clone()));
service.set_txt_record(txt.clone());

service.set_registered_callback(Box::new(|_, context| {
service.set_registered_callback(Box::new(|result, context| {
assert!(result.is_ok());

let mut browser =
MdnsBrowser::new(ServiceType::with_sub_types("http", "tcp", vec!["printer"]).unwrap());

Expand Down

0 comments on commit bc05415

Please sign in to comment.