Skip to main content

manycastr/worker/
mod.rs

1use crate::tls::TlsOptions;
2use clap::ArgMatches;
3use gethostname::gethostname;
4use std::error::Error;
5use std::sync::Arc;
6use std::sync::atomic::AtomicBool;
7
8pub(crate) use crate::worker::config::Worker;
9
10mod bpf;
11mod client;
12mod config;
13mod inbound;
14mod measurement;
15mod outbound;
16mod trace_codec;
17
18impl Worker {
19    /// Create a worker instance, which includes establishing a connection with the orchestrator.
20    ///
21    /// # Arguments
22    /// * `args` - contains the parsed command-line arguments
23    pub async fn new(args: &ArgMatches) -> Result<Worker, Box<dyn Error>> {
24        // Get hostname from command line arguments or use the system hostname
25        let hostname = args
26            .get_one::<String>("hostname")
27            .map(|h| h.parse::<String>().expect("Unable to parse hostname"))
28            .unwrap_or_else(|| gethostname().into_string().expect("Unable to get hostname"));
29
30        let orc_addr = args.get_one::<String>("orchestrator").unwrap();
31        let tls = TlsOptions::from_args(args);
32        let grpc_client = Self::connect(orc_addr.to_owned(), &tls).await?;
33
34        // Initialize a worker instance
35        let mut worker = Self {
36            grpc_client,
37            hostname,
38            is_busy: Arc::new(AtomicBool::new(false)),
39            outbound_txs: vec![],
40            outbound_handles: vec![],
41            abort_inbound: Arc::new(AtomicBool::new(false)),
42        };
43
44        worker.connect_to_server().await?;
45
46        Ok(worker)
47    }
48}