added commandline parse, use structopt

This commit is contained in:
asxalex 2024-02-18 21:47:43 +08:00
parent cb251fba8a
commit 209b870942
4 changed files with 29 additions and 2 deletions

View File

@ -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"

View File

@ -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();

View File

@ -3,3 +3,6 @@ pub use sn::*;
mod license;
pub use license::*;
mod params;
pub use params::*;

View File

@ -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,
}