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
//
// Copyright (C) 2018 Kubos Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Contributed by: William Greer ([email protected]) and Sam Justice ([email protected])
//

//! TOML parser for the `comms-service`. This module parses a `toml` file and returns a
//! struct containing configuration information for a `comms-service`.

use serde_derive::Deserialize;

// Default values for control block configurations.
/// Default message handler starting port
pub const DEFAULT_HANDLER_START: u16 = 13100;
/// Default message handler ending port
pub const DEFAULT_HANDLER_END: u16 = 13149;
/// Default message handler timeout
pub const DEFAULT_TIMEOUT: u64 = 1500;
/// Default ground gateway IP address
pub static DEFAULT_GROUND_IP: &str = "192.168.8.1";
/// Default satellite IP address
pub static DEFAULT_SATELLITE_IP: &str = "192.168.8.2";

/// A struct that holds useful configuration options to use in a `comms-service` implementation.
/// Created by parsing a configuration file in the `toml` file format.
#[derive(Debug, Deserialize)]
pub struct CommsConfig {
    /// Starting port used to define a range of ports that are used in the message handlers
    /// that handle messages received from the ground.
    pub handler_port_min: Option<u16>,
    /// Ending port used to define a range of ports that are used in the message handlers
    /// that handle messages received from the ground.
    pub handler_port_max: Option<u16>,
    /// Optional list of ports used by downlink endpoints that send messages to the ground.
    /// Each port in the list will be used by one downlink endpoint.
    pub downlink_ports: Option<Vec<u16>>,
    /// Timeout for the completion of GraphQL operations within message handlers (in milliseconds).
    pub timeout: Option<u64>,
    /// IP address of the ground gateway.
    pub ground_ip: Option<String>,
    /// Specifies the port to which the ground gateway is bound.
    pub ground_port: Option<u16>,
    /// Satellite's IP address.
    pub satellite_ip: Option<String>,
}

// Implementation of `Default` trait for `CommsConfig`.
impl Default for CommsConfig {
    fn default() -> Self {
        CommsConfig {
            handler_port_min: Some(DEFAULT_HANDLER_START),
            handler_port_max: Some(DEFAULT_HANDLER_END),
            downlink_ports: None,
            ground_port: None,
            timeout: Some(DEFAULT_TIMEOUT),
            ground_ip: Some(DEFAULT_GROUND_IP.to_string()),
            satellite_ip: Some(DEFAULT_SATELLITE_IP.to_string()),
        }
    }
}

impl CommsConfig {
    /// Builds a new configuration for a specific `comms-service`.
    pub fn new(service_config: kubos_system::Config) -> Self {
        let config = service_config
            .get("comms")
            .and_then(|raw| raw.try_into().ok());
        config.unwrap_or_default()
    }
}