reachability_core/
inspect.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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::{LinkState, Proto};
use fuchsia_inspect::Node;
use fuchsia_inspect_contrib::inspect_log;
use fuchsia_inspect_contrib::nodes::BoundedListNode;

// Keep only the 50 most recent events.
static INSPECT_LOG_WINDOW_SIZE: usize = 50;

/// Maintains the Inspect information.
pub(crate) struct InspectInfo {
    _node: Node,
    v4: BoundedListNode,
    v6: BoundedListNode,
}

impl InspectInfo {
    /// Create inspect node `id` rooted at `n`.
    pub(crate) fn new(n: &Node, id: &str, name: &str) -> Self {
        let node = n.create_child(id);
        node.record_string("name", name);
        let mut v4 = BoundedListNode::new(node.create_child("IPv4"), INSPECT_LOG_WINDOW_SIZE);
        inspect_log!(v4, state: "None");
        let mut v6 = BoundedListNode::new(node.create_child("IPv6"), INSPECT_LOG_WINDOW_SIZE);
        inspect_log!(v6, state: "None");

        InspectInfo { _node: node, v4, v6 }
    }
    pub(crate) fn log_link_state(&mut self, proto: Proto, link_state: LinkState) {
        match proto {
            Proto::IPv4 => inspect_log!(self.v4, state: format!("{:?}", link_state)),
            Proto::IPv6 => inspect_log!(self.v6, state: format!("{:?}", link_state)),
        }
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use diagnostics_assertions::assert_data_tree;
    use fuchsia_inspect::Inspector;

    #[test]
    fn test_log_state() {
        let _executor = fuchsia_async::TestExecutor::new();
        let inspector = Inspector::default();
        let mut i = InspectInfo::new(inspector.root(), "id", "myname");
        assert_data_tree!(inspector, root: contains {
            id: {
                name:"myname",
                IPv4:{"0": contains {
                    state: "None"
                }
                },
                IPv6:{"0": contains {
                    state: "None"
                }
                }
            }
        });

        i.log_link_state(Proto::IPv4, LinkState::Internet);
        assert_data_tree!(inspector, root: contains {
            id: {
                name:"myname",
                IPv4:{"0": contains {
                    state: "None"
                },
                "1": contains {
                    state: "Internet"
                }
                },
                IPv6:{"0": contains {
                    state: "None"
                }
                }
            }
        });
        i.log_link_state(Proto::IPv4, LinkState::Gateway);
        i.log_link_state(Proto::IPv6, LinkState::Local);
        assert_data_tree!(inspector, root: contains {
            id: {
                name:"myname",
                IPv4:{"0": contains {
                    state: "None"
                },
                "1": contains {
                    state: "Internet"
                },
                "2": contains {
                    state: "Gateway"
                }
                },
                IPv6:{"0": contains {
                    state: "None"
                },
                "1": contains {
                    state: "Local"
                }
                }
            }
        });
    }
}