add crate cli_helpers with option and config parsing; use in aquatic

Putting cli functionality into its own crate will allow using it
from aquatic_bench and possibly other programs.
This commit is contained in:
Joakim Frostegård 2020-04-09 16:46:35 +02:00
parent 06756f1c74
commit ac52668a3d
9 changed files with 147 additions and 10 deletions

View file

@ -13,6 +13,7 @@ name = "aquatic"
[dependencies]
bittorrent_udp = { path = "../bittorrent_udp" }
cli_helpers = { path = "../cli_helpers" }
dashmap = "3"
histogram = "0.6"
indexmap = "1"
@ -20,6 +21,7 @@ mimalloc = { version = "0.1", default-features = false }
mio = { version = "0.7", features = ["udp", "os-poll", "os-util"] }
net2 = "0.2"
rand = { version = "0.7", features = ["small_rng"] }
serde = { version = "1", features = ["derive"] }
[dev-dependencies]
quickcheck = "0.9"

View file

@ -1,4 +1,5 @@
use aquatic;
use cli_helpers;
#[global_allocator]
@ -6,5 +7,8 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
fn main(){
aquatic::run();
cli_helpers::run_with_cli_and_config(
"aquatic: udp bittorrent tracker",
aquatic::run,
)
}

View file

@ -1,7 +1,9 @@
use std::net::SocketAddr;
use serde::{Serialize, Deserialize};
#[derive(Clone)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
/// Spawn this number of threads for workers
pub num_threads: usize,
@ -11,7 +13,7 @@ pub struct Config {
}
#[derive(Clone)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NetworkConfig {
/// Bind to this address
pub address: SocketAddr,
@ -27,14 +29,14 @@ pub struct NetworkConfig {
}
#[derive(Clone)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StatisticsConfig {
/// Print statistics this often (seconds). Don't print when set to zero.
pub interval: u64,
}
#[derive(Clone)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CleaningConfig {
/// Clean torrents and connections this often (seconds)
pub interval: u64,
@ -88,4 +90,4 @@ impl Default for CleaningConfig {
max_connection_age: 60 * 5,
}
}
}
}

View file

@ -10,8 +10,7 @@ use config::Config;
use common::State;
pub fn run(){
let config = Config::default();
pub fn run(config: Config){
let state = State::new();
for i in 0..config.num_threads {