summaryrefslogtreecommitdiff
path: root/src/bin/server.rs
blob: 9ea1f0dfac251f043d4d29ac915f1f04938e18f7 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
extern crate space;

use clap::{App, SubCommand};
use diesel::pg::PgConnection;
use diesel::prelude::*;
use std::collections::HashMap;
use std::io::Write;
use std::net::TcpListener;
use std::thread::{sleep, spawn};
use std::time::{Duration, Instant, SystemTime};

use space::constants;
use space::mass::{Mass, MassEntry};
use space::math::{get_db_url, rand_name};
use space::schema::masses::dsl::masses as db_masses;
use space::schema::masses::dsl::name as name_column;
use space::server_connection::ServerConnection;

fn populate() -> HashMap<String, Mass> {
    let mut masses: HashMap<String, Mass> = HashMap::new();

    for _ in 0..constants::ASTROID_COUNT {
        masses.insert(rand_name(), Mass::new_astroid());
    }

    masses
}

fn backup(masses: HashMap<String, Mass>) {
    let connection = PgConnection::establish(&get_db_url()).expect("Cannot connect");
    let timestamp = SystemTime::now();
    for (name, mass) in masses {
        let mass_entry = mass.to_mass_entry(name.to_string(), timestamp);
        diesel::insert_into(db_masses)
            .values(&mass_entry)
            .on_conflict(name_column)
            .do_update()
            .set(&mass_entry)
            .execute(&connection)
            .expect("Cannot backup");
    }
}

fn restore() -> HashMap<String, Mass> {
    let connection = PgConnection::establish(&get_db_url()).expect("Cannot connect");
    db_masses
        .load::<MassEntry>(&connection)
        .expect("Cannot query, are you sure you can restore?")
        .iter()
        .map(|mass_entry| mass_entry.to_mass())
        .collect()
}

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

    let matches = App::new("space server")
        .subcommand(SubCommand::with_name("--restore"))
        .get_matches();

    let mut masses = match matches.subcommand_name() {
        Some("--restore") => restore(),
        _ => populate(),
    };

    let mut backup_countdown = constants::BACKUP_COUNTDOWN;
    let mut connections: Vec<ServerConnection> = Vec::new();
    for stream in listener.incoming() {
        match stream {
            Ok(stream) => {
                let new_connection = ServerConnection::new(stream, &mut masses);
                let exists = connections.iter().position(|connection| {
                    connection.name == new_connection.name
                        && connection.module_type == new_connection.module_type
                });
                if let Some(index) = exists {
                    connections.remove(index);
                }
                connections.push(new_connection);
            }
            _ => {
                let timer = Instant::now();

                for connection in &mut connections {
                    if connection.open {
                        let mut ship = masses.remove(&connection.name).unwrap();

                        let send = ship.get_client_data(connection.module_type.clone(), &masses);
                        connection.open = connection.stream.write(send.as_bytes()).is_ok();

                        let recv = connection.receive();
                        ship.give_received_data(connection.module_type.clone(), recv);

                        masses.insert(connection.name.clone(), ship);
                    }
                }

                for key in masses.clone().keys() {
                    let mut mass = masses.remove(key).unwrap();
                    mass.process(&mut masses);
                    masses.insert(key.to_string(), mass);
                }

                if backup_countdown == 0 {
                    let masses_clone = masses.clone();
                    spawn(move || backup(masses_clone));
                    backup_countdown = constants::BACKUP_COUNTDOWN;
                }

                if timer.elapsed().as_millis() < constants::LOOP_DURATION_MS.into() {
                    sleep(Duration::from_millis(
                        constants::LOOP_DURATION_MS - timer.elapsed().as_millis() as u64,
                    ));
                }
                backup_countdown -= 1;
            }
        }
    }
}