64 lines
2.3 KiB
Rust
64 lines
2.3 KiB
Rust
use std::{io, io::Write, sync::Arc};
|
|
use crate::identity::Identity;
|
|
use crate::session_manager::SessionManager;
|
|
use text_io::read;
|
|
|
|
/*fn get_identity_or_create() -> identity::Identity {
|
|
match identity::get_identity() {
|
|
Ok(identity) => {identity}
|
|
Err(_) => {
|
|
identity::create_new_identidy("Bob").unwrap()
|
|
}
|
|
}
|
|
}*/
|
|
|
|
pub fn main(){
|
|
let identity = match Identity::get_identity() {
|
|
Ok(identity) => {identity}
|
|
Err(_) => {
|
|
println!("Creating new identity");
|
|
Identity::create_new_identidy("Bob").unwrap()
|
|
}
|
|
};
|
|
let session_manager = Arc::new(SessionManager::new(identity));
|
|
match SessionManager::start_listener(&session_manager) {
|
|
Ok(_) => {}
|
|
Err(e) => println!("{}", e.to_string())
|
|
}
|
|
SessionManager::start_receiver_loop(&session_manager);
|
|
let mut input = String::new();
|
|
while input != "exit" {
|
|
io::stdout().write(":~$ ".as_bytes()).unwrap();
|
|
io::stdout().flush().unwrap();
|
|
input = read!("{}\n");
|
|
let args: Vec<&str> = input.split(" ").collect();
|
|
if args[0] == "init" {
|
|
match session_manager.connect_to(args[1]) {
|
|
Ok(index) => {
|
|
println!("Connected !");
|
|
while input != "exit" {
|
|
io::stdout().write("BOB:~$ ".as_bytes()).unwrap();
|
|
io::stdout().flush().unwrap();
|
|
input = read!("{}\n");
|
|
match session_manager.send_to(&index, &input) {
|
|
Ok(_) => {
|
|
match session_manager.receive_from(index) {
|
|
Ok(msg) => println!("{}", std::str::from_utf8(&msg).unwrap()),
|
|
Err(e) => println!("{}", e.to_string())
|
|
}
|
|
}
|
|
Err(e) => println!("{}", e.to_string())
|
|
}
|
|
}
|
|
}
|
|
Err(e) => println!("{}", e.to_string())
|
|
}
|
|
} else if args[0] == "list" {
|
|
for (i, n) in session_manager.get_sessions() {
|
|
println!("{}: {}", i, n);
|
|
}
|
|
} else if args[0] != "exit" {
|
|
println!("?");
|
|
}
|
|
}
|
|
} |