diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index 09b78aa..72fb7bd 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -58,6 +58,9 @@ pub enum Error { #[error(display = "Credential error")] AuthFailed, + #[error(display = "Failed to translate camera address")] + AddrResolutionError, + #[error(display = "ADPCM Decoding Error")] AdpcmDecodingError(&'static str), @@ -81,22 +84,34 @@ impl Drop for BcCamera { } impl BcCamera { - pub fn new_with_addr(hostname: T) -> Result { - let address = hostname - .to_socket_addrs()? - .next() - .ok_or(Error::Other("Address resolution failed"))?; + pub fn connect(host: T) -> Result { + let addr_iter = match host.to_socket_addrs() { + Ok(iter) => iter, + Err(_) => return Err(Error::AddrResolutionError), + }; - Ok(Self { - address, - connection: None, - logged_in: false, - }) - } + for addr in addr_iter { + debug!("Trying {}", addr); + let conn = match BcConnection::new(addr, RX_TIMEOUT) { + Ok(conn) => conn, + Err(err) => match err { + connection::Error::CommunicationError(ref err) => { + debug!("Assuming timeout from {}", err); + continue; + } + err => return Err(err.into()), + }, + }; - pub fn connect(&mut self) -> Result<()> { - self.connection = Some(BcConnection::new(self.address, RX_TIMEOUT)?); - Ok(()) + debug!("Success: {}", addr); + return Ok(Self { + address: addr, + connection: Some(conn), + logged_in: false, + }); + } + + return Err(Error::Timeout); } pub fn disconnect(&mut self) { diff --git a/src/config.rs b/src/config.rs index 79b4af5..d99ae55 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,7 +2,6 @@ use lazy_static::lazy_static; use regex::Regex; use serde::Deserialize; use std::clone::Clone; -use std::net::SocketAddr; use std::time::Duration; use validator::{Validate, ValidationError}; use validator_derive::Validate; @@ -45,7 +44,7 @@ pub struct CameraConfig { pub name: String, #[serde(rename = "address")] - pub camera_addr: SocketAddr, + pub camera_addr: String, pub username: String, pub password: Option, diff --git a/src/main.rs b/src/main.rs index ffba065..44a63f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -198,7 +198,6 @@ fn camera_main( ) -> Result { let mut connected = false; (|| { - let mut camera = BcCamera::new_with_addr(camera_config.camera_addr)?; if camera_config.timeout.is_some() { warn!("The undocumented `timeout` config option has been removed and is no longer needed."); warn!("Please update your config file."); @@ -208,7 +207,8 @@ fn camera_main( "{}: Connecting to camera at {}", camera_config.name, camera_config.camera_addr ); - camera.connect()?; + + let mut camera = BcCamera::connect(&camera_config.camera_addr)?; camera.login(&camera_config.username, camera_config.password.as_deref())?;