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
// 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 {
    anyhow::Error,
    fidl_test_policy::{
        RestrictedRequest, RestrictedRequestStream, UnrestrictedRequest, UnrestrictedRequestStream,
    },
    fuchsia_async as fasync,
    fuchsia_component::server::ServiceFs,
    futures::prelude::*,
};

/// Trivial service host that just launches a restricted and unrestricted
/// protocol that both return a trivial string.
#[fasync::run_singlethreaded]
async fn main() {
    let mut fs = ServiceFs::new_local();
    fs.dir("svc").add_fidl_service(move |stream| {
        fasync::Task::local(
            run_restricted_service(stream)
                .unwrap_or_else(|e| panic!("error running service: {:?}", e)),
        )
        .detach();
    });
    fs.dir("svc").add_fidl_service(move |stream| {
        fasync::Task::local(
            run_unrestricted_service(stream)
                .unwrap_or_else(|e| panic!("error running service: {:?}", e)),
        )
        .detach();
    });

    fs.take_and_serve_directory_handle().expect("failed to serve outgoing dir");
    fs.collect::<()>().await;
}

/// Trivial service that just returns the value "restricted"
async fn run_restricted_service(mut stream: RestrictedRequestStream) -> Result<(), Error> {
    while let Some(request) = stream.try_next().await? {
        match request {
            RestrictedRequest::GetRestricted { responder } => {
                responder.send("restricted")?;
            }
        }
    }
    Ok(())
}

/// Trivial service that just returns the value "unrestricted"
async fn run_unrestricted_service(mut stream: UnrestrictedRequestStream) -> Result<(), Error> {
    while let Some(request) = stream.try_next().await? {
        match request {
            UnrestrictedRequest::GetUnrestricted { responder } => {
                responder.send("unrestricted")?;
            }
        }
    }
    Ok(())
}