1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45//! The reachability monitor monitors reachability state and generates an event to signal
6//! changes.
78use fuchsia_component::server::ServiceFs;
9use fuchsia_inspect::component;
10use futures::channel::mpsc::unbounded;
11use futures::{FutureExt as _, StreamExt as _};
12use log::info;
13use reachability_core::{Monitor, NetworkCheckAction, NetworkCheckCookie};
14use reachability_handler::ReachabilityHandler;
15use std::pin::pin;
1617mod eventloop;
1819use crate::eventloop::EventLoop;
2021#[fuchsia::main(logging_tags = ["reachability"])]
22pub fn main() {
23// TODO(dpradilla): use a `StructOpt` to pass in a log level option where the user can control
24 // how verbose logs should be.
25info!("Starting reachability monitor!");
26let mut executor = fuchsia_async::LocalExecutor::new();
2728let mut fs = ServiceFs::new_local();
2930let mut handler = ReachabilityHandler::new();
31 handler.publish_service(fs.dir("svc"));
3233let inspector = component::inspector();
34// Report data on the size of the inspect VMO, and the number of allocation
35 // failures encountered. (Allocation failures can lead to missing data.)
36component::serve_inspect_stats();
3738let _inspect_server_task =
39 inspect_runtime::publish(inspector, inspect_runtime::PublishOptions::default())
40 .expect("publish Inspect task");
4142let fs = fs.take_and_serve_directory_handle().expect("failed to serve ServiceFS directory");
4344let (sender, receiver) = unbounded::<(NetworkCheckAction, NetworkCheckCookie)>();
45let mut monitor = Monitor::new(sender).expect("failed to create reachability monitor");
46let () = monitor.set_inspector(inspector);
4748info!("monitoring");
49let mut eventloop = EventLoop::new(monitor, handler, receiver, inspector);
50let mut eventloop_fut = pin!(eventloop.run().fuse());
51let mut serve_fut = pin!(fs.fuse().collect());
5253 executor.run_singlethreaded(async {
54futures::select! {
55 r = eventloop_fut => {
56let r: Result<(), anyhow::Error> = r;
57panic!("unexpectedly exited event loop with result {:?}", r);
58 },
59 () = serve_fut => {
60panic!("unexpectedly stopped serving ServiceFS");
61 },
62 }
63 })
64}