Files
bft-crdt-experiment/side-node/src/main.rs

68 lines
1.9 KiB
Rust
Raw Normal View History

2024-06-11 16:52:40 +01:00
use std::io::BufRead;
use bft_json_crdt::json_crdt::{BaseCrdt, SignedOp};
2024-06-06 19:32:29 +01:00
use cli::{parse_args, Commands};
2024-06-07 18:42:28 +01:00
use crdt::TransactionList;
use node::SideNode;
use tokio::{sync::mpsc, task};
use websocket::WebSocketClient;
2024-05-29 12:44:52 +01:00
2024-05-29 13:17:16 +01:00
pub(crate) mod cli;
2024-06-07 18:42:28 +01:00
pub(crate) mod crdt;
2024-05-29 16:47:35 +01:00
pub(crate) mod init;
pub(crate) mod keys;
pub(crate) mod node;
pub(crate) mod utils;
pub(crate) mod websocket;
2024-05-29 13:17:16 +01:00
2024-05-29 12:44:52 +01:00
#[tokio::main]
async fn main() {
let args = parse_args();
2024-05-29 12:44:52 +01:00
match &args.command {
Some(Commands::Init { name }) => {
2024-06-06 15:54:33 +01:00
let config = init::config::SideNodeConfig {
name: name.to_string(),
2024-06-06 15:54:33 +01:00
};
let _ = init::init(utils::home(name), config);
}
Some(Commands::Run { name }) => {
let mut node = setup(name).await;
node.start().await;
}
2024-05-29 16:47:35 +01:00
None => println!("No command provided. Exiting. See --help for more information."),
2024-05-29 12:44:52 +01:00
}
2024-05-29 08:32:40 +01:00
}
2024-06-07 17:18:46 +01:00
/// Wire everything up outside the application so we can test more easily later
async fn setup(name: &String) -> SideNode {
let side_dir = utils::home(name);
let keys = keys::load_from_file(side_dir);
let (incoming_sender, incoming_receiver) = mpsc::channel::<SignedOp>(32);
let (stdin_sender, stdin_receiver) = std::sync::mpsc::channel();
task::spawn(async move {
stdin_input(stdin_sender);
});
let crdt = BaseCrdt::<TransactionList>::new(&keys);
let node = SideNode::new(crdt, keys, incoming_receiver, stdin_receiver);
tokio::spawn(async move {
WebSocketClient::start(incoming_sender).await;
});
println!("Node setup complete.");
node
}
2024-06-10 16:43:45 +01:00
2024-06-11 16:52:15 +01:00
/// Wait for stdin terminal input and send it to the node if any arrives
fn stdin_input(stdin_sender: std::sync::mpsc::Sender<String>) {
let stdin = std::io::stdin();
let lines = stdin.lock().lines();
for line in lines {
println!("We're in stdin_input");
let line = line.unwrap();
2024-06-11 16:52:15 +01:00
stdin_sender.send(line).unwrap();
}
}