//! # Chat //! //! Super simple console chat application without any fancy design. //! Only intended for demo purposes. use actaeon::{ config::Config, node::{Center, ToAddress}, topic::Topic, Interface, }; use sodiumoxide::crypto::box_; use std::io; use std::sync::mpsc; fn main() -> io::Result<()> { let config = Config::new(20, 1, 100, "example.com".to_string(), 4242); let (_, secret) = box_::gen_keypair(); let center = Center::new(secret, String::from("127.0.0.1"), 4242); let interface = Interface::new(config, center).unwrap(); let (s, r) = mpsc::channel(); std::thread::sleep(std::time::Duration::from_millis(125)); println!("Actaeon Chat Example Application!"); println!(" - - - - - - - - - - - - - - "); println!("Enter chat room name: "); let mut buffer = String::new(); let stdin = io::stdin(); stdin.read_line(&mut buffer)?; let topic = buffer.to_address(); let topic = interface.subscribe(&topic); receiver(topic, r); loop { println!("Send: "); let mut buffer = String::new(); let stdin = io::stdin(); stdin.read_line(&mut buffer)?; let message = buffer.as_bytes().to_vec(); let _ = s.send(message); } } fn receiver(mut topic: Topic, recv: mpsc::Receiver>) { std::thread::spawn(move || loop { if let Some(msg) = topic.try_recv() { let body = msg.message.body.as_bytes(); let message = String::from_utf8_lossy(&body); let from = &msg.source().as_bytes()[0]; println!("{}: {}", from, message); } if let Ok(bytes) = recv.try_recv() { let _ = topic.broadcast(bytes); } }); }