diff --git a/Cargo.lock b/Cargo.lock index c1397e3..c3e87c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -55,6 +55,8 @@ name = "hptp-send" version = "0.1.0" dependencies = [ "hptp", + "thiserror", + "tokio", ] [[package]] diff --git a/hptp-send/Cargo.toml b/hptp-send/Cargo.toml index 128ccb3..70ae0ae 100644 --- a/hptp-send/Cargo.toml +++ b/hptp-send/Cargo.toml @@ -8,4 +8,6 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -hptp = { path = "../hptp" } \ No newline at end of file +hptp = { path = "../hptp" } +tokio = { version = "0.2.*", features = ["rt-core", "udp"] } +thiserror = "*" \ No newline at end of file diff --git a/hptp-send/src/main.rs b/hptp-send/src/main.rs index d200e36..2f13df7 100644 --- a/hptp-send/src/main.rs +++ b/hptp-send/src/main.rs @@ -1,41 +1,53 @@ -use std::str::FromStr; - +// #![feature(backtrace)] extern crate hptp; +extern crate tokio; +#[macro_use] +extern crate thiserror; -#[derive(Debug)] -struct Hostname { - ip: String, - port: u16, -} +use std::net::SocketAddrV4; -struct InvalidHostnameError; - -impl FromStr for Hostname { - type Err = InvalidHostnameError; - fn from_str(s: &str) -> Result { - let i = s.find(':').ok_or(InvalidHostnameError)?; - Ok(Hostname { - ip: s[..i].into(), - port: s[i + 1..].parse().map_err(|_| InvalidHostnameError)?, - }) - } -} - -fn usage() { - print!("usage:\n./3700send :\n") -} - -fn parse_args() -> Result { - std::env::args().nth(1).ok_or(())?.parse().map_err(|_| ()) +#[derive(Error, Debug)] +enum Error { + #[error("invalid command-line arguments")] + InvalidArgs, } fn main() { - match parse_args() { - Ok(x) => start(x), - Err(_) => usage(), + match entry() { + Err(Error::InvalidArgs) => print_usage(), + /* + Err(e) => { + use std::error::Error; + println!("Error: {:?}", e); + for bt in e.backtrace() { + println!("{}", bt); + } + } + */ + Ok(()) => (), } } -fn start(hn: Hostname) { - println!("ip={}, port={}", hn.ip, hn.port) +fn print_usage() { + print!("usage:\n./3700send :\n") +} + +fn entry() -> Result<(), Error> { + let mut rt = tokio::runtime::Runtime::new().unwrap(); + // let mut log = Logger::new(); + rt.block_on(start()) +} + +async fn start() -> Result<(), Error> { + let addr = parse_arg()?; + println!("ip={}, port={}", addr.ip(), addr.port()); + Ok(()) +} + +fn parse_arg() -> Result { + std::env::args() + .nth(1) + .ok_or(Error::InvalidArgs)? + .parse() + .map_err(|_| Error::InvalidArgs) }