settings/handler/
setting_handler_factory_impl.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// 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::base::SettingType;
use crate::handler::base::{
    Context, Environment, GenerateHandler, SettingHandlerFactory, SettingHandlerFactoryError,
};
use crate::handler::setting_handler::{Command, Payload, State};
use crate::message::base::{Audience, MessageEvent, MessengerType};
use crate::service;
use crate::service::message::{Delegate, Signature};
use crate::service_context::ServiceContext;
use async_trait::async_trait;
use futures::StreamExt;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
use std::sync::atomic::{AtomicU64, Ordering};

/// SettingHandlerFactoryImpl houses registered closures for generating setting
/// handlers.
pub(crate) struct SettingHandlerFactoryImpl {
    environment: Environment,
    generators: HashMap<SettingType, GenerateHandler>,

    /// Atomic counter used to generate new IDs, which uniquely identify a context.
    context_id_counter: Rc<AtomicU64>,
}

#[async_trait(?Send)]
impl SettingHandlerFactory for SettingHandlerFactoryImpl {
    async fn generate(
        &mut self,
        setting_type: SettingType,
        delegate: Delegate,
        notifier_signature: Signature,
    ) -> Result<Signature, SettingHandlerFactoryError> {
        if !self.environment.settings.contains(&setting_type) {
            return Err(SettingHandlerFactoryError::SettingNotFound(setting_type));
        }

        let (messenger, receptor) = delegate
            .create(MessengerType::Unbound)
            .await
            .map_err(|_| SettingHandlerFactoryError::HandlerMessengerError)?;
        let signature = receptor.get_signature();

        let generate_function = self
            .generators
            .get(&setting_type)
            .ok_or(SettingHandlerFactoryError::GeneratorNotFound(setting_type))?;

        (generate_function)(Context::new(
            setting_type,
            messenger,
            receptor,
            notifier_signature,
            self.environment.clone(),
            self.context_id_counter.fetch_add(1, Ordering::Relaxed),
        ))
        .await
        .map_err(|e| {
            SettingHandlerFactoryError::HandlerStartupError(
                setting_type,
                format!("Failed to generate setting handler: {e:?}").into(),
            )
        })?;

        let (controller_messenger, _) = delegate
            .create(MessengerType::Unbound)
            .await
            .map_err(|_| SettingHandlerFactoryError::ControllerMessengerError)?;

        // At this point, we know the controller was constructed successfully.
        // Tell the controller to run the Startup phase to initialize its state.
        let mut controller_receptor = controller_messenger.message(
            Payload::Command(Command::ChangeState(State::Startup)).into(),
            Audience::Messenger(signature),
        );

        // Wait for the startup phase to be over before continuing.
        while let Some(message_event) = controller_receptor.next().await {
            match message_event {
                MessageEvent::Status(_) => {} // no-op
                MessageEvent::Message(service::Payload::Controller(Payload::Result(result)), _) => {
                    // Startup phase is complete. If it had no errors the proxy can assume it
                    // has an active controller with create() and startup() already run on it
                    // before handling its request.
                    return result.map(|_| signature).map_err(|e| {
                        SettingHandlerFactoryError::HandlerStartupError(
                            setting_type,
                            format!("Got bad startup response: {e:?}").into(),
                        )
                    });
                }
                e => {
                    return Err(SettingHandlerFactoryError::HandlerStartupError(
                        setting_type,
                        format!("Unexpected startup response: {e:?}").into(),
                    ));
                }
            }
        }

        panic!("Did not get any responses from {setting_type:?} controller startup");
    }
}

impl SettingHandlerFactoryImpl {
    pub(crate) fn new(
        settings: HashSet<SettingType>,
        service_context: Rc<ServiceContext>,
        context_id_counter: Rc<AtomicU64>,
    ) -> SettingHandlerFactoryImpl {
        SettingHandlerFactoryImpl {
            environment: Environment::new(settings, service_context),
            generators: HashMap::new(),
            context_id_counter,
        }
    }

    pub(crate) fn register(
        &mut self,
        setting_type: SettingType,
        generate_function: GenerateHandler,
    ) {
        // Ignores any previously registered generator.
        let _ = self.generators.insert(setting_type, generate_function);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::handler::base::Request;
    use crate::handler::setting_handler::controller::{Create, Handle};
    use crate::handler::setting_handler::{
        BoxedController, ClientImpl, ControllerError, ControllerStateResult, SettingHandlerResult,
    };
    use fuchsia_async as fasync;
    use futures::future::FutureExt;
    use futures::select;

    /// Test controller used to test startup waiting behavior in SettingHandlerFactoryImpl.
    struct TestController {
        client: Rc<ClientImpl>,
    }

    #[async_trait(?Send)]
    impl Create for TestController {
        async fn create(client: Rc<ClientImpl>) -> Result<Self, ControllerError> {
            Ok(Self { client })
        }
    }

    #[async_trait(?Send)]
    impl Handle for TestController {
        // Not relevant.
        async fn handle(&self, _request: Request) -> Option<SettingHandlerResult> {
            None
        }

        // When we see a startup message, send a signal to signify we're done.
        async fn change_state(&mut self, state: State) -> Option<ControllerStateResult> {
            let _ = self.client.emit_state_event(state);
            match state {
                State::Startup => Some(Ok(())),
                _ => None,
            }
        }
    }

    #[fasync::run_until_stalled(test)]
    async fn ensure_startup_is_awaited() {
        let delegate = service::MessageHub::create_hub();
        let mut factory_impl = SettingHandlerFactoryImpl::new(
            [SettingType::Unknown].into(),
            Rc::new(ServiceContext::new(None, Some(delegate.clone()))),
            Rc::new(AtomicU64::new(0)),
        );

        // Register generation of controller with factory_impl.
        let generate_handler: GenerateHandler = Box::new(move |context| {
            Box::pin(ClientImpl::create(
                context,
                Box::new(move |proxy| {
                    Box::pin(async move {
                        Ok(Box::new(TestController::create(proxy).await.unwrap())
                            as BoxedController)
                    })
                }),
            ))
        });
        factory_impl.register(SettingType::Unknown, generate_handler);

        // Create a broker that only listens to replies.
        let (_, broker_receptor) =
            delegate.create_sink().await.expect("could not create event sink receptor");

        let (_, receptor) =
            delegate.create(MessengerType::Unbound).await.expect("messenger should be created");
        // Generate the controller, but don't await it yet so we can time it with the response
        // from the broker.
        let mut generate_future = factory_impl
            .generate(SettingType::Unknown, delegate, receptor.get_signature())
            .into_stream()
            .fuse();
        let mut broker_receptor = broker_receptor.fuse();

        // We need to validate that the generate_done always completes after the startup_done
        // signal. If this ever fails, it implies that the generator does not properly wait for
        // startup to complete.
        let mut generate_done = None;
        let mut startup_done = None;
        let mut idx: u8 = 0;
        while generate_done.is_none() || startup_done.is_none() {
            select! {
                maybe = broker_receptor.next() => {
                    let _ = maybe.expect("should have gotten a reply");
                    startup_done = Some(idx);
                    idx += 1;
                }
                result = generate_future.select_next_some() => {
                    let _ = result.expect("should have received a signature");
                    generate_done = Some(idx);
                    idx += 1;
                }

                complete => break,
            }
        }

        // Validate that the id for generate is larger than the one for startup,
        // implying that generate finished after startup was done.
        assert!(generate_done.unwrap() > startup_done.unwrap());
    }
}