mod cleaner;
mod ctl;
mod monitor;
mod receiver;
mod sender;
use std::path::PathBuf;
use clap::{Parser, Subcommand};
#[derive(Debug, Parser)]
#[command(
name = "parachuter",
version,
about = "Reliable, rate-limited file downlink over lossy unidirectional UDP links."
)]
struct Cli {
#[command(subcommand)]
cmd: TopCmd,
}
#[derive(Debug, Subcommand)]
enum TopCmd {
Sender(DaemonArgs),
Receiver(ReceiverArgs),
Cleaner(DaemonArgs),
Ctl(ctl::CtlArgs),
Monitor(monitor::MonitorArgs),
}
#[derive(Debug, clap::Args)]
pub struct DaemonArgs {
#[arg(short, long, default_value = "/etc/parachuter/config.toml")]
pub config: PathBuf,
#[arg(long, default_value_t = false)]
pub once: bool,
}
#[derive(Debug, clap::Args)]
pub struct ReceiverArgs {
#[arg(short, long, default_value = "/etc/parachuter/config.toml")]
pub config: PathBuf,
}
fn init_tracing(default: &str) {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(default)),
)
.with_target(true)
.init();
}
fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
match cli.cmd {
TopCmd::Sender(args) => {
init_tracing("info,parachuter=debug");
tokio_runtime(4).block_on(sender::run(args))
}
TopCmd::Receiver(args) => {
init_tracing("info,parachuter=debug");
tokio_runtime(4).block_on(receiver::run(args))
}
TopCmd::Cleaner(args) => {
init_tracing("info,parachuter=debug");
tokio_runtime(2).block_on(cleaner::run(args))
}
TopCmd::Ctl(args) => {
init_tracing("warn");
tokio_runtime(1).block_on(ctl::run(args))
}
TopCmd::Monitor(args) => tokio_runtime(1).block_on(monitor::run(args)),
}
}
fn tokio_runtime(workers: usize) -> tokio::runtime::Runtime {
let mut builder = if workers <= 1 {
tokio::runtime::Builder::new_current_thread()
} else {
let mut b = tokio::runtime::Builder::new_multi_thread();
b.worker_threads(workers);
b
};
builder.enable_all().build().expect("tokio runtime")
}