diff --git a/Cargo.toml b/Cargo.toml index e9bbbdc..88c062c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,12 +8,15 @@ edition = "2018" [dependencies] cookie-factory = "0.3.1" +crossbeam-utils = "0.7" env_logger = "*" err-derive = "0.2.4" log = { version = "0.4.8", features = ["release_max_level_warn"] } indoc = "0.3.5" md5 = "0.7.0" nom = "5.1.1" +serde = { version = "1.0", features = ["derive"] } +toml = "0.5" yaserde = { git = "https://github.com/media-io/yaserde.git" } yaserde_derive = { git = "https://github.com/media-io/yaserde.git" } xml-rs = "0.8.2" diff --git a/sample_config.toml b/sample_config.toml new file mode 100644 index 0000000..611f559 --- /dev/null +++ b/sample_config.toml @@ -0,0 +1,13 @@ +[[cameras]] +name = "driveway" +username = "admin" +password = "12345678" +address = "192.168.1.187:9000" +serve = { port = 9900 } + +[[cameras]] +name = "storage shed" +username = "admin" +password = "987654321" +address = "192.168.1.245:9000" +serve = { port = 9901 } diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index b8b43d3..42f0492 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -2,7 +2,7 @@ use err_derive::Error; use log::*; use md5; use std::io::Write; -use std::net::{IpAddr, SocketAddr, ToSocketAddrs, TcpStream}; +use std::net::{SocketAddr, ToSocketAddrs, TcpStream}; use super::bc::{model::*, xml::*}; use Md5Trunc::*; @@ -44,10 +44,6 @@ impl Drop for BcCamera { } impl BcCamera { - pub fn new_with_ip(ip_addr: IpAddr) -> Result { - Self::new_with_addr((ip_addr, 9000)) - } - pub fn new_with_addr(hostname: T) -> Result { let address = hostname.to_socket_addrs()?.next() .ok_or(Error::OtherError("Address resolution failed"))?; diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..d7a05f3 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,48 @@ +use serde::Deserialize; +use std::fmt; +use std::net::SocketAddr; +use std::net::ToSocketAddrs; + +#[derive(Debug, Deserialize)] +pub struct Config { + pub cameras: Vec, +} + +#[derive(Debug, Deserialize)] +pub struct CameraConfig { + pub name: String, + + #[serde(rename="address")] + pub camera_addr: SocketAddr, + + #[serde(rename="serve")] + pub bind_addr: BindAddr, + + pub stream: Option, + pub username: String, + pub password: Option, +} + +#[derive(Debug, PartialEq, Eq, Deserialize)] +pub struct BindAddr { + host: Option, + port: u16, +} + +impl fmt::Display for BindAddr { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if let Some(ref host) = self.host { + write!(f, "{}:{}", host, self.port) + } else { + write!(f, "port {}", self.port) + } + } +} + +impl ToSocketAddrs for BindAddr { + type Iter = std::vec::IntoIter; + fn to_socket_addrs(&self) -> std::io::Result { + let host = self.host.as_deref().unwrap_or("0.0.0.0"); + (host, self.port).to_socket_addrs() + } +} diff --git a/src/main.rs b/src/main.rs index 2291916..20ec7f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,34 +1,56 @@ -#![allow(dead_code)] // while I'm still fleshing this out - -use err_derive::Error; -use std::env::args; -use std::net::TcpListener; - mod bc; mod bc_protocol; +mod config; use bc_protocol::BcCamera; +use config::{Config, CameraConfig}; +use crossbeam_utils::thread; +use err_derive::Error; +use std::env::args; +use std::fs; +use std::net::TcpListener; #[derive(Debug, Error)] pub enum Error { + #[error(display="Configuration parsing error")] + ConfigError(#[error(source)] toml::de::Error), #[error(display="Communication error")] ProtocolError(#[error(source)] bc_protocol::Error), - #[error(display="Socket setup error")] + #[error(display="I/O error")] IoError(#[error(source)] std::io::Error), } fn main() -> Result<(), Error> { - let mut camera = BcCamera::new_with_addr(args().nth(1).unwrap())?; - camera.connect()?; - camera.login("admin", Some("12345678"))?; + let config_path = args().nth(1).expect("A config file must be supplied on the command line"); + let config: Config = toml::from_str(&fs::read_to_string(config_path)?)?; - let bind_addr = "0.0.0.0:9999"; - println!("Logged in to camera; awaiting connection on {}", bind_addr); + thread::scope(|s| { + for camera in config.cameras { + s.spawn(move |_| { + // TODO handle these errors + camera_main(&camera) + }); + } + }).unwrap(); + + Ok(()) +} + +fn camera_main(camera_config: &CameraConfig) -> Result<(), Error> { + let mut camera = BcCamera::new_with_addr(camera_config.camera_addr)?; + + println!("{}: Connecting to camera at {}", camera_config.name, camera_config.camera_addr); + + camera.connect()?; + camera.login(&camera_config.username, camera_config.password.as_deref())?; + + let bind_addr = &camera_config.bind_addr; + println!("{}: Logged in to camera; awaiting connection on {}", camera_config.name, bind_addr); let listener = TcpListener::bind(bind_addr)?; let (mut out_socket, remote_addr) = listener.accept()?; - println!("Connected to {}, starting video stream", remote_addr); + println!("{}: Connected to {}, starting video stream", camera_config.name, remote_addr); camera.start_video(&mut out_socket)?;