server_node/
config.rs

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
121
122
123
124
125
126
127
128
129
130
//! # Server Configuration and TOML File Loading

//! # DHCP Cluster - Server Implementation
//!
//! This crate contains a distributed DHCP server implementation, with a custom protocol between nodes.
//!
//! For the protocol definition, look into the [`peer::message`] module.
//!
//! The server architecture comprises of threads, which use blocking operations to communicate over [`std::net::TcpStream`]s.
//! There are two threads per active peer, one for receiving messages and one for sending messages.
//! There is also a server logic thread, handling bookkeeping for the peer- and client events.
//!
//! For the communication thread implementation, look into the [`peer`] module.
//!
//! This module contains the server configuration structure, along with facilities for
//! loading configuration files from the filesystem.
//!
//! Jump to [`Config::load_toml_file`] for configuration file loading.

use crate::{dhcp::Ipv4Range, server::peer};
use serde::Deserialize;
use std::{
    net::{Ipv4Addr, SocketAddr},
    num::NonZero,
    thread,
    time::Duration,
};
use toml_config::TomlConfig;

#[derive(Deserialize, Debug)]
pub struct DhcpSection {
    net: Ipv4Addr,
    prefix_length: u32,
    lease_time_seconds: u64,
}

#[derive(Deserialize, Debug)]
pub struct ServerSection {
    listen_cluster: SocketAddr,
    listen_dhcp: SocketAddr,
    client_timeout_seconds: Option<u64>,
    thread_count: Option<usize>,
}

#[derive(Deserialize, Debug)]
pub struct ClusterSection {
    id: peer::Id,
    heartbeat_timeout_millis: u64,
    connect_timeout_seconds: Option<u64>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct Peer {
    pub id: peer::Id,
    pub host: String,
}

#[derive(Deserialize, Debug)]
pub struct File {
    dhcp: DhcpSection,
    server: ServerSection,
    cluster: ClusterSection,
    peers: Vec<Peer>,
}

/// Server configuration
///
/// Use [`Config::load_toml_file`] to initialize.
#[derive(Debug)]
pub struct Config {
    pub dhcp_pool: Ipv4Range,
    pub prefix_length: u32,
    pub lease_time: Duration,

    pub listen_cluster: SocketAddr,
    pub listen_dhcp: SocketAddr,
    pub client_timeout: Duration,
    pub thread_count: NonZero<usize>,

    pub id: peer::Id,
    pub heartbeat_timeout: Duration,
    pub connect_timeout: Duration,
    pub peers: Vec<Peer>,
}

impl From<File> for Config {
    // This implementation is a no-op for now, but down the line it's possible
    // that our server configuration struct diverges from the
    // configuration file contents
    fn from(file: File) -> Self {
        let dhcp = file.dhcp;
        let server = file.server;
        let cluster = file.cluster;

        Self {
            // DHCP
            dhcp_pool: Ipv4Range::from_cidr(dhcp.net, dhcp.prefix_length),
            prefix_length: dhcp.prefix_length,
            lease_time: Duration::from_secs(dhcp.lease_time_seconds),

            // Server
            listen_cluster: server.listen_cluster,
            listen_dhcp: server.listen_dhcp,
            client_timeout: server
                .client_timeout_seconds
                .and_then(|sec| (sec != 0).then_some(Duration::from_secs(sec)))
                .unwrap_or(Duration::from_secs(10)),
            thread_count: server
                .thread_count
                .and_then(NonZero::new)
                .unwrap_or_else(|| {
                    #[allow(clippy::unwrap_used, reason = "NonZero constructed from literal")]
                    thread::available_parallelism()
                        .map(|n| NonZero::new(usize::from(n) * 4).unwrap())
                        .unwrap_or(NonZero::new(8).unwrap())
                }),

            // Cluster
            id: cluster.id,
            heartbeat_timeout: Duration::from_millis(cluster.heartbeat_timeout_millis),
            connect_timeout: cluster
                .connect_timeout_seconds
                .and_then(|sec| (sec != 0).then_some(Duration::from_secs(sec)))
                .unwrap_or(Duration::from_secs(10)),
            peers: file.peers,
        }
    }
}

impl TomlConfig<File> for Config {}