inspect_testing/
deprecated_fidl_server.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
// Copyright 2019 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::table::*;
use anyhow::Error;
use fidl_fuchsia_inspect_deprecated::{self as fidl_inspect, InspectRequest, InspectRequestStream};
use fuchsia_async as fasync;
use futures::{TryFutureExt, TryStreamExt};

pub fn spawn_inspect_server(mut stream: InspectRequestStream, node: NodeObject) {
    fasync::Task::spawn(
        async move {
            while let Some(req) = stream.try_next().await? {
                match req {
                    InspectRequest::ReadData { responder } => {
                        let metrics = get_metrics(&node);
                        let properties = get_properties(&node);
                        let object = fidl_inspect::Object {
                            name: node.get_node_name(),
                            properties,
                            metrics,
                        };
                        responder.send(&object)?;
                    }
                    InspectRequest::ListChildren { responder } => {
                        let names = get_children_names(&node);
                        responder.send(&names)?;
                    }
                    InspectRequest::OpenChild { child_name, child_channel, responder } => {
                        let stream = child_channel.into_stream();
                        let ok = match &node {
                            NodeObject::Root(table) => {
                                spawn_inspect_server(stream, NodeObject::Table(table.clone()));
                                true
                            }
                            NodeObject::Row(row) => {
                                match row.cells.iter().find(|c| c.node_name == child_name) {
                                    Some(child) => {
                                        spawn_inspect_server(
                                            stream,
                                            NodeObject::Cell(child.clone()),
                                        );
                                        true
                                    }
                                    None => false,
                                }
                            }
                            NodeObject::Table(table) => {
                                match table.rows.iter().find(|c| c.node_name == child_name) {
                                    Some(child) => {
                                        spawn_inspect_server(
                                            stream,
                                            NodeObject::Row(child.clone()),
                                        );
                                        true
                                    }
                                    None => false,
                                }
                            }
                            _ => false,
                        };
                        responder.send(ok)?;
                    }
                }
            }
            Ok(())
        }
        .unwrap_or_else(|e: Error| eprintln!("error running inspect server: {:?}", e)),
    )
    .detach();
}

fn get_children_names(node: &NodeObject) -> Vec<String> {
    match node {
        NodeObject::Table(table) => table.rows.iter().map(|row| row.node_name.clone()).collect(),
        NodeObject::Row(row) => row.cells.iter().map(|cell| cell.node_name.clone()).collect(),
        NodeObject::Root(table) => vec![table.node_name.clone()],
        _ => vec![],
    }
}

fn get_metrics(node: &NodeObject) -> Vec<fidl_inspect::Metric> {
    match node {
        NodeObject::Cell(cell) => vec![
            fidl_inspect::Metric {
                key: "double_value".to_string(),
                value: fidl_inspect::MetricValue::DoubleValue(cell.double_value),
            },
            fidl_inspect::Metric {
                key: "value".to_string(),
                value: fidl_inspect::MetricValue::IntValue(cell.value),
            },
        ],
        _ => vec![],
    }
}

fn get_properties(node: &NodeObject) -> Vec<fidl_inspect::Property> {
    match node {
        NodeObject::Cell(cell) => vec![fidl_inspect::Property {
            key: "name".to_string(),
            value: fidl_inspect::PropertyValue::Str(cell.name.clone()),
        }],
        NodeObject::Table(table) => vec![
            fidl_inspect::Property {
                key: "binary_data".to_string(),
                value: fidl_inspect::PropertyValue::Bytes(table.binary_data.clone()),
            },
            fidl_inspect::Property {
                key: "object_name".to_string(),
                value: fidl_inspect::PropertyValue::Str(table.object_name.clone()),
            },
        ],
        _ => vec![],
    }
}