Make all nodes thread safe
This commit is contained in:
@@ -4,13 +4,15 @@ use super::spatial::Spatial;
|
||||
use crate::core::client::Client;
|
||||
use anyhow::{anyhow, Result};
|
||||
use libstardustxr::scenegraph::ScenegraphError;
|
||||
use rccell::{RcCell, WeakCell};
|
||||
use nanoid::nanoid;
|
||||
use parking_lot::RwLock;
|
||||
use std::rc::{Rc, Weak};
|
||||
use std::sync::Arc;
|
||||
use std::{collections::HashMap, vec::Vec};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Weak as WeakArc};
|
||||
use std::vec::Vec;
|
||||
|
||||
use core::hash::BuildHasherDefault;
|
||||
use nanoid::nanoid;
|
||||
use dashmap::DashMap;
|
||||
use rustc_hash::FxHasher;
|
||||
|
||||
pub type Signal = fn(&Node, Rc<Client>, &[u8]) -> Result<()>;
|
||||
@@ -21,14 +23,14 @@ pub struct Node {
|
||||
pub(crate) client: Weak<Client>,
|
||||
path: String,
|
||||
// trailing_slash_pos: usize,
|
||||
local_signals: HashMap<String, Signal, BuildHasherDefault<FxHasher>>,
|
||||
local_methods: HashMap<String, Method, BuildHasherDefault<FxHasher>>,
|
||||
destroyable: bool,
|
||||
local_signals: DashMap<String, Signal, BuildHasherDefault<FxHasher>>,
|
||||
local_methods: DashMap<String, Method, BuildHasherDefault<FxHasher>>,
|
||||
destroyable: AtomicBool,
|
||||
|
||||
alias: Option<Alias>,
|
||||
pub spatial: Option<Arc<Spatial>>,
|
||||
pub field: Option<Arc<Field>>,
|
||||
pub pulse_sender: Option<Arc<PulseSender>>,
|
||||
alias: RwLock<Option<Alias>>,
|
||||
pub spatial: RwLock<Option<Arc<Spatial>>>,
|
||||
pub field: RwLock<Option<Arc<Field>>>,
|
||||
pub pulse_sender: RwLock<Option<Arc<PulseSender>>>,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
@@ -42,33 +44,33 @@ impl Node {
|
||||
self.path.as_str()
|
||||
}
|
||||
pub fn is_destroyable(&self) -> bool {
|
||||
self.destroyable
|
||||
self.destroyable.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub fn create(parent: &str, name: &str, destroyable: bool) -> Self {
|
||||
let mut path = parent.to_string();
|
||||
path.push('/');
|
||||
path.push_str(name);
|
||||
let mut node = Node {
|
||||
let node = Node {
|
||||
uid: nanoid!(),
|
||||
client: Weak::new(),
|
||||
path,
|
||||
// trailing_slash_pos: parent.len(),
|
||||
local_signals: Default::default(),
|
||||
local_methods: Default::default(),
|
||||
destroyable,
|
||||
destroyable: AtomicBool::from(destroyable),
|
||||
|
||||
alias: None,
|
||||
spatial: None,
|
||||
field: None,
|
||||
pulse_sender: None,
|
||||
alias: RwLock::new(None),
|
||||
spatial: RwLock::new(None),
|
||||
field: RwLock::new(None),
|
||||
pulse_sender: RwLock::new(None),
|
||||
};
|
||||
node.add_local_signal("destroy", Node::destroy_flex);
|
||||
node
|
||||
}
|
||||
pub fn destroy(&self) {
|
||||
if let Some(client) = self.get_client() {
|
||||
let _ = client.get_scenegraph().remove_node(self.get_path());
|
||||
let _ = client.scenegraph.remove_node(self.get_path());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,10 +81,10 @@ impl Node {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_local_signal(&mut self, name: &str, signal: Signal) {
|
||||
pub fn add_local_signal(&self, name: &str, signal: Signal) {
|
||||
self.local_signals.insert(name.to_string(), signal);
|
||||
}
|
||||
pub fn add_local_method(&mut self, name: &str, method: Method) {
|
||||
pub fn add_local_method(&self, name: &str, method: Method) {
|
||||
self.local_methods.insert(name.to_string(), method);
|
||||
}
|
||||
|
||||
@@ -92,15 +94,14 @@ impl Node {
|
||||
method: &str,
|
||||
data: &[u8],
|
||||
) -> Result<(), ScenegraphError> {
|
||||
if let Some(alias) = self.alias.as_ref() {
|
||||
if let Some(alias) = self.alias.read().as_ref() {
|
||||
if !alias.signals.contains(&method.to_string()) {
|
||||
return Err(ScenegraphError::SignalNotFound);
|
||||
}
|
||||
alias
|
||||
.original
|
||||
.upgrade()
|
||||
.ok_or_else(|| ScenegraphError::BrokenAlias)?
|
||||
.borrow()
|
||||
.ok_or(ScenegraphError::BrokenAlias)?
|
||||
.send_local_signal(calling_client, method, data)
|
||||
} else {
|
||||
let signal = self
|
||||
@@ -117,15 +118,14 @@ impl Node {
|
||||
method: &str,
|
||||
data: &[u8],
|
||||
) -> Result<Vec<u8>, ScenegraphError> {
|
||||
if let Some(alias) = self.alias.as_ref() {
|
||||
if let Some(alias) = self.alias.read().as_ref() {
|
||||
if !alias.methods.contains(&method.to_string()) {
|
||||
return Err(ScenegraphError::MethodNotFound);
|
||||
}
|
||||
alias
|
||||
.original
|
||||
.upgrade()
|
||||
.ok_or_else(|| ScenegraphError::BrokenAlias)?
|
||||
.borrow()
|
||||
.ok_or(ScenegraphError::BrokenAlias)?
|
||||
.execute_local_method(calling_client, method, data)
|
||||
} else {
|
||||
let method = self
|
||||
@@ -139,7 +139,7 @@ impl Node {
|
||||
pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> {
|
||||
self.get_client()
|
||||
.ok_or_else(|| anyhow!("Node has no client, can't send remote signal!"))?
|
||||
.get_messenger()
|
||||
.messenger
|
||||
.send_remote_signal(self.path.as_str(), method, data)
|
||||
.map_err(|_| anyhow!("Unable to write in messenger"))
|
||||
}
|
||||
@@ -151,27 +151,27 @@ impl Node {
|
||||
// ) -> Result<()> {
|
||||
// self.get_client()
|
||||
// .ok_or_else(|| anyhow!("Node has no client, can't send remote signal!"))?
|
||||
// .get_messenger()
|
||||
// .messenger
|
||||
// .execute_remote_method(self.path.as_str(), method, data, callback)
|
||||
// .map_err(|_| anyhow!("Unable to write in messenger"))
|
||||
// }
|
||||
}
|
||||
|
||||
struct Alias {
|
||||
original: WeakCell<Node>,
|
||||
original: WeakArc<Node>,
|
||||
|
||||
signals: Vec<String>,
|
||||
methods: Vec<String>,
|
||||
}
|
||||
impl Alias {
|
||||
pub fn add_to(
|
||||
node: &RcCell<Node>,
|
||||
original: &RcCell<Node>,
|
||||
node: &Arc<Node>,
|
||||
original: &Arc<Node>,
|
||||
signals: Vec<String>,
|
||||
methods: Vec<String>,
|
||||
) {
|
||||
node.borrow_mut().alias = Some(Alias {
|
||||
original: original.downgrade(),
|
||||
*node.alias.write() = Some(Alias {
|
||||
original: Arc::downgrade(original),
|
||||
signals,
|
||||
methods,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user