use std::thread;
use tokio::sync::{broadcast, mpsc, watch};
pub mod consumer_state;
mod tokio_server;
pub fn start() -> Result<(), ()> {
let (ser_thread_alive_tokio_tx, ser_alive_consumer_rx) = {
watch::channel::<bool>(false)
};
let (cli_conn_tokio_tx, cli_conn_consumer_rx) = {
mpsc::channel::<String>(16)
};
let (ser_msg_tokio_tx, _) = {
broadcast::channel::<Vec<tungstenite::Message>>(16)
};
let ser_msg_consumer_tx = ser_msg_tokio_tx.clone();
let (cli_msg_store_tokio_tx, cli_msg_store_consumer_rx) = {
mpsc::channel::<tungstenite::Message>(16)
};
let (ser_req_shutdown_consumer_tx, ser_req_shutdown_tokio_rx) = {
watch::channel::<bool>(false)
};
use consumer_state as cs;
cs::set_value(&cs::CS_SER_ALIVE_RX, ser_alive_consumer_rx)
.expect("Failed to set consumer state channel!");
cs::set_value(&cs::CS_CLI_CONN_RX, cli_conn_consumer_rx)
.expect("Failed to set consumer state channel!");
cs::set_value(&cs::CS_SER_MSG_TX, ser_msg_consumer_tx)
.expect("Failed to set consumer state channel!");
cs::set_value(&cs::CS_CLI_MSG_RX, cli_msg_store_consumer_rx)
.expect("Failed to set consumer state channel!");
cs::set_value(&cs::CS_SER_REQ_SHUTDOWN_TX, ser_req_shutdown_consumer_tx)
.expect("Failed to set consumer state channel!");
let _thread_handle = thread::spawn(|| tokio_server::main(
ser_thread_alive_tokio_tx,
cli_conn_tokio_tx,
ser_msg_tokio_tx,
cli_msg_store_tokio_tx,
ser_req_shutdown_tokio_rx
));
Ok(())
}