2020-05-11 18:51:04 +00:00
|
|
|
use syndicate::{peer, spaces};
|
2019-09-19 09:54:01 +00:00
|
|
|
|
2019-10-16 15:54:58 +00:00
|
|
|
use std::sync::{Mutex, Arc};
|
|
|
|
use tokio::net::TcpListener;
|
2018-12-09 13:28:01 +00:00
|
|
|
|
2019-09-19 09:54:01 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2019-10-20 15:01:27 +00:00
|
|
|
let spaces = Arc::new(Mutex::new(spaces::Spaces::new()));
|
2019-09-19 09:54:01 +00:00
|
|
|
let mut id = 0;
|
|
|
|
|
2019-10-15 15:58:52 +00:00
|
|
|
let port = 8001;
|
|
|
|
let mut listener = TcpListener::bind(format!("0.0.0.0:{}", port)).await?;
|
|
|
|
println!("Listening on port {}", port);
|
2019-09-19 09:54:01 +00:00
|
|
|
loop {
|
|
|
|
let (stream, addr) = listener.accept().await?;
|
|
|
|
let connid = id;
|
2019-10-16 13:16:50 +00:00
|
|
|
let spaces = Arc::clone(&spaces);
|
2019-10-20 13:02:51 +00:00
|
|
|
id += 1;
|
2019-09-19 09:54:01 +00:00
|
|
|
tokio::spawn(async move {
|
2019-10-16 15:54:58 +00:00
|
|
|
match peer::Peer::new(connid, stream).await.run(spaces).await {
|
2019-10-24 19:05:39 +00:00
|
|
|
Ok(_) => println!("Connection {} ({:?}) terminated", connid, addr),
|
|
|
|
Err(e) => println!("Connection {} ({:?}) died with {:?}", connid, addr, e),
|
2019-09-19 09:54:01 +00:00
|
|
|
}
|
|
|
|
});
|
2019-09-17 19:28:31 +00:00
|
|
|
}
|
2018-12-09 13:28:01 +00:00
|
|
|
}
|