2023-03-22 18:34:06 +00:00
|
|
|
use std::sync::mpsc;
|
|
|
|
use std::sync::mpsc::Sender;
|
|
|
|
use std::thread;
|
2023-03-22 18:36:36 +00:00
|
|
|
use log::{error, info};
|
|
|
|
|
2023-03-22 18:34:06 +00:00
|
|
|
use crate::apiworker::{apiworker_main, APIWorkerMessage};
|
2023-03-21 17:00:01 +00:00
|
|
|
use crate::config::load_config;
|
2023-03-29 00:42:36 +00:00
|
|
|
|
2023-03-22 18:34:06 +00:00
|
|
|
use crate::nebulaworker::{nebulaworker_main, NebulaWorkerMessage};
|
|
|
|
use crate::socketworker::{socketworker_main, SocketWorkerMessage};
|
2023-03-29 13:37:03 +00:00
|
|
|
use crate::timerworker::{timer_main, TimerWorkerMessage};
|
2023-03-22 18:34:06 +00:00
|
|
|
use crate::util::check_server_url;
|
2023-03-21 17:00:01 +00:00
|
|
|
|
|
|
|
pub fn daemon_main(name: String, server: String) {
|
|
|
|
// Validate the `server`
|
2023-03-22 18:34:06 +00:00
|
|
|
check_server_url(&server);
|
|
|
|
|
|
|
|
info!("Loading config...");
|
|
|
|
let config = match load_config(&name) {
|
|
|
|
Ok(cfg) => cfg,
|
2023-03-21 17:00:01 +00:00
|
|
|
Err(e) => {
|
2023-03-22 18:34:06 +00:00
|
|
|
error!("Error loading configuration: {}", e);
|
2023-03-21 17:00:01 +00:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
2023-03-22 18:34:06 +00:00
|
|
|
|
2023-03-23 17:17:37 +00:00
|
|
|
info!("Creating transmitter");
|
2023-03-22 18:34:06 +00:00
|
|
|
|
|
|
|
let (tx_api, rx_api) = mpsc::channel::<APIWorkerMessage>();
|
|
|
|
let (tx_socket, rx_socket) = mpsc::channel::<SocketWorkerMessage>();
|
|
|
|
let (tx_nebula, rx_nebula) = mpsc::channel::<NebulaWorkerMessage>();
|
2023-03-29 13:37:03 +00:00
|
|
|
let (tx_timer, rx_timer) = mpsc::channel::<TimerWorkerMessage>();
|
2023-03-22 18:34:06 +00:00
|
|
|
|
|
|
|
let transmitter = ThreadMessageSender {
|
|
|
|
socket_thread: tx_socket,
|
|
|
|
api_thread: tx_api,
|
2023-03-29 13:37:03 +00:00
|
|
|
nebula_thread: tx_nebula,
|
|
|
|
timer_thread: tx_timer,
|
2023-03-22 18:34:06 +00:00
|
|
|
};
|
|
|
|
|
2023-03-23 17:17:37 +00:00
|
|
|
let mainthread_transmitter = transmitter.clone();
|
|
|
|
|
|
|
|
info!("Setting signal trap...");
|
|
|
|
|
|
|
|
match ctrlc::set_handler(move || {
|
|
|
|
info!("Ctrl-C detected. Stopping threads...");
|
|
|
|
match mainthread_transmitter.nebula_thread.send(NebulaWorkerMessage::Shutdown) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error sending shutdown message to nebula worker thread: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match mainthread_transmitter.api_thread.send(APIWorkerMessage::Shutdown) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error sending shutdown message to api worker thread: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match mainthread_transmitter.socket_thread.send(SocketWorkerMessage::Shutdown) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error sending shutdown message to socket worker thread: {}", e);
|
|
|
|
}
|
|
|
|
}
|
2023-03-29 13:37:03 +00:00
|
|
|
match mainthread_transmitter.timer_thread.send(TimerWorkerMessage::Shutdown) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error sending shutdown message to timer worker thread: {}", e);
|
|
|
|
}
|
|
|
|
}
|
2023-03-23 17:17:37 +00:00
|
|
|
}) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Unable to set sigtrap: {}", e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("Starting API thread...");
|
|
|
|
|
2023-03-22 18:34:06 +00:00
|
|
|
let config_api = config.clone();
|
|
|
|
let transmitter_api = transmitter.clone();
|
2023-03-27 16:32:26 +00:00
|
|
|
let name_api = name.clone();
|
2023-03-28 16:16:00 +00:00
|
|
|
let server_api = server.clone();
|
2023-03-22 18:34:06 +00:00
|
|
|
let api_thread = thread::spawn(move || {
|
2023-03-28 16:16:00 +00:00
|
|
|
apiworker_main(config_api, name_api, server_api,transmitter_api, rx_api);
|
2023-03-22 18:34:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
info!("Starting Nebula thread...");
|
|
|
|
let config_nebula = config.clone();
|
|
|
|
let transmitter_nebula = transmitter.clone();
|
2023-03-30 11:29:02 +00:00
|
|
|
let name_nebula = name.clone();
|
2023-03-22 18:34:06 +00:00
|
|
|
let nebula_thread = thread::spawn(move || {
|
2023-03-30 11:29:02 +00:00
|
|
|
nebulaworker_main(config_nebula, name, transmitter_nebula, rx_nebula);
|
2023-03-22 18:34:06 +00:00
|
|
|
});
|
|
|
|
|
2023-03-29 13:37:03 +00:00
|
|
|
info!("Starting timer thread...");
|
|
|
|
let timer_transmitter = transmitter.clone();
|
|
|
|
let timer_thread = thread::spawn(move || {
|
|
|
|
timer_main(timer_transmitter, rx_timer);
|
|
|
|
});
|
|
|
|
|
2023-03-22 18:34:06 +00:00
|
|
|
info!("Starting socket worker thread...");
|
|
|
|
let socket_thread = thread::spawn(move || {
|
2023-03-23 17:50:21 +00:00
|
|
|
socketworker_main(config, name.clone(), transmitter, rx_socket);
|
2023-03-22 18:34:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
info!("Waiting for socket thread to exit...");
|
|
|
|
match socket_thread.join() {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => {
|
|
|
|
error!("Error waiting for socket thread to exit.");
|
2023-03-21 17:00:01 +00:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
2023-03-23 17:17:37 +00:00
|
|
|
info!("Socket thread exited");
|
2023-03-21 17:00:01 +00:00
|
|
|
|
2023-03-22 18:34:06 +00:00
|
|
|
info!("Waiting for API thread to exit...");
|
|
|
|
match api_thread.join() {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => {
|
|
|
|
error!("Error waiting for api thread to exit.");
|
2023-03-21 17:00:01 +00:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2023-03-22 18:34:06 +00:00
|
|
|
}
|
2023-03-23 17:17:37 +00:00
|
|
|
info!("API thread exited");
|
2023-03-22 18:34:06 +00:00
|
|
|
|
2023-03-29 13:37:03 +00:00
|
|
|
info!("Waiting for timer thread to exit...");
|
|
|
|
match timer_thread.join() {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => {
|
|
|
|
error!("Error waiting for timer thread to exit.");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info!("Timer thread exited");
|
|
|
|
|
2023-03-22 18:34:06 +00:00
|
|
|
info!("Waiting for Nebula thread to exit...");
|
|
|
|
match nebula_thread.join() {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => {
|
|
|
|
error!("Error waiting for nebula thread to exit.");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
2023-03-23 17:17:37 +00:00
|
|
|
info!("Nebula thread exited");
|
2023-03-22 18:34:06 +00:00
|
|
|
|
|
|
|
info!("All threads exited");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ThreadMessageSender {
|
2023-03-23 17:17:37 +00:00
|
|
|
pub socket_thread: Sender<SocketWorkerMessage>,
|
|
|
|
pub api_thread: Sender<APIWorkerMessage>,
|
2023-03-29 13:37:03 +00:00
|
|
|
pub nebula_thread: Sender<NebulaWorkerMessage>,
|
|
|
|
pub timer_thread: Sender<TimerWorkerMessage>
|
2023-03-21 17:00:01 +00:00
|
|
|
}
|