archivist_lib/events/sources/
event_source.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2022 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::events::router::{Dispatcher, EventProducer};
use anyhow::Error;
use fcomponent::EventStreamProxy;
use fidl_fuchsia_component as fcomponent;
use fuchsia_component::client::connect_to_protocol_at_path;
use tracing::warn;

pub struct EventSource {
    dispatcher: Dispatcher,
    event_stream: EventStreamProxy,
}

impl EventSource {
    pub async fn new(event_stream_path: &str) -> Result<Self, Error> {
        let event_stream =
            connect_to_protocol_at_path::<fcomponent::EventStreamMarker>(event_stream_path)?;
        let _ = event_stream.wait_for_ready().await;
        Ok(Self { event_stream, dispatcher: Dispatcher::default() })
    }

    #[cfg(test)]
    fn new_for_test(event_stream: EventStreamProxy) -> Self {
        // Connect to /events/event_stream which contains our newer FIDL protocol
        Self { event_stream, dispatcher: Dispatcher::default() }
    }

    pub async fn spawn(mut self) -> Result<(), Error> {
        while let Ok(events) = self.event_stream.get_next().await {
            for event in events {
                match event.try_into() {
                    Ok(event) => {
                        if let Err(err) = self.dispatcher.emit(event) {
                            if err.is_disconnected() {
                                break;
                            }
                        }
                    }
                    Err(err) => {
                        warn!(?err, "Failed to interpret event");
                    }
                }
            }
        }
        Ok(())
    }
}

impl EventProducer for EventSource {
    fn set_dispatcher(&mut self, dispatcher: Dispatcher) {
        self.dispatcher = dispatcher;
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;
    use crate::events::types::*;
    use crate::identity::ComponentIdentity;
    use fuchsia_async as fasync;
    use futures::channel::mpsc::UnboundedSender;
    use futures::StreamExt;
    use moniker::ExtendedMoniker;
    use std::collections::BTreeSet;

    #[fuchsia::test]
    async fn event_stream() {
        let events = BTreeSet::from([EventType::InspectSinkRequested, EventType::LogSinkRequested]);
        let (mut event_stream, dispatcher) = Dispatcher::new_for_test(events);
        let (stream_server, _server_task, sender) = spawn_fake_event_stream();
        let mut source = EventSource::new_for_test(stream_server);
        source.set_dispatcher(dispatcher);
        let _task = fasync::Task::spawn(async move { source.spawn().await });

        // Send a `InspectSinkRequested` event for diagnostics.
        sender
            .unbounded_send(fcomponent::Event {
                header: Some(fcomponent::EventHeader {
                    event_type: Some(fcomponent::EventType::CapabilityRequested),
                    moniker: Some("./foo/bar".to_string()),
                    component_url: Some("fuchsia-pkg://fuchsia.com/foo#meta/bar.cm".to_string()),
                    timestamp: Some(zx::BootInstant::get()),
                    ..Default::default()
                }),
                payload: Some(fcomponent::EventPayload::CapabilityRequested(
                    fcomponent::CapabilityRequestedPayload {
                        name: Some("fuchsia.inspect.InspectSink".to_string()),
                        capability: Some(zx::Channel::create().0),
                        ..Default::default()
                    },
                )),
                ..Default::default()
            })
            .expect("send logsink requested event ok");

        // Send a `LogSinkRequested` event.
        sender
            .unbounded_send(fcomponent::Event {
                header: Some(fcomponent::EventHeader {
                    event_type: Some(fcomponent::EventType::CapabilityRequested),
                    moniker: Some("./foo/bar".to_string()),
                    component_url: Some("fuchsia-pkg://fuchsia.com/foo#meta/bar.cm".to_string()),
                    timestamp: Some(zx::BootInstant::get()),
                    ..Default::default()
                }),
                payload: Some(fcomponent::EventPayload::CapabilityRequested(
                    fcomponent::CapabilityRequestedPayload {
                        name: Some("fuchsia.logger.LogSink".to_string()),
                        capability: Some(zx::Channel::create().0),
                        ..Default::default()
                    },
                )),
                ..Default::default()
            })
            .expect("send logsink requested event ok");

        let expected_component_id = ExtendedMoniker::parse_str("./foo/bar").unwrap();
        let expected_identity = ComponentIdentity::new(
            expected_component_id,
            "fuchsia-pkg://fuchsia.com/foo#meta/bar.cm",
        );

        // Assert the third received event was a InsepctSinkRequested event.
        let event = event_stream.next().await.unwrap();
        match event.payload {
            EventPayload::InspectSinkRequested(InspectSinkRequestedPayload {
                component,
                request_stream: _,
            }) => {
                assert_eq!(*component, expected_identity)
            }
            other => panic!("unexpected event payload: {other:?}"),
        }

        // Assert the last received event was a LogSinkRequested event.
        let event = event_stream.next().await.unwrap();
        match event.payload {
            EventPayload::LogSinkRequested(LogSinkRequestedPayload { component, .. }) => {
                assert_eq!(*component, expected_identity)
            }
            other => panic!("unexpected event payload: {other:?}"),
        }
    }

    fn spawn_fake_event_stream(
    ) -> (fcomponent::EventStreamProxy, fasync::Task<()>, UnboundedSender<fcomponent::Event>) {
        let (sender, mut receiver) = futures::channel::mpsc::unbounded::<fcomponent::Event>();
        let (proxy, server_end) = fidl::endpoints::create_proxy::<fcomponent::EventStreamMarker>();
        let task = fasync::Task::spawn(async move {
            let mut request_stream = server_end.into_stream();
            loop {
                if let Some(Ok(request)) = request_stream.next().await {
                    match request {
                        fcomponent::EventStreamRequest::GetNext { responder } => {
                            if let Some(event) = receiver.next().await {
                                responder.send(vec![event]).unwrap();
                            } else {
                                break;
                            }
                        }
                        fcomponent::EventStreamRequest::WaitForReady { responder } => {
                            responder.send().unwrap();
                        }
                    }
                }
            }
        });
        (proxy, task, sender)
    }
}