Restructuring tokio tasks and stdin receiver

This commit is contained in:
Dave Hrycyszyn
2024-06-11 16:50:21 +01:00
parent 443c4e1dac
commit f3bea8c62d
3 changed files with 43 additions and 33 deletions

View File

@@ -8,6 +8,7 @@ pub(crate) struct SideNode {
crdt: BaseCrdt<TransactionList>,
keys: fastcrypto::ed25519::Ed25519KeyPair,
incoming_receiver: mpsc::Receiver<SignedOp>,
stdin_receiver: std::sync::mpsc::Receiver<String>,
}
impl SideNode {
@@ -15,25 +16,37 @@ impl SideNode {
crdt: BaseCrdt<TransactionList>,
keys: Ed25519KeyPair,
incoming_receiver: mpsc::Receiver<SignedOp>,
stdin_receiver: std::sync::mpsc::Receiver<String>,
) -> Self {
let node = Self {
crdt,
keys,
incoming_receiver,
stdin_receiver,
};
node
}
pub(crate) async fn start(&mut self) {
println!("Starting node...");
loop {
let incoming = self.incoming_receiver.recv().await.unwrap();
println!("Received incoming message: {:?}", incoming);
self.handle_incoming(incoming);
match self.stdin_receiver.try_recv() {
Ok(stdin) => {
println!("Received stdin input: {:?}", stdin);
}
Err(_) => {}
}
match self.incoming_receiver.try_recv() {
Ok(incoming) => {
self.handle_incoming(&incoming);
}
Err(_) => {}
}
}
}
fn handle_incoming(&mut self, incoming: SignedOp) {
fn handle_incoming(&mut self, incoming: &SignedOp) {
println!("WINNNINGINGINGINGINGIGNIGN");
self.crdt.apply(incoming.clone());
}