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
// Copyright 2018 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;
use futures::StreamExt;
use serde_json::Value;
use std::sync::Arc;
use tracing::*;

// Sl4f related inclusions
use crate::common_utils::error::Sl4fError;
use crate::server::constants::CONCURRENT_REQ_LIMIT;
use crate::server::sl4f::Sl4f;
use crate::server::sl4f_types::{AsyncCommandRequest, AsyncRequest, AsyncResponse, MethodId};

pub async fn run_fidl_loop(sl4f: Arc<Sl4f>, receiver: async_channel::Receiver<AsyncRequest>) {
    let handler = move |request| handle_request(Arc::clone(&sl4f), request);

    receiver.for_each_concurrent(CONCURRENT_REQ_LIMIT, handler).await
}

async fn handle_request(sl4f: Arc<Sl4f>, request: AsyncRequest) {
    match request {
        AsyncRequest::Cleanup(done) => {
            // Cleanup all resources associated with sl4f and notify the blocked request when done.
            sl4f.cleanup().await;
            sl4f.print().await;
            done.send(()).unwrap();
        }
        AsyncRequest::Command(AsyncCommandRequest { tx, method_id, params }) => {
            info!(tag = "run_fidl_loop", ?tx, ?method_id, ?params, "Received synchronous request");
            match method_to_fidl(method_id, params, Arc::clone(&sl4f)).await {
                Ok(response) => {
                    let async_response = AsyncResponse::new(Ok(response));

                    // Ignore any tx sending errors since there is not a recovery path.  The
                    // connection to the test server may be broken.
                    let _ = tx.send(async_response);
                }
                Err(err) => {
                    error!(?err, "Error returned from calling method_to_fidl");
                    let async_response = AsyncResponse::new(Err(err));

                    // Ignore any tx sending errors since there is not a recovery path.  The
                    // connection to the test server may be broken.
                    let _ = tx.send(async_response);
                }
            };
        }
    }
}

async fn method_to_fidl(method_id: MethodId, args: Value, sl4f: Arc<Sl4f>) -> Result<Value, Error> {
    if let Some(facade) = sl4f.get_facade(&method_id.facade) {
        facade.handle_request(method_id.method, args).await
    } else if sl4f.has_proxy_facade(&method_id.facade) {
        sl4f.handle_proxy_request(method_id.facade, method_id.method, args).await
    } else {
        Err(Sl4fError::new(&format!("Invalid FIDL method type {:?}", &method_id.facade)).into())
    }
}