checkpoint before android work

This commit is contained in:
John Smith
2022-03-11 07:35:41 -05:00
parent 2a9522cc24
commit 82f680b35f
14 changed files with 61 additions and 88 deletions

View File

@@ -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<JoinHandle<()>>,
tx: Option<flume::Sender<(VeilidLogLevel, String)>>,
update_callback: UpdateCallback,
}
#[derive(Clone)]
@@ -21,21 +19,10 @@ static API_LOGGER: OnceCell<ApiLogger> = OnceCell::new();
impl ApiLogger {
fn new_inner(level: LevelFilter, update_callback: UpdateCallback) -> ApiLoggerInner {
let (tx, rx) = flume::unbounded::<(VeilidLogLevel, String)>();
let join_handle: Option<JoinHandle<()>> = 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,
})
}
}
}

View File

@@ -4,12 +4,12 @@ pub use rust_fsm::*;
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
pub type StateChangeCallback<T> = Arc<
dyn Fn(<T as StateMachineImpl>::State, <T as StateMachineImpl>::State) -> SystemPinBoxFuture<()>
dyn Fn(<T as StateMachineImpl>::State, <T as StateMachineImpl>::State)
+ 'static,
>;
} else {
pub type StateChangeCallback<T> = Arc<
dyn Fn(<T as StateMachineImpl>::State, <T as StateMachineImpl>::State) -> SystemPinBoxFuture<()>
dyn Fn(<T as StateMachineImpl>::State, <T as StateMachineImpl>::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)

View File

@@ -8,9 +8,9 @@ use crate::xx::*;
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
pub type UpdateCallback = Arc<dyn Fn(VeilidUpdate) -> SystemPinBoxFuture<()>>;
pub type UpdateCallback = Arc<dyn Fn(VeilidUpdate)>;
} else {
pub type UpdateCallback = Arc<dyn Fn(VeilidUpdate) -> SystemPinBoxFuture<()> + Send + Sync>;
pub type UpdateCallback = Arc<dyn Fn(VeilidUpdate) + Send + Sync>;
}
}
@@ -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() });
}
}

View File

@@ -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),
)
}