This commit is contained in:
John Smith 2022-01-23 11:12:54 -05:00
parent 2eeb8e52f2
commit 1decd333c8
4 changed files with 221 additions and 154 deletions

View File

@ -109,6 +109,9 @@ name = "anyhow"
version = "1.0.52" version = "1.0.52"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84450d0b4a8bd1ba4144ce8ce718fbc5d071358b1e5384bace6536b3d1f2d5b3" checksum = "84450d0b4a8bd1ba4144ce8ce718fbc5d071358b1e5384bace6536b3d1f2d5b3"
dependencies = [
"backtrace",
]
[[package]] [[package]]
name = "arrayref" name = "arrayref"
@ -1357,9 +1360,9 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.112" version = "0.2.113"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" checksum = "eef78b64d87775463c549fbd80e19249ef436ea3bf1de2a1eb7e717ec7fab1e9"
[[package]] [[package]]
name = "libsqlite3-sys" name = "libsqlite3-sys"
@ -2392,9 +2395,9 @@ dependencies = [
[[package]] [[package]]
name = "socket2" name = "socket2"
version = "0.4.2" version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dc90fe6c7be1a323296982db1836d1ea9e47b6839496dde9a541bc496df3516" checksum = "0f82496b90c36d70af5fcd482edaa2e0bd16fade569de1330405fecbbdac736b"
dependencies = [ dependencies = [
"libc", "libc",
"winapi", "winapi",
@ -2432,9 +2435,9 @@ checksum = "734676eb262c623cec13c3155096e08d1f8f29adce39ba17948b18dad1e54142"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.85" version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a684ac3dcd8913827e18cd09a68384ee66c1de24157e3c556c9ab16d85695fb7" checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2718,10 +2721,12 @@ dependencies = [
name = "veilid-flutter" name = "veilid-flutter"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"async-std", "async-std",
"cfg-if 1.0.0", "cfg-if 1.0.0",
"flutter_rust_bridge", "flutter_rust_bridge",
"log", "log",
"parking_lot",
"veilid-core", "veilid-core",
] ]

View File

@ -10,7 +10,9 @@ crate-type = ["cdylib", "staticlib"]
async-std = { version = "^1", features = ["unstable"] } async-std = { version = "^1", features = ["unstable"] }
veilid-core = { path="../../veilid-core" } veilid-core = { path="../../veilid-core" }
flutter_rust_bridge = "^1" flutter_rust_bridge = "^1"
parking_lot = "^0"
log = "^0" log = "^0"
anyhow = { version = "^1", features = ["backtrace"] }
[build-dependencies] [build-dependencies]
cfg-if = "^1" cfg-if = "^1"

View File

@ -83,5 +83,5 @@ fn main() {
.wait() .wait()
.expect("flutter_rust_bridge_codegen was not running"); .expect("flutter_rust_bridge_codegen was not running");
println!("cargo:rerun-if-changed=src/api.c"); println!("cargo:rerun-if-changed={}", input_path.to_str().unwrap());
} }

View File

@ -2,6 +2,13 @@ use std::sync::Arc;
use flutter_rust_bridge::*; use flutter_rust_bridge::*;
use log::*; use log::*;
use std::collections::HashMap; use std::collections::HashMap;
use async_std::sync::Mutex as AsyncMutex;
use anyhow::*;
use std::fmt;
// Globals
static API: AsyncMutex<Option<veilid_core::VeilidAPI>> = AsyncMutex::new(None);
///////////////////////////////////////// /////////////////////////////////////////
// Config Settings // Config Settings
@ -100,45 +107,45 @@ pub struct VeilidConfigNetwork {
pub leases: VeilidConfigLeases, pub leases: VeilidConfigLeases,
} }
#[derive(Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct VeilidConfigTableStore { pub struct VeilidConfigTableStore {
pub directory: String, pub directory: String,
pub delete: bool, pub delete: bool,
} }
#[derive(Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct VeilidConfigBlockStore { pub struct VeilidConfigBlockStore {
pub directory: String, pub directory: String,
pub delete: bool, pub delete: bool,
} }
#[derive(Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct VeilidConfigProtectedStore { pub struct VeilidConfigProtectedStore {
pub allow_insecure_fallback: bool,
pub always_use_insecure_storage: bool,
pub insecure_fallback_directory: String,
pub delete: bool,
} }
#[derive(Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct VeilidConfigCapabilities {
pub protocol_udp: bool,
pub protocol_connect_tcp: bool,
pub protocol_accept_tcp: bool,
pub protocol_connect_ws: bool,
pub protocol_accept_ws: bool,
pub protocol_connect_wss: bool,
pub protocol_accept_wss: bool,
}
#[derive(Default, Clone)]
pub struct VeilidConfig { pub struct VeilidConfig {
pub program_name: String, pub program_name: String,
pub namespace: String, pub namespace: String,
pub capabilities: VeilidConfigCapabilities, // Capabilities
pub protected_store: VeilidConfigProtectedStore, pub capabilities__protocol_udp: bool,
pub capabilities__protocol_connect_tcp: bool,
pub capabilities__protocol_accept_tcp: bool,
pub capabilities__protocol_connect_ws: bool,
pub capabilities__protocol_accept_ws: bool,
pub capabilities__protocol_connect_wss: bool,
pub capabilities__protocol_accept_wss: bool,
// Protected Store
pub protected_store__allow_insecure_fallback: bool,
pub protected_store__always_use_insecure_storage: bool,
pub protected_store__insecure_fallback_directory: String,
pub protected_store__delete: bool,
// Table Store
pub table_store: VeilidConfigTableStore, pub table_store: VeilidConfigTableStore,
// Block Store
pub block_store: VeilidConfigBlockStore, pub block_store: VeilidConfigBlockStore,
// Network
pub network: VeilidConfigNetwork, pub network: VeilidConfigNetwork,
} }
@ -173,6 +180,45 @@ pub enum VeilidAPIError {
}, },
} }
impl fmt::Display for VeilidAPIError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
VeilidAPIError::AlreadyInitialized => write!(f, "VeilidAPIError::AlreadyInitialized"),
VeilidAPIError::NotInitialized => write!(f, "VeilidAPIError::NotInitialized"),
VeilidAPIError::InvalidConfig(e) => write!(f, "VeilidAPIError::InvalidConfig({})", e),
VeilidAPIError::Timeout => write!(f, "VeilidAPIError::Timeout"),
VeilidAPIError::Shutdown => write!(f, "VeilidAPIError::Shutdown"),
VeilidAPIError::NodeNotFound(ni) => write!(f, "VeilidAPIError::NodeNotFound({})", ni),
VeilidAPIError::NoDialInfo(ni) => write!(f, "VeilidAPIError::NoDialInfo({})", ni),
VeilidAPIError::Internal(e) => write!(f, "VeilidAPIError::Internal({})", e),
VeilidAPIError::Unimplemented(e) => write!(f, "VeilidAPIError::Unimplemented({})", e),
VeilidAPIError::ParseError { message, value } => {
write!(f, "VeilidAPIError::ParseError({}: {})", message, value)
}
VeilidAPIError::InvalidArgument {
context,
argument,
value,
} => {
write!(
f,
"VeilidAPIError::InvalidArgument({}: {} = {})",
context, argument, value
)
}
VeilidAPIError::MissingArgument { context, argument } => {
write!(
f,
"VeilidAPIError::MissingArgument({}: {})",
context, argument
)
}
}
}
}
impl std::error::Error for VeilidAPIError {}
impl VeilidAPIError { impl VeilidAPIError {
fn from_core(api_error: veilid_core::VeilidAPIError) -> Self { fn from_core(api_error: veilid_core::VeilidAPIError) -> Self {
match api_error { match api_error {
@ -233,7 +279,7 @@ impl VeilidUpdate {
#[derive(Debug)] #[derive(Debug)]
pub struct VeilidState { pub struct VeilidState {
attachment: AttachmentState, pub attachment: AttachmentState,
} }
impl VeilidState { impl VeilidState {
@ -244,14 +290,19 @@ impl VeilidState {
} }
} }
type Result<T> = std::result::Result<T, VeilidAPIError>;
///////////////////////////////////////// /////////////////////////////////////////
pub fn startup_veilid_core(sink: StreamSink<VeilidUpdate>, config: VeilidConfig) -> Result<VeilidState> { pub fn startup_veilid_core(sink: StreamSink<VeilidUpdate>, config: VeilidConfig) -> Result<VeilidState> {
async_std::task::block_on( async {
let api = API.lock().await;
if api.is_some() {
return Err(anyhow!(VeilidAPIError::AlreadyInitialized));
}
let core = veilid_core::VeilidCore::new(); let core = veilid_core::VeilidCore::new();
// convert config to hashmap // convert config to hashmap
let config_map = HashMap::<String, Box<dyn core::any::Any + 'static>>::new(); let config_map = HashMap::<String, Box<dyn core::any::Any + Send + 'static>>::new();
macro_rules! get_config { macro_rules! get_config {
($key:expr) => { ($key:expr) => {
config_map.insert(stringify!($key)[7..].to_owned(), Box::new($key.clone())); config_map.insert(stringify!($key)[7..].to_owned(), Box::new($key.clone()));
@ -367,19 +418,28 @@ pub fn startup_veilid_core(sink: StreamSink<VeilidUpdate>, config: VeilidConfig)
), ),
}; };
async_std::task::block_on( async { let veilid_api = core.startup(setup).await.map_err(|e| VeilidAPIError::InvalidConfig(e.clone()))?;
let api = core.startup(setup).await.map_err(|e| VeilidAPIError::InvalidConfig(e.clone()))?; *api = Some(veilid_api.clone());
let core_state = api.get_state().await.map_err(VeilidAPIError::from_core)?;
let core_state = veilid_api.get_state().await.map_err(VeilidAPIError::from_core)?;
Ok(VeilidState::from_core(core_state)) Ok(VeilidState::from_core(core_state))
}) })
} }
pub fn get_veilid_state() -> Result<VeilidState> { pub fn get_veilid_state() -> Result<VeilidState> {
async_std::task::block_on( async {
let veilid_api = API.lock().await.ok_or(anyhow!(VeilidAPIError::NotInitialized))?;
let core_state = veilid_api.get_state().await.map_err(VeilidAPIError::from_core)?;
Ok(VeilidState::from_core(core_state))
})
} }
// xxx api functions // xxx api functions
pub fn shutdown_veilid_core() -> Result<()> { pub fn shutdown_veilid_core() -> Result<()> {
async_std::task::block_on( async {
let veilid_api = API.lock().await.take().ok_or(anyhow!(VeilidAPIError::NotInitialized))?;
veilid_api.shutdown().await;
Ok(())
})
} }