This commit is contained in:
John Smith 2022-10-05 19:12:10 -04:00
parent 98f90154f3
commit b1cc0d803c
4 changed files with 44 additions and 11 deletions

14
Cargo.lock generated
View File

@ -1919,6 +1919,19 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "gloo-utils"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40913a05c8297adca04392f707b1e73b12ba7b8eab7244a4961580b1fd34063c"
dependencies = [
"js-sys",
"serde",
"serde_json",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "grpcio"
version = "0.9.1"
@ -5448,6 +5461,7 @@ dependencies = [
"console_error_panic_hook",
"data-encoding",
"futures-util",
"gloo-utils",
"js-sys",
"lazy_static",
"send_wrapper 0.6.0",

View File

@ -10,28 +10,37 @@ use std::io;
/////////////////////////////////////////////////////////////////
struct NetworkInner {
network_manager: NetworkManager,
network_started: bool,
network_needs_restart: bool,
protocol_config: Option<ProtocolConfig>,
}
struct NetworkUnlockedInner {
network_manager: NetworkManager,
}
#[derive(Clone)]
pub struct Network {
config: VeilidConfig,
inner: Arc<Mutex<NetworkInner>>,
unlocked_inner: Arc<NetworkUnlockedInner>,
}
impl Network {
fn new_inner(network_manager: NetworkManager) -> NetworkInner {
fn new_inner() -> NetworkInner {
NetworkInner {
network_manager,
network_started: false,
network_needs_restart: false,
protocol_config: None, //join_handle: None,
}
}
fn new_unlocked_inner(network_manager: NetworkManager) -> NetworkUnlockedInner {
NetworkUnlockedInner {
network_manager
}
}
pub fn new(
network_manager: NetworkManager,
routing_table: RoutingTable,
@ -39,15 +48,16 @@ impl Network {
) -> Self {
Self {
config: network_manager.config(),
inner: Arc::new(Mutex::new(Self::new_inner(network_manager))),
inner: Arc::new(Mutex::new(Self::new_inner())),
unlocked_inner: Arc::new(Self::new_unlocked_inner(network_manager))
}
}
fn network_manager(&self) -> NetworkManager {
self.inner.lock().network_manager.clone()
self.unlocked_inner.network_manager.clone()
}
fn connection_manager(&self) -> ConnectionManager {
self.inner.lock().network_manager.connection_manager()
self.unlocked_inner.network_manager.connection_manager()
}
/////////////////////////////////////////////////////////////////
@ -269,15 +279,22 @@ impl Network {
trace!("stopping network");
// Reset state
let network_manager = self.inner.lock().network_manager.clone();
let network_manager = self.network_manager();
let routing_table = network_manager.routing_table();
// Drop all dial info
routing_table.clear_dial_info_details(RoutingDomain::PublicInternet);
routing_table.clear_dial_info_details(RoutingDomain::LocalNetwork);
let mut editor = routing_table.edit_routing_domain(RoutingDomain::PublicInternet);
editor.disable_node_info_updates();
editor.clear_dial_info_details();
editor.commit().await;
let mut editor = routing_table.edit_routing_domain(RoutingDomain::LocalNetwork);
editor.disable_node_info_updates();
editor.clear_dial_info_details();
editor.commit().await;
// Cancels all async background tasks by dropping join handles
*self.inner.lock() = Self::new_inner(network_manager);
*self.inner.lock() = Self::new_inner();
trace!("network stopped");
}

View File

@ -27,6 +27,7 @@ lazy_static = "^1"
send_wrapper = "^0"
futures-util = { version = "^0", default_features = false, features = ["alloc"] }
data-encoding = { version = "^2" }
gloo-utils = { version = "^0", features = ["serde"] }
[dev-dependencies]
wasm-bindgen-test = "^0"

View File

@ -10,6 +10,7 @@ use alloc::*;
use core::any::{Any, TypeId};
use core::cell::RefCell;
use futures_util::FutureExt;
use gloo_utils::format::JsValueSerdeExt;
use js_sys::*;
use lazy_static::*;
use send_wrapper::*;
@ -306,5 +307,5 @@ pub fn veilid_version() -> JsValue {
minor,
patch,
};
JsValue::from_serde(&vv).unwrap()
<JsValue as JsValueSerdeExt>::from_serde(&vv).unwrap()
}