From 209b8709421e238251f6e43b65688fd256c1ae5f Mon Sep 17 00:00:00 2001 From: asxalex Date: Sun, 18 Feb 2024 21:47:43 +0800 Subject: [PATCH] added commandline parse, use structopt --- Cargo.toml | 1 + src/bin/sdlan-sn/main.rs | 15 +++++++++++++-- src/bin/sdlan-sn/utils/mod.rs | 3 +++ src/bin/sdlan-sn/utils/params.rs | 12 ++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/bin/sdlan-sn/utils/params.rs diff --git a/Cargo.toml b/Cargo.toml index a3187f3..15f94f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ rsa = "0.9.6" serde = { version = "1.0.196", features = ["derive"] } serde_json = "1.0.113" serde_repr = "0.1.18" +structopt = "0.3.26" tokio = { version = "1.36.0", features = ["full"] } tracing = "0.1.40" tracing-appender = "0.2.3" diff --git a/src/bin/sdlan-sn/main.rs b/src/bin/sdlan-sn/main.rs index a2afeb8..48ce8bd 100644 --- a/src/bin/sdlan-sn/main.rs +++ b/src/bin/sdlan-sn/main.rs @@ -3,11 +3,14 @@ use sdlan_sn_rs::packet; use sdlan_sn_rs::utils::Result; use tracing::{debug, error}; +use std::net::Ipv4Addr; //use std::io::Read; use std::net::SocketAddr; use std::time::Duration; use tokio::net::UdpSocket; +use structopt::StructOpt; + mod config; mod utils; @@ -36,16 +39,24 @@ async fn client(address: &str) -> Result<()> { #[tokio::main] async fn main() -> Result<()> { + // init log let _guard = log::init_log(); debug!("main starts here"); // check license license_ok()?; - let listener = UdpSocket::bind(SERVER).await?; + // parse command line argument + let args = utils::CommandLine::from_args(); + debug!("args: {:?}", args); + // check the argument + let _: Ipv4Addr = args.address.parse().expect("invalid address found"); + + let server = format!("{}:{}", args.address, args.port); + let listener = UdpSocket::bind(server).await?; let supernode = utils::SuperNode::new(listener); - utils::init_supernode(supernode); + utils::init_supernode(supernode).expect("failed to init supernode"); tokio::spawn(async { client(SERVER).await.unwrap(); diff --git a/src/bin/sdlan-sn/utils/mod.rs b/src/bin/sdlan-sn/utils/mod.rs index a9158f4..2914948 100644 --- a/src/bin/sdlan-sn/utils/mod.rs +++ b/src/bin/sdlan-sn/utils/mod.rs @@ -3,3 +3,6 @@ pub use sn::*; mod license; pub use license::*; + +mod params; +pub use params::*; diff --git a/src/bin/sdlan-sn/utils/params.rs b/src/bin/sdlan-sn/utils/params.rs new file mode 100644 index 0000000..1bb30a7 --- /dev/null +++ b/src/bin/sdlan-sn/utils/params.rs @@ -0,0 +1,12 @@ +use structopt::StructOpt; + +#[derive(StructOpt, Debug)] +pub struct CommandLine { + /// fedration, other sn + #[structopt(short = "f", long = "fedration", default_value = "")] + pub fedration: String, + #[structopt(short = "p", long = "port", default_value = "7655")] + pub port: u16, + #[structopt(short = "a", long = "addr", default_value = "0.0.0.0")] + pub address: String, +}