sandbox/fidl/
data_router.rs1use crate::fidl::router;
6use crate::{ConversionError, Data, Router, RouterResponse};
7use fidl::handle::AsHandleRef;
8use futures::TryStreamExt;
9use std::sync::Arc;
10use vfs::directory::entry::DirectoryEntry;
11use vfs::execution_scope::ExecutionScope;
12use {fidl_fuchsia_component_sandbox as fsandbox, fidl_fuchsia_io as fio};
13
14impl crate::RemotableCapability for Router<Data> {
15 fn try_into_directory_entry(
16 self,
17 scope: ExecutionScope,
18 ) -> Result<Arc<dyn DirectoryEntry>, ConversionError> {
19 Ok(self.into_directory_entry(fio::DirentType::Service, scope))
20 }
21}
22
23impl From<Router<Data>> for fsandbox::Capability {
24 fn from(router: Router<Data>) -> Self {
25 let (client_end, sender_stream) =
26 fidl::endpoints::create_request_stream::<fsandbox::DataRouterMarker>();
27 router.serve_and_register(sender_stream, client_end.get_koid().unwrap());
28 fsandbox::Capability::DataRouter(client_end)
29 }
30}
31
32impl TryFrom<RouterResponse<Data>> for fsandbox::DataRouterRouteResponse {
33 type Error = fsandbox::RouterError;
34
35 fn try_from(resp: RouterResponse<Data>) -> Result<Self, Self::Error> {
36 match resp {
37 RouterResponse::<Data>::Capability(c) => {
38 Ok(fsandbox::DataRouterRouteResponse::Data(c.into()))
39 }
40 RouterResponse::<Data>::Unavailable => {
41 Ok(fsandbox::DataRouterRouteResponse::Unavailable(fsandbox::Unit {}))
42 }
43 RouterResponse::<Data>::Debug(_) => Err(fsandbox::RouterError::NotSupported),
44 }
45 }
46}
47
48impl Router<Data> {
49 async fn serve_router(
50 self,
51 mut stream: fsandbox::DataRouterRequestStream,
52 ) -> Result<(), fidl::Error> {
53 while let Ok(Some(request)) = stream.try_next().await {
54 match request {
55 fsandbox::DataRouterRequest::Route { payload, responder } => {
56 responder.send(router::route_from_fidl(&self, payload).await)?;
57 }
58 fsandbox::DataRouterRequest::_UnknownMethod { ordinal, .. } => {
59 log::warn!(ordinal:%; "Received unknown DataRouter request");
60 }
61 }
62 }
63 Ok(())
64 }
65
66 pub fn serve_and_register(self, stream: fsandbox::DataRouterRequestStream, koid: zx::Koid) {
68 let router = self.clone();
69
70 crate::fidl::registry::insert(self.into(), koid, async move {
72 router.serve_router(stream).await.expect("failed to serve Router");
73 });
74 }
75}