This commit is contained in:
John Smith
2022-02-15 13:40:17 -05:00
parent 125901fcd8
commit 7458d0d991
12 changed files with 191 additions and 103 deletions

View File

@@ -357,38 +357,38 @@ impl AttachmentManager {
attachment_machine.state()
}
pub async fn wait_for_state(&self, state: AttachmentState, timeout_ms: Option<u32>) -> bool {
let start_time = intf::get_timestamp();
// pub async fn wait_for_state(&self, state: AttachmentState, timeout_ms: Option<u32>) -> bool {
// let start_time = intf::get_timestamp();
loop {
let (current_state, eventual) = self
.inner
.lock()
.attachment_machine
.state_eventual_instance();
if current_state == state {
break;
}
if let Some(timeout_ms) = timeout_ms {
let timeout_time = start_time + (timeout_ms as u64 * 1000);
let cur_time = intf::get_timestamp();
if timeout_time > cur_time {
let timeout_dur_ms = ((timeout_time - cur_time) / 1000) as u32;
// loop {
// let (current_state, eventual) = self
// .inner
// .lock()
// .attachment_machine
// .state_eventual_instance();
// if current_state == state {
// break;
// }
// if let Some(timeout_ms) = timeout_ms {
// let timeout_time = start_time + (timeout_ms as u64 * 1000);
// let cur_time = intf::get_timestamp();
// if timeout_time > cur_time {
// let timeout_dur_ms = ((timeout_time - cur_time) / 1000) as u32;
if match intf::timeout(timeout_dur_ms, eventual).await {
Ok(v) => v,
Err(_) => return false,
} == state
{
return true;
}
} else {
return false;
}
} else if eventual.await == state {
break;
}
}
true
}
// if match intf::timeout(timeout_dur_ms, eventual).await {
// Ok(v) => v,
// Err(_) => return false,
// } == state
// {
// return true;
// }
// } else {
// return false;
// }
// } else if eventual.await == state {
// break;
// }
// }
// true
// }
}

View File

@@ -21,6 +21,7 @@ pub struct VeilidCoreContext {
pub block_store: BlockStore,
pub crypto: Crypto,
pub attachment_manager: AttachmentManager,
pub update_callback: UpdateCallback,
}
impl VeilidCoreContext {
@@ -128,12 +129,13 @@ impl VeilidCoreContext {
// Set up attachment manager
trace!("VeilidCoreContext::new init attachment manager");
let update_callback_move = update_callback.clone();
let attachment_manager =
AttachmentManager::new(config.clone(), table_store.clone(), crypto.clone());
if let Err(e) = attachment_manager
.init(Arc::new(
move |_old_state: AttachmentState, new_state: AttachmentState| {
update_callback(VeilidUpdate::Attachment { state: new_state })
update_callback_move(VeilidUpdate::Attachment { state: new_state })
},
))
.await
@@ -154,6 +156,7 @@ impl VeilidCoreContext {
block_store,
crypto,
attachment_manager,
update_callback,
})
}
@@ -167,6 +170,9 @@ impl VeilidCoreContext {
self.protected_store.terminate().await;
self.config.terminate().await;
// send final shutdown update
(self.update_callback)(VeilidUpdate::Shutdown).await;
trace!("VeilidCoreContext::shutdown complete");
ApiLogger::terminate();
}

View File

@@ -22,14 +22,7 @@ pub async fn test_attach_detach() {
api.attach().await.unwrap();
intf::sleep(5000).await;
api.detach().await.unwrap();
api.wait_for_update(
VeilidUpdate::Attachment {
state: AttachmentState::Detached,
},
None,
)
.await
.unwrap();
intf::sleep(2000).await;
api.shutdown().await;
info!("--- test auto detach ---");

View File

@@ -168,6 +168,7 @@ pub enum VeilidUpdate {
Attachment {
state: AttachmentState,
},
Shutdown,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -1216,28 +1217,6 @@ impl VeilidAPI {
Ok(())
}
// wait for a matching update
pub async fn wait_for_update(
&self,
update: VeilidUpdate,
timeout_ms: Option<u32>,
) -> Result<(), VeilidAPIError> {
match update {
VeilidUpdate::Log {
log_level: _,
message: _,
} => {
// No point in waiting for a log
}
VeilidUpdate::Attachment { state } => {
self.attachment_manager()?
.wait_for_state(state, timeout_ms)
.await;
}
}
Ok(())
}
// Change api logging level if it is enabled
pub async fn change_api_log_level(&self, log_level: VeilidConfigLogLevel) {
ApiLogger::change_log_level(log_level.to_level_filter());