remove veilid-wasm project, too out of date

refactor state updates and formalize a VeilidState object
work on veilid-flutter api
This commit is contained in:
John Smith
2022-01-18 12:33:14 -05:00
parent e39835d51f
commit 1b6864bf22
36 changed files with 512 additions and 3963 deletions

View File

@@ -367,7 +367,9 @@ impl AttachmentManager {
attachment_machine.state()
}
pub async fn wait_for_state(&self, state: AttachmentState) {
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
@@ -377,9 +379,28 @@ impl AttachmentManager {
if current_state == state {
break;
}
if eventual.await == 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
}
}

View File

@@ -27,7 +27,7 @@ pub mod xx;
pub use self::attachment_manager::AttachmentState;
pub use self::veilid_api::*;
pub use self::veilid_config::*;
pub use self::veilid_core::{VeilidCore, VeilidCoreSetup, VeilidState, VeilidStateChange};
pub use self::veilid_core::{VeilidCore, VeilidCoreSetup};
pub mod veilid_capnp {
include!(concat!(env!("OUT_DIR"), "/proto/veilid_capnp.rs"));

View File

@@ -8,10 +8,10 @@ static LOREM_IPSUM:&[u8] = b"Lorem ipsum dolor sit amet, consectetur adipiscing
fn setup_veilid_core() -> VeilidCoreSetup {
VeilidCoreSetup {
state_change_callback: Arc::new(
move |change: VeilidStateChange| -> SystemPinBoxFuture<()> {
update_callback: Arc::new(
move |veilid_update: VeilidUpdate| -> SystemPinBoxFuture<()> {
Box::pin(async move {
trace!("state_change_callback: {:?}", change);
trace!("update_callback: {:?}", veilid_update);
})
},
),

View File

@@ -5,10 +5,10 @@ use crate::*;
fn setup_veilid_core() -> VeilidCoreSetup {
VeilidCoreSetup {
state_change_callback: Arc::new(
move |change: VeilidStateChange| -> SystemPinBoxFuture<()> {
update_callback: Arc::new(
move |veilid_update: VeilidUpdate| -> SystemPinBoxFuture<()> {
Box::pin(async move {
trace!("state_change_callback: {:?}", change);
trace!("update_callback: {:?}", veilid_update);
})
},
),

View File

@@ -6,10 +6,10 @@ use crate::*;
fn setup_veilid_core() -> VeilidCoreSetup {
VeilidCoreSetup {
state_change_callback: Arc::new(
move |change: VeilidStateChange| -> SystemPinBoxFuture<()> {
update_callback: Arc::new(
move |veilid_update: VeilidUpdate| -> SystemPinBoxFuture<()> {
Box::pin(async move {
trace!("state_change_callback: {:?}", change);
trace!("update_callback: {:?}", veilid_update);
})
},
),

View File

@@ -145,10 +145,10 @@ cfg_if! {
pub fn setup_veilid_core() -> VeilidCoreSetup {
VeilidCoreSetup {
state_change_callback: Arc::new(
move |change: VeilidStateChange| -> SystemPinBoxFuture<()> {
update_callback: Arc::new(
move |veilid_update: VeilidUpdate| -> SystemPinBoxFuture<()> {
Box::pin(async move {
trace!("state_change_callback: {:?}", change);
trace!("update_callback: {:?}", veilid_update);
})
},
),

View File

@@ -25,7 +25,7 @@ pub async fn test_attach_detach() {
api.attach().await.unwrap();
intf::sleep(5000).await;
api.detach().await.unwrap();
api.wait_for_state(VeilidState::Attachment(AttachmentState::Detached))
api.wait_for_update(VeilidUpdate::Attachment(AttachmentState::Detached), None)
.await
.unwrap();
api.shutdown().await;

View File

@@ -5,7 +5,7 @@ pub use debug::*;
pub use crate::rpc_processor::InfoAnswer;
use crate::*;
use attachment_manager::AttachmentManager;
use attachment_manager::*;
use core::fmt;
use network_manager::NetworkManager;
use routing_table::*;
@@ -106,6 +106,18 @@ macro_rules! parse_error {
/////////////////////////////////////////////////////////////////////////////////////////////////////
#[derive(Debug)]
pub enum VeilidUpdate {
Attachment(AttachmentState),
}
#[derive(Debug)]
pub struct VeilidState {
pub attachment: AttachmentState,
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
///
#[derive(Clone, Debug, Default, PartialOrd, PartialEq, Eq, Ord)]
pub struct NodeId {
pub key: DHTKey,
@@ -1100,12 +1112,13 @@ impl VeilidAPI {
////////////////////////////////////////////////////////////////
// Attach/Detach
// issue state changed updates for updating clients
pub async fn send_state_update(&self) -> Result<(), VeilidAPIError> {
trace!("VeilidCore::send_state_update");
// get a full copy of the current state
pub async fn get_state(&self) -> Result<VeilidState, VeilidAPIError> {
trace!("VeilidCore::get_state");
let attachment_manager = self.attachment_manager()?;
attachment_manager.send_state_update().await;
Ok(())
Ok(VeilidState {
attachment: attachment_manager.get_state(),
})
}
// connect to the network
@@ -1124,12 +1137,17 @@ impl VeilidAPI {
Ok(())
}
// wait for state change
// xxx: should have optional timeout
pub async fn wait_for_state(&self, state: VeilidState) -> Result<(), VeilidAPIError> {
match state {
VeilidState::Attachment(cs) => {
self.attachment_manager()?.wait_for_state(cs).await;
// wait for a matching update
pub async fn wait_for_update(
&self,
update: VeilidUpdate,
timeout_ms: Option<u32>,
) -> Result<(), VeilidAPIError> {
match update {
VeilidUpdate::Attachment(cs) => {
self.attachment_manager()?
.wait_for_state(cs, timeout_ms)
.await;
}
}
Ok(())

View File

@@ -7,27 +7,14 @@ use crate::xx::*;
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
pub type StateChangeCallback = Arc<dyn Fn(VeilidStateChange) -> SystemPinBoxFuture<()>>;
pub type UpdateCallback = Arc<dyn Fn(VeilidUpdate) -> SystemPinBoxFuture<()>>;
} else {
pub type StateChangeCallback = Arc<dyn Fn(VeilidStateChange) -> SystemPinBoxFuture<()> + Send + Sync>;
pub type UpdateCallback = Arc<dyn Fn(VeilidUpdate) -> SystemPinBoxFuture<()> + Send + Sync>;
}
}
#[derive(Debug)]
pub enum VeilidStateChange {
Attachment {
old_state: AttachmentState,
new_state: AttachmentState,
},
}
#[derive(Debug)]
pub enum VeilidState {
Attachment(AttachmentState),
}
pub struct VeilidCoreSetup {
pub state_change_callback: StateChangeCallback,
pub update_callback: UpdateCallback,
pub config_callback: ConfigCallback,
}
@@ -139,16 +126,13 @@ impl VeilidCore {
// Set up attachment manager
trace!("VeilidCore::internal_startup init attachment manager");
let cb = setup.state_change_callback;
let cb = setup.update_callback;
let attachment_manager =
AttachmentManager::new(config.clone(), table_store.clone(), crypto.clone());
attachment_manager
.init(Arc::new(
move |old_state: AttachmentState, new_state: AttachmentState| {
cb(VeilidStateChange::Attachment {
old_state,
new_state,
})
move |_old_state: AttachmentState, new_state: AttachmentState| {
cb(VeilidUpdate::Attachment(new_state))
},
))
.await?;