From 82f680b35f61d91b6e1a07a70e4cee91e44f1c89 Mon Sep 17 00:00:00 2001 From: John Smith Date: Fri, 11 Mar 2022 07:35:41 -0500 Subject: [PATCH] checkpoint before android work --- Cargo.lock | 1 + veilid-cli/src/main.rs | 4 +- veilid-core/src/api_logger.rs | 40 ++++--------------- veilid-core/src/callback_state_machine.rs | 6 +-- veilid-core/src/core_context.rs | 15 ++++--- .../src/tests/common/test_veilid_config.rs | 10 ++--- veilid-flutter/android/build.gradle | 1 + .../android/src/main/AndroidManifest.xml | 5 +++ .../example/android/app/build.gradle | 1 + veilid-flutter/example/lib/config.dart | 2 +- veilid-flutter/example/lib/main.dart | 18 +++++---- veilid-flutter/rust/src/dart_ffi.rs | 26 +++++------- veilid-server/Cargo.toml | 1 + veilid-server/src/server.rs | 19 ++++----- 14 files changed, 61 insertions(+), 88 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b29885a..beb7f507 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4295,6 +4295,7 @@ dependencies = [ "daemonize", "directories", "failure", + "flume", "futures", "lazy_static", "log", diff --git a/veilid-cli/src/main.rs b/veilid-cli/src/main.rs index ca3c0cef..82fd8936 100644 --- a/veilid-cli/src/main.rs +++ b/veilid-cli/src/main.rs @@ -4,7 +4,7 @@ use veilid_core::xx::*; use async_std::prelude::*; -use clap::{App, Arg, ColorChoice}; +use clap::{Arg, ColorChoice, Command}; use flexi_logger::*; use std::ffi::OsStr; use std::net::ToSocketAddrs; @@ -20,7 +20,7 @@ pub mod veilid_client_capnp { } fn parse_command_line(default_config_path: &OsStr) -> Result { - let matches = App::new("veilid-cli") + let matches = Command::new("veilid-cli") .version("0.1") .color(ColorChoice::Auto) .about("Veilid Console Client") diff --git a/veilid-core/src/api_logger.rs b/veilid-core/src/api_logger.rs index 60ab81ae..1597fa76 100644 --- a/veilid-core/src/api_logger.rs +++ b/veilid-core/src/api_logger.rs @@ -1,5 +1,4 @@ use crate::core_context::*; -use crate::intf::*; use crate::veilid_api::*; use crate::xx::*; use log::{set_boxed_logger, set_max_level, Level, LevelFilter, Log, Metadata, Record}; @@ -8,8 +7,7 @@ use once_cell::sync::OnceCell; struct ApiLoggerInner { level: LevelFilter, filter_ignore: Cow<'static, [Cow<'static, str>]>, - join_handle: Option>, - tx: Option>, + update_callback: UpdateCallback, } #[derive(Clone)] @@ -21,21 +19,10 @@ static API_LOGGER: OnceCell = OnceCell::new(); impl ApiLogger { fn new_inner(level: LevelFilter, update_callback: UpdateCallback) -> ApiLoggerInner { - let (tx, rx) = flume::unbounded::<(VeilidLogLevel, String)>(); - let join_handle: Option> = Some(spawn(async move { - while let Ok(v) = rx.recv_async().await { - (update_callback)(VeilidUpdate::Log { - log_level: v.0, - message: v.1, - }) - .await; - } - })); ApiLoggerInner { level, filter_ignore: Default::default(), - join_handle, - tx: Some(tx), + update_callback, } } @@ -54,20 +41,8 @@ impl ApiLogger { pub async fn terminate() { if let Some(api_logger) = API_LOGGER.get() { - let mut join_handle = None; - { - let mut inner = api_logger.inner.lock(); - - // Terminate channel - if let Some(inner) = (*inner).as_mut() { - inner.tx = None; - join_handle = inner.join_handle.take(); - } - *inner = None; - } - if let Some(jh) = join_handle { - jh.await; - } + let mut inner = api_logger.inner.lock(); + *inner = None; // Clear everything and we're done set_max_level(LevelFilter::Off); @@ -139,9 +114,10 @@ impl Log for ApiLogger { let s = format!("{}{}{}", tgt, loc, record.args()); - if let Some(tx) = &inner.tx { - let _ = tx.try_send((ll, s)); - } + (inner.update_callback)(VeilidUpdate::Log { + log_level: ll, + message: s, + }) } } } diff --git a/veilid-core/src/callback_state_machine.rs b/veilid-core/src/callback_state_machine.rs index 50c07e59..aa9ebe3a 100644 --- a/veilid-core/src/callback_state_machine.rs +++ b/veilid-core/src/callback_state_machine.rs @@ -4,12 +4,12 @@ pub use rust_fsm::*; cfg_if! { if #[cfg(target_arch = "wasm32")] { pub type StateChangeCallback = Arc< - dyn Fn(::State, ::State) -> SystemPinBoxFuture<()> + dyn Fn(::State, ::State) + 'static, >; } else { pub type StateChangeCallback = Arc< - dyn Fn(::State, ::State) -> SystemPinBoxFuture<()> + dyn Fn(::State, ::State) + Send + Sync + 'static, @@ -88,7 +88,7 @@ where (inner.callback.clone(), eventual) }; if let Some(cb) = callback { - cb(old_state, new_state).await; + cb(old_state, new_state); } eventual.resolve(new_state).await; Ok(output) diff --git a/veilid-core/src/core_context.rs b/veilid-core/src/core_context.rs index f84936c2..b38f3fb6 100644 --- a/veilid-core/src/core_context.rs +++ b/veilid-core/src/core_context.rs @@ -8,9 +8,9 @@ use crate::xx::*; cfg_if! { if #[cfg(target_arch = "wasm32")] { - pub type UpdateCallback = Arc SystemPinBoxFuture<()>>; + pub type UpdateCallback = Arc; } else { - pub type UpdateCallback = Arc SystemPinBoxFuture<()> + Send + Sync>; + pub type UpdateCallback = Arc; } } @@ -73,7 +73,7 @@ impl ServicesContext { info!("Veilid API logging initialized"); } - trace!("startup starting"); + info!("Veilid API starting up"); // Set up protected store trace!("init protected store"); @@ -134,12 +134,12 @@ impl ServicesContext { } self.attachment_manager = Some(attachment_manager); - trace!("startup complete"); + info!("Veilid API startup complete"); Ok(()) } pub async fn shutdown(&mut self) { - trace!("shutdown starting"); + info!("Veilid API shutting down"); if let Some(attachment_manager) = &mut self.attachment_manager { trace!("terminate attachment manager"); @@ -162,13 +162,13 @@ impl ServicesContext { protected_store.terminate().await; } - trace!("shutdown complete"); + info!("Veilid API shutdown complete"); // api logger terminate is idempotent ApiLogger::terminate().await; // send final shutdown update - (self.update_callback)(VeilidUpdate::Shutdown).await; + (self.update_callback)(VeilidUpdate::Shutdown); } } @@ -221,7 +221,6 @@ impl VeilidCoreContext { if #[cfg(target_os = "android")] { if utils::android::ANDROID_GLOBALS.lock().is_none() { error!("Android globals are not set up"); - config.terminate().await; return Err(VeilidAPIError::Internal { message: "Android globals are not set up".to_owned() }); } } diff --git a/veilid-core/src/tests/common/test_veilid_config.rs b/veilid-core/src/tests/common/test_veilid_config.rs index ac2e770a..fa67f948 100644 --- a/veilid-core/src/tests/common/test_veilid_config.rs +++ b/veilid-core/src/tests/common/test_veilid_config.rs @@ -160,13 +160,9 @@ cfg_if! { pub fn setup_veilid_core() -> (UpdateCallback, ConfigCallback) { ( - Arc::new( - move |veilid_update: VeilidUpdate| -> SystemPinBoxFuture<()> { - Box::pin(async move { - trace!("update_callback: {:?}", veilid_update); - }) - }, - ), + Arc::new(move |veilid_update: VeilidUpdate| { + println!("update_callback: {:?}", veilid_update); + }), Arc::new(config_callback), ) } diff --git a/veilid-flutter/android/build.gradle b/veilid-flutter/android/build.gradle index 084e79e8..7950329a 100644 --- a/veilid-flutter/android/build.gradle +++ b/veilid-flutter/android/build.gradle @@ -74,6 +74,7 @@ android { } dependencies { + implementation 'androidx.security:security-crypto:1.1.0-alpha03' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" } diff --git a/veilid-flutter/android/src/main/AndroidManifest.xml b/veilid-flutter/android/src/main/AndroidManifest.xml index c032cc21..a8a95055 100644 --- a/veilid-flutter/android/src/main/AndroidManifest.xml +++ b/veilid-flutter/android/src/main/AndroidManifest.xml @@ -1,3 +1,8 @@ + + + + + diff --git a/veilid-flutter/example/android/app/build.gradle b/veilid-flutter/example/android/app/build.gradle index bc4e186f..16b5d357 100644 --- a/veilid-flutter/example/android/app/build.gradle +++ b/veilid-flutter/example/android/app/build.gradle @@ -64,5 +64,6 @@ flutter { } dependencies { + implementation 'androidx.security:security-crypto:1.1.0-alpha03' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" } diff --git a/veilid-flutter/example/lib/config.dart b/veilid-flutter/example/lib/config.dart index b320ab2e..2ff86541 100644 --- a/veilid-flutter/example/lib/config.dart +++ b/veilid-flutter/example/lib/config.dart @@ -7,7 +7,7 @@ Future getDefaultVeilidConfig() async { return VeilidConfig( programName: "Veilid Plugin Test", namespace: "", - apiLogLevel: VeilidConfigLogLevel.trace, + apiLogLevel: VeilidConfigLogLevel.info, capabilities: VeilidConfigCapabilities( protocolUDP: !kIsWeb, protocolConnectTCP: !kIsWeb, diff --git a/veilid-flutter/example/lib/main.dart b/veilid-flutter/example/lib/main.dart index 205e5b38..d6ef3bb3 100644 --- a/veilid-flutter/example/lib/main.dart +++ b/veilid-flutter/example/lib/main.dart @@ -42,9 +42,9 @@ void setRootLogLevel(LogLevel? level) { void initLoggy() { Loggy.initLoggy( - logPrinter: ConsolePrinter( + logPrinter: StreamPrinter(ConsolePrinter( const PrettyDeveloperPrinter(), - ), + )), logOptions: getLogOptions(null), ); } @@ -90,14 +90,16 @@ class _MyAppState extends State with UiLoggy { // We also handle the message potentially returning null. try { veilidVersion = Veilid.instance.veilidVersionString(); - } on PlatformException { + } on Exception { veilidVersion = 'Failed to get veilid version.'; } - loggy.error("Error test"); - loggy.warning("Warning test"); - loggy.info("Info test"); - loggy.debug("Debug test"); - loggy.trace("Trace test"); + + // In case of hot restart shut down first + try { + await Veilid.instance.shutdownVeilidCore(); + } on Exception { + // + } // If the widget was removed from the tree while the asynchronous platform // message was in flight, we want to discard the reply rather than calling diff --git a/veilid-flutter/rust/src/dart_ffi.rs b/veilid-flutter/rust/src/dart_ffi.rs index f4fa3412..7f083927 100644 --- a/veilid-flutter/rust/src/dart_ffi.rs +++ b/veilid-flutter/rust/src/dart_ffi.rs @@ -115,21 +115,17 @@ pub extern "C" fn startup_veilid_core(port: i64, config: FfiStr) { } let sink = stream.clone(); - let update_callback = Arc::new( - move |update: veilid_core::VeilidUpdate| -> veilid_core::SystemPinBoxFuture<()> { - let sink = sink.clone(); - Box::pin(async move { - match update { - veilid_core::VeilidUpdate::Shutdown => { - sink.close(); - } - _ => { - sink.item_json(update); - } - } - }) - }, - ); + let update_callback = Arc::new(move |update: veilid_core::VeilidUpdate| { + let sink = sink.clone(); + match update { + veilid_core::VeilidUpdate::Shutdown => { + sink.close(); + } + _ => { + sink.item_json(update); + } + } + }); let res = veilid_core::api_startup_json(update_callback, config_json).await; let veilid_api = check_err_json!(stream, res); diff --git a/veilid-server/Cargo.toml b/veilid-server/Cargo.toml index 15006e54..fe9efebb 100644 --- a/veilid-server/Cargo.toml +++ b/veilid-server/Cargo.toml @@ -32,6 +32,7 @@ url = "^2" ctrlc = "^3" lazy_static = "^1" bugsalot = "^0" +flume = { version = "^0", features = ["async"] } [target.'cfg(windows)'.dependencies] windows-service = "^0" diff --git a/veilid-server/src/server.rs b/veilid-server/src/server.rs index 4876e419..1d337810 100644 --- a/veilid-server/src/server.rs +++ b/veilid-server/src/server.rs @@ -1,7 +1,7 @@ use crate::client_api; use crate::settings::*; use crate::veilid_logs::*; -use async_std::channel::{bounded, Receiver, Sender}; +use flume::{bounded, Receiver, Sender}; use lazy_static::*; use log::*; use parking_lot::Mutex; @@ -31,16 +31,11 @@ pub async fn run_veilid_server(settings: Settings, logs: VeilidLogs) -> Result<( ) = bounded(1); // Create VeilidCore setup - let update_callback = Arc::new( - move |change: veilid_core::VeilidUpdate| -> veilid_core::SystemPinBoxFuture<()> { - let sender = sender.clone(); - Box::pin(async move { - if sender.send(change).await.is_err() { - error!("error sending veilid update callback"); - } - }) - }, - ); + let update_callback = Arc::new(move |change: veilid_core::VeilidUpdate| { + if sender.send(change).is_err() { + error!("error sending veilid update callback"); + } + }); let config_callback = settings.get_core_config_callback(); // Start Veilid Core and get API @@ -66,7 +61,7 @@ pub async fn run_veilid_server(settings: Settings, logs: VeilidLogs) -> Result<( // Handle state changes on main thread for capnproto rpc let update_receiver_jh = capi.clone().map(|capi| { async_std::task::spawn_local(async move { - while let Ok(change) = receiver.recv().await { + while let Ok(change) = receiver.recv_async().await { capi.clone().handle_update(change); } })