settings/
ingress.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
// Copyright 2021 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.

//! This mod defines the building blocks for receiving inbound communication from external
//! interfaces, such as FIDL. It also includes common implementations for working with
//! `Jobs` for incoming requests.

/// The [fidl] mod enables defining components that provide inbound communication over FIDL.
pub mod fidl;

pub(crate) mod request;
pub(crate) mod watch;

/// [Scoped] is a simple wrapper that can be used to overcome issues with using types outside this
/// crate in traits that are defined outside as well. For example, the [From] trait requires that
/// genericized type be defined within the crate. The extract method can be used to retrieve the
/// contained data.
pub(crate) struct Scoped<T>(pub T);

pub(crate) mod registration {
    use super::fidl;
    use crate::base::Dependency;
    use crate::job::source::Seeder;
    use fuchsia_component::server::{ServiceFsDir, ServiceObjLocal};
    use std::collections::HashSet;

    /// [Registrar] defines the medium over which communication occurs. Each entry includes a
    /// closure that takes a specific set of inputs necessary to bring up that particular
    /// communication.
    pub enum Registrar {
        Fidl(fidl::Register),
        /// This value is reserved for testing purposes.
        #[cfg(test)]
        Test(Box<dyn FnOnce()>),
        #[cfg(test)]
        TestWithSeeder(Box<dyn FnOnce(&Seeder)>),
    }

    impl Registrar {
        /// Brings up the communication by supplying the subset of needed inputs to the particular
        /// [Registrar].
        pub fn register(
            self,
            job_seeder: &Seeder,
            service_dir: &mut ServiceFsDir<'_, ServiceObjLocal<'_, ()>>,
        ) {
            match self {
                Registrar::Fidl(register_fn) => {
                    register_fn(job_seeder, service_dir);
                }
                #[cfg(test)]
                Registrar::Test(register_fn) => {
                    register_fn();
                }
                #[cfg(test)]
                Registrar::TestWithSeeder(register_fn) => {
                    register_fn(job_seeder);
                }
            }
        }
    }

    /// [Registrant] brings up an inbound interface in the service.
    pub struct Registrant {
        /// The name of the interface being registered.
        interface: String,
        /// The [Registrar] responsible for bringing up the interface.
        registrar: Registrar,
        /// A list of [Dependencies](Dependency) the registrant relies on being present in order to
        /// function.
        dependencies: HashSet<Dependency>,
    }

    impl Registrant {
        pub(crate) fn new(
            interface: String,
            registrar: Registrar,
            dependencies: HashSet<Dependency>,
        ) -> Registrant {
            Registrant { interface, registrar, dependencies }
        }

        pub(crate) fn get_dependencies(&self) -> &HashSet<Dependency> {
            &self.dependencies
        }

        pub(crate) fn get_interface(&self) -> &String {
            &self.interface
        }

        pub(crate) fn register(
            self,
            job_seeder: &Seeder,
            service_dir: &mut ServiceFsDir<'_, ServiceObjLocal<'_, ()>>,
        ) {
            self.registrar.register(job_seeder, service_dir);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::registration::{Registrant, Registrar};
    use crate::base::{Dependency, Entity, SettingType};
    use crate::job::source::Seeder;
    use crate::message::base::MessengerType;
    use crate::service;
    use assert_matches::assert_matches;
    use fuchsia_component::server::ServiceFs;

    #[fuchsia::test(allow_stalls = false)]
    async fn test_registration() {
        let (tx, rx) = futures::channel::oneshot::channel::<()>();
        let dependency = Dependency::Entity(Entity::Handler(SettingType::Unknown));
        let registrant = Registrant::new(
            "Registrar::Test".to_string(),
            Registrar::Test(Box::new(move || {
                assert!(tx.send(()).is_ok());
            })),
            [dependency].into(),
        );

        let mut fs = ServiceFs::new_local();

        // Verify added dependency.
        assert!(registrant.get_dependencies().contains(&dependency));

        let delegate = service::MessageHub::create_hub();
        let job_manager_signature = delegate
            .create(MessengerType::Unbound)
            .await
            .expect("messenger should be created")
            .0
            .get_signature();
        let job_seeder = Seeder::new(&delegate, job_manager_signature).await;

        // Register and consume Registrant.
        registrant.register(&job_seeder, &mut fs.root_dir());

        // Verify registration occurred.
        assert_matches!(rx.await, Ok(()));
    }
}