inspect_testing/
deprecated_fidl_server.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::table::*;
6use anyhow::Error;
7use fidl_fuchsia_inspect_deprecated::{self as fidl_inspect, InspectRequest, InspectRequestStream};
8use fuchsia_async as fasync;
9use futures::{TryFutureExt, TryStreamExt};
10
11pub fn spawn_inspect_server(mut stream: InspectRequestStream, node: NodeObject) {
12    fasync::Task::spawn(
13        async move {
14            while let Some(req) = stream.try_next().await? {
15                match req {
16                    InspectRequest::ReadData { responder } => {
17                        let metrics = get_metrics(&node);
18                        let properties = get_properties(&node);
19                        let object = fidl_inspect::Object {
20                            name: node.get_node_name(),
21                            properties,
22                            metrics,
23                        };
24                        responder.send(&object)?;
25                    }
26                    InspectRequest::ListChildren { responder } => {
27                        let names = get_children_names(&node);
28                        responder.send(&names)?;
29                    }
30                    InspectRequest::OpenChild { child_name, child_channel, responder } => {
31                        let stream = child_channel.into_stream();
32                        let ok = match &node {
33                            NodeObject::Root(table) => {
34                                spawn_inspect_server(stream, NodeObject::Table(table.clone()));
35                                true
36                            }
37                            NodeObject::Row(row) => {
38                                match row.cells.iter().find(|c| c.node_name == child_name) {
39                                    Some(child) => {
40                                        spawn_inspect_server(
41                                            stream,
42                                            NodeObject::Cell(child.clone()),
43                                        );
44                                        true
45                                    }
46                                    None => false,
47                                }
48                            }
49                            NodeObject::Table(table) => {
50                                match table.rows.iter().find(|c| c.node_name == child_name) {
51                                    Some(child) => {
52                                        spawn_inspect_server(
53                                            stream,
54                                            NodeObject::Row(child.clone()),
55                                        );
56                                        true
57                                    }
58                                    None => false,
59                                }
60                            }
61                            _ => false,
62                        };
63                        responder.send(ok)?;
64                    }
65                }
66            }
67            Ok(())
68        }
69        .unwrap_or_else(|e: Error| eprintln!("error running inspect server: {:?}", e)),
70    )
71    .detach();
72}
73
74fn get_children_names(node: &NodeObject) -> Vec<String> {
75    match node {
76        NodeObject::Table(table) => table.rows.iter().map(|row| row.node_name.clone()).collect(),
77        NodeObject::Row(row) => row.cells.iter().map(|cell| cell.node_name.clone()).collect(),
78        NodeObject::Root(table) => vec![table.node_name.clone()],
79        _ => vec![],
80    }
81}
82
83fn get_metrics(node: &NodeObject) -> Vec<fidl_inspect::Metric> {
84    match node {
85        NodeObject::Cell(cell) => vec![
86            fidl_inspect::Metric {
87                key: "double_value".to_string(),
88                value: fidl_inspect::MetricValue::DoubleValue(cell.double_value),
89            },
90            fidl_inspect::Metric {
91                key: "value".to_string(),
92                value: fidl_inspect::MetricValue::IntValue(cell.value),
93            },
94        ],
95        _ => vec![],
96    }
97}
98
99fn get_properties(node: &NodeObject) -> Vec<fidl_inspect::Property> {
100    match node {
101        NodeObject::Cell(cell) => vec![fidl_inspect::Property {
102            key: "name".to_string(),
103            value: fidl_inspect::PropertyValue::Str(cell.name.clone()),
104        }],
105        NodeObject::Table(table) => vec![
106            fidl_inspect::Property {
107                key: "binary_data".to_string(),
108                value: fidl_inspect::PropertyValue::Bytes(table.binary_data.clone()),
109            },
110            fidl_inspect::Property {
111                key: "object_name".to_string(),
112                value: fidl_inspect::PropertyValue::Str(table.object_name.clone()),
113            },
114        ],
115        _ => vec![],
116    }
117}