| // 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. |
| |
| //#![deny(warnings)] |
| #![feature( |
| futures_api, |
| pin, |
| arbitrary_self_types, |
| await_macro, |
| async_await |
| )] |
| |
| use fidl_fuchsia_bluetooth_control::ControlMarker; |
| use fidl::endpoints::ServiceMarker; |
| use failure::{Error, ResultExt}; |
| use fuchsia_app::server::ServicesServer; |
| use fuchsia_async as fasync; |
| use futures::future::FutureExt; |
| use futures::stream::TryStreamExt; |
| |
| mod actor; |
| mod adapters; |
| mod control; |
| mod host; |
| mod host_dispatcher; |
| mod types; |
| |
| use crate::host_dispatcher::*; |
| use crate::host::*; |
| use crate::actor::{ActorHandle, System}; |
| use crate::adapters::watch_hosts; |
| use crate::control::control_service; |
| |
| fn main() -> Result<(), Error> { |
| let executor = fasync::Executor::new().context("Error creating executor")?; |
| let mut system = System::new(); |
| let dispatcher = system.spawn(HostDispatcher::<FidlHost>::new()); |
| let mut d = dispatcher.clone(); |
| fasync::spawn(watch_hosts().try_for_each(move |msg| { |
| d.send(HostDispatcherMsg::AdapterMsg(msg)); |
| async { Ok(()) } |
| }).map(|_| ())); |
| |
| let d_ = dispatcher.clone(); |
| let _server = |
| ServicesServer::new() |
| .add_service((ControlMarker::NAME, move |chan: fasync::Channel| { run_control_service(d_.clone(), chan)})) |
| .start()?; |
| |
| system.run(executor) |
| } |
| |
| fn run_control_service<H: Host + 'static>(hd: ActorHandle<HostDispatcherMsg<H>>, chan: fasync::Channel) { |
| fasync::spawn({ |
| let hd_ = hd.clone(); |
| async { |
| let r = await!(control_service(hd_, chan)); |
| r.unwrap_or_else(|e| eprintln!("Failed to spawn {:?}", e)) |
| }}) |
| } |