sl4f_lib/server/
sl4f_executor.rs1use anyhow::Error;
6use futures::StreamExt;
7use log::*;
8use serde_json::Value;
9use std::sync::Arc;
10
11use crate::common_utils::error::Sl4fError;
13use crate::server::constants::CONCURRENT_REQ_LIMIT;
14use crate::server::sl4f::Sl4f;
15use crate::server::sl4f_types::{AsyncCommandRequest, AsyncRequest, AsyncResponse, MethodId};
16
17pub async fn run_fidl_loop(sl4f: Arc<Sl4f>, receiver: async_channel::Receiver<AsyncRequest>) {
18 let handler = move |request| handle_request(Arc::clone(&sl4f), request);
19
20 receiver.for_each_concurrent(CONCURRENT_REQ_LIMIT, handler).await
21}
22
23async fn handle_request(sl4f: Arc<Sl4f>, request: AsyncRequest) {
24 match request {
25 AsyncRequest::Cleanup(done) => {
26 sl4f.cleanup().await;
28 sl4f.print().await;
29 done.send(()).unwrap();
30 }
31 AsyncRequest::Command(AsyncCommandRequest { tx, method_id, params }) => {
32 info!(tag = "run_fidl_loop", tx:?, method_id:?, params:?; "Received synchronous request");
33 match method_to_fidl(method_id, params, Arc::clone(&sl4f)).await {
34 Ok(response) => {
35 let async_response = AsyncResponse::new(Ok(response));
36
37 let _ = tx.send(async_response);
40 }
41 Err(err) => {
42 error!(err:?; "Error returned from calling method_to_fidl");
43 let async_response = AsyncResponse::new(Err(err));
44
45 let _ = tx.send(async_response);
48 }
49 };
50 }
51 }
52}
53
54async fn method_to_fidl(method_id: MethodId, args: Value, sl4f: Arc<Sl4f>) -> Result<Value, Error> {
55 if let Some(facade) = sl4f.get_facade(&method_id.facade) {
56 facade.handle_request(method_id.method, args).await
57 } else if sl4f.has_proxy_facade(&method_id.facade) {
58 sl4f.handle_proxy_request(method_id.facade, method_id.method, args).await
59 } else {
60 Err(Sl4fError::new(&format!("Invalid FIDL method type {:?}", &method_id.facade)).into())
61 }
62}