refactor: fine-grained interior mutability for scenegraph
This commit is contained in:
@@ -2,20 +2,20 @@ use super::scenegraph::Scenegraph;
|
||||
use crate::nodes::spatial;
|
||||
use libstardustxr::messenger::Messenger;
|
||||
use mio::net::UnixStream;
|
||||
use rccell::{RcCell, WeakCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct Client<'a> {
|
||||
pub messenger: Messenger<'a>,
|
||||
messenger: Messenger<'a>,
|
||||
scenegraph: Scenegraph<'a>,
|
||||
}
|
||||
|
||||
impl<'a> Client<'a> {
|
||||
pub fn from_connection(connection: UnixStream) -> RcCell<Self> {
|
||||
let client = RcCell::new(Client {
|
||||
scenegraph: Default::default(),
|
||||
pub fn from_connection(connection: UnixStream) -> Rc<Self> {
|
||||
let client = Rc::new(Client {
|
||||
messenger: Messenger::new(connection),
|
||||
scenegraph: Default::default(),
|
||||
});
|
||||
client.borrow_mut().scenegraph.set_client(client.clone());
|
||||
client.scenegraph.set_client(&client);
|
||||
spatial::create_interface(client.clone());
|
||||
client
|
||||
}
|
||||
@@ -29,7 +29,4 @@ impl<'a> Client<'a> {
|
||||
pub fn get_scenegraph(&self) -> &Scenegraph<'a> {
|
||||
&self.scenegraph
|
||||
}
|
||||
pub fn get_scenegraph_mut(&mut self) -> &mut Scenegraph<'a> {
|
||||
&mut self.scenegraph
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use super::client::Client;
|
||||
use anyhow::{anyhow, Result};
|
||||
use anyhow::Result;
|
||||
use libstardustxr::server;
|
||||
use mio::net::UnixListener;
|
||||
use mio::unix::pipe;
|
||||
use mio::{Events, Interest, Poll, Token};
|
||||
use rccell::RcCell;
|
||||
use slab::Slab;
|
||||
use std::io::Write;
|
||||
use std::rc::Rc;
|
||||
use std::thread::{self, JoinHandle};
|
||||
|
||||
pub struct EventLoop {
|
||||
@@ -24,7 +24,7 @@ impl EventLoop {
|
||||
let join_handle = thread::Builder::new()
|
||||
.name("event_loop".to_owned())
|
||||
.spawn(move || -> Result<()> {
|
||||
let mut clients: Slab<Option<RcCell<Client>>> = Slab::new();
|
||||
let mut clients: Slab<Option<Rc<Client>>> = Slab::new();
|
||||
let mut poll = Poll::new()?;
|
||||
let mut events = Events::with_capacity(1024);
|
||||
const LISTENER: Token = Token(usize::MAX - 1);
|
||||
@@ -59,14 +59,7 @@ impl EventLoop {
|
||||
},
|
||||
STOP => return Ok(()),
|
||||
token => loop {
|
||||
match clients
|
||||
.get(token.0)
|
||||
.unwrap()
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.borrow()
|
||||
.dispatch()
|
||||
{
|
||||
match clients.get(token.0).unwrap().as_ref().unwrap().dispatch() {
|
||||
Ok(_) => continue,
|
||||
Err(e) => {
|
||||
if e.kind() == std::io::ErrorKind::WouldBlock {
|
||||
|
||||
@@ -3,43 +3,45 @@ use crate::nodes::core::Node;
|
||||
use anyhow::Result;
|
||||
use libstardustxr::scenegraph;
|
||||
use libstardustxr::scenegraph::ScenegraphError;
|
||||
use rccell::{RcCell, WeakCell};
|
||||
use rccell::RcCell;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::{Rc, Weak};
|
||||
use std::sync::RwLock;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Scenegraph<'a> {
|
||||
client: WeakCell<Client<'a>>,
|
||||
pub nodes: HashMap<String, RcCell<Node<'a>>>,
|
||||
client: RefCell<Weak<Client<'a>>>,
|
||||
pub nodes: RwLock<HashMap<String, RcCell<Node<'a>>>>,
|
||||
}
|
||||
|
||||
impl<'a> Scenegraph<'a> {
|
||||
pub fn set_client(&mut self, client: RcCell<Client<'a>>) {
|
||||
self.client = client.downgrade();
|
||||
pub fn get_client(&self) -> Rc<Client<'a>> {
|
||||
self.client.borrow().upgrade().unwrap()
|
||||
}
|
||||
|
||||
pub fn add_node(&mut self, node: Node<'a>) -> RcCell<Node<'a>> {
|
||||
pub fn set_client(&self, client: &Rc<Client<'a>>) {
|
||||
*self.client.borrow_mut() = Rc::downgrade(client);
|
||||
}
|
||||
|
||||
pub fn add_node(&self, node: Node<'a>) -> RcCell<Node<'a>> {
|
||||
let path = node.get_path().to_string();
|
||||
let node_rc = RcCell::new(node);
|
||||
self.nodes.insert(path, node_rc.clone());
|
||||
self.nodes.write().unwrap().insert(path, node_rc.clone());
|
||||
node_rc
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Default for Scenegraph<'a> {
|
||||
fn default() -> Self {
|
||||
Scenegraph {
|
||||
client: WeakCell::new(),
|
||||
nodes: HashMap::new(),
|
||||
}
|
||||
pub fn get_node(&self, path: &str) -> Option<RcCell<Node<'a>>> {
|
||||
Some(self.nodes.read().ok()?.get(path)?.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
|
||||
fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> {
|
||||
self.nodes
|
||||
.get(path)
|
||||
self.get_node(path)
|
||||
.ok_or(ScenegraphError::NodeNotFound)?
|
||||
.borrow()
|
||||
.send_local_signal(self.client.upgrade().unwrap(), method, data)
|
||||
.send_local_signal(self.get_client(), method, data)
|
||||
.map_err(|_| ScenegraphError::MethodNotFound)
|
||||
}
|
||||
fn execute_method(
|
||||
@@ -48,11 +50,10 @@ impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
|
||||
method: &str,
|
||||
data: &[u8],
|
||||
) -> Result<Vec<u8>, ScenegraphError> {
|
||||
self.nodes
|
||||
.get(path)
|
||||
self.get_node(path)
|
||||
.ok_or(ScenegraphError::NodeNotFound)?
|
||||
.borrow()
|
||||
.execute_local_method(self.client.upgrade().unwrap(), method, data)
|
||||
.execute_local_method(self.get_client(), method, data)
|
||||
.map_err(|_| ScenegraphError::MethodNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user