summaryrefslogtreecommitdiff
path: root/src/bin/server.rs
blob: e0428550fd8f4cf9484a8233349198cf7eda4ba1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::thread::sleep;
use std::time::Duration;
use std::net::TcpListener;

extern crate space;
use space::mass::Mass;
use space::astroid::Astroid;
use space::connection::Connection;


fn populate() -> Vec<Box<Mass>> {
    let mut masses : Vec<Box<Mass>> = Vec::new();

    masses.push(Box::new(Astroid::new("cZfAJ", (10, -5, 4))));

    masses
}

fn main() {
    let listener = TcpListener::bind("localhost:6000").unwrap();
    listener.set_nonblocking(true).unwrap();

    let mut masses = populate();

    let mut connections = Vec::new();
    for stream in listener.incoming() {
        match stream {
            Ok(stream) => connections.push(Connection::new(stream, &mut masses)),
            _ => {
                for i in 0..connections.len() {
                    connections[i].process(&mut masses);
                }
                connections.retain(|connection| connection.open);

                sleep(Duration::from_millis(100));
            }
        }
    }
}