feat: basic scenegraph, node, and spatial

This commit is contained in:
Nova
2022-05-16 14:19:08 -04:00
parent 888e60b8a2
commit cbfd4c13c2
10 changed files with 197 additions and 7 deletions

23
Cargo.lock generated
View File

@@ -66,6 +66,16 @@ dependencies = [
"serde_derive",
]
[[package]]
name = "ctrlc"
version = "3.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b37feaa84e6861e00a1f5e5aa8da3ee56d605c9992d33e082786754828e20865"
dependencies = [
"nix",
"winapi",
]
[[package]]
name = "dirs"
version = "4.0.0"
@@ -193,6 +203,17 @@ dependencies = [
"rand",
]
[[package]]
name = "nix"
version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f17df307904acd05aa8e32e97bb20f2a0df1728bbc2d771ae8f9a90463441e9"
dependencies = [
"bitflags",
"cfg-if",
"libc",
]
[[package]]
name = "num-traits"
version = "0.2.15"
@@ -349,9 +370,11 @@ name = "stardust-xr"
version = "0.9.0"
dependencies = [
"anyhow",
"ctrlc",
"libstardustxr",
"mio",
"slab",
"thiserror",
]
[[package]]

View File

@@ -8,5 +8,7 @@ version = "0.9.0"
[dependencies]
libstardustxr = {path = "../libstardustxr-rs"}
anyhow = "1.0.57"
ctrlc = "3.2.2"
mio = {version = "0.8.3", features = ["net", "os-poll", "os-ext"]}
slab = "0.4.6"
slab = "0.4.6"
thiserror = "1.0.31"

View File

@@ -1,4 +1,4 @@
use libstardustxr::fusion::scenegraph::Scenegraph;
use super::scenegraph::Scenegraph;
use libstardustxr::messenger::Messenger;
use mio::net::UnixStream;
use std::rc::{Rc, Weak};

View File

@@ -22,7 +22,7 @@ impl EventLoop {
let mut socket = UnixListener::bind(socket_path.clone())?;
let (sender, mut receiver) = pipe::new()?;
let join_handle = Some(thread::spawn(move || -> Result<()> {
let mut clients: Slab<Client> = Slab::new();
let mut clients: Slab<Option<Client>> = Slab::new();
let mut poll = Poll::new()?;
let mut events = Events::with_capacity(1024);
const LISTENER: Token = Token(usize::MAX - 1);
@@ -37,8 +37,15 @@ impl EventLoop {
match event.token() {
LISTENER => loop {
match socket.accept() {
Ok((socket, _)) => {
clients.insert(Client::from_connection(socket));
Ok((mut socket, _)) => {
let client_number = clients.insert(None);
poll.registry().register(
&mut socket,
Token(client_number),
Interest::READABLE,
)?;
let client = Client::from_connection(socket);
*clients.get_mut(client_number).unwrap() = Some(client);
}
Err(e) => {
if e.kind() == std::io::ErrorKind::WouldBlock {
@@ -50,7 +57,7 @@ impl EventLoop {
},
STOP => return Ok(()),
token => loop {
match clients.get(token.0).unwrap().dispatch() {
match clients.get(token.0).unwrap().as_ref().unwrap().dispatch() {
Ok(_) => continue,
Err(e) => {
if e.kind() == std::io::ErrorKind::WouldBlock {
@@ -76,6 +83,11 @@ impl Drop for EventLoop {
fn drop(&mut self) {
let buf: [u8; 1] = [1; 1];
let _ = self.stop_write.write(buf.as_slice());
let _ = self.join_handle.take().unwrap().join();
let _ = self
.join_handle
.take()
.unwrap()
.join()
.expect("Couldn't join the event loop thread at drop");
}
}

View File

@@ -1,2 +1,3 @@
pub mod client;
pub mod eventloop;
pub mod scenegraph;

66
src/core/scenegraph.rs Normal file
View File

@@ -0,0 +1,66 @@
use crate::nodes::core::Node;
use anyhow::Result;
use libstardustxr::scenegraph;
use libstardustxr::scenegraph::ScenegraphError;
use std::{cell::RefCell, collections::HashMap, rc::Weak};
#[derive(Default)]
pub struct Scenegraph<'a> {
nodes: RefCell<HashMap<String, Weak<Node<'a>>>>,
}
impl<'a> Scenegraph<'a> {
pub fn new() -> Self {
Default::default()
}
pub fn add_node(&self, node: Weak<Node<'a>>) {
let node_ref = node.upgrade();
if node_ref.is_none() {
return;
}
self.nodes
.borrow_mut()
.insert(String::from(node_ref.unwrap().get_path()), node);
}
pub fn remove_node(&self, node: Weak<Node<'a>>) {
let node_ref = node.upgrade();
if node_ref.is_none() {
return;
}
self.nodes.borrow_mut().remove(node_ref.unwrap().get_path());
}
pub fn get_node(&self, path: &str) -> Weak<Node<'a>> {
self.nodes.borrow().get(path).cloned().unwrap_or_default()
}
}
impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> {
self.nodes
.borrow()
.get(path)
.ok_or(ScenegraphError::NodeNotFound)?
.upgrade()
.ok_or(ScenegraphError::NodeNotFound)?
.send_local_signal(method, data)
.map_err(|_| ScenegraphError::MethodNotFound)
}
fn execute_method(
&self,
path: &str,
method: &str,
data: &[u8],
) -> Result<Vec<u8>, ScenegraphError> {
self.nodes
.borrow()
.get(path)
.ok_or(ScenegraphError::NodeNotFound)?
.upgrade()
.ok_or(ScenegraphError::NodeNotFound)?
.execute_local_method(method, data)
.map_err(|_| ScenegraphError::MethodNotFound)
}
}

View File

@@ -1,8 +1,19 @@
mod core;
mod nodes;
use self::core::eventloop::EventLoop;
use ctrlc;
use std::sync::mpsc::channel;
fn main() {
let (tx, rx) = channel();
ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel."))
.expect("Error setting Ctrl-C handler");
println!("Setting up Stardust socket...");
let event_loop = EventLoop::new(None).expect("Couldn't create server socket");
println!("Stardust socket created at {}", event_loop.socket_path);
rx.recv().expect("Could not receive from channel.");
}

74
src/nodes/core.rs Normal file
View File

@@ -0,0 +1,74 @@
use crate::core::client::Client;
use anyhow::{anyhow, ensure, Result};
use libstardustxr::messenger::Messenger;
use std::{
collections::HashMap,
rc::{Rc, Weak},
vec::Vec,
};
type Signal<'a> = dyn Fn(&[u8]) + 'a;
type Method<'a> = dyn Fn(&[u8]) -> Vec<u8> + 'a;
pub struct Node<'a> {
path: String,
trailing_slash_pos: usize,
pub messenger: Weak<Messenger<'a>>,
local_signals: HashMap<String, Box<Signal<'a>>>,
local_methods: HashMap<String, Box<Method<'a>>>,
}
impl<'a> Node<'a> {
pub fn get_name(&self) -> &str {
&self.path[self.trailing_slash_pos + 1..]
}
pub fn get_path(&self) -> &str {
self.path.as_str()
}
pub fn from_path(client: &Client<'a>, path: &str) -> Result<Rc<Self>> {
ensure!(path.starts_with('/'), "Invalid path {}", path);
let node = Node {
path: path.to_string(),
trailing_slash_pos: path.rfind('/').ok_or(anyhow!("Invalid path {}", path))?,
messenger: client.get_weak_messenger(),
local_signals: HashMap::new(),
local_methods: HashMap::new(),
};
let node_ref = Rc::new(node);
client.scenegraph.add_node(Rc::downgrade(&node_ref));
Ok(node_ref)
}
pub fn send_local_signal(&self, method: &str, data: &[u8]) -> Result<()> {
self.local_signals
.get(method)
.ok_or(anyhow!("Signal {} not found", method))?(data);
Ok(())
}
pub fn execute_local_method(&self, method: &str, data: &[u8]) -> Result<Vec<u8>> {
Ok(self
.local_methods
.get(method)
.ok_or(anyhow!("Method {} not found", method))?(data))
}
pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> {
self.messenger
.upgrade()
.ok_or(anyhow!("Invalid messenger"))?
.send_remote_signal(self.path.as_str(), method, data)
.map_err(|_| anyhow!("Unable to write in messenger"))
}
pub fn execute_remote_method(
&self,
method: &str,
data: &[u8],
callback: Box<dyn Fn(&[u8]) + 'a>,
) -> Result<()> {
self.messenger
.upgrade()
.ok_or(anyhow!("Invalid messenger"))?
.execute_remote_method(self.path.as_str(), method, data, callback)
.map_err(|_| anyhow!("Unable to write in messenger"))
}
}

1
src/nodes/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod core;

0
src/nodes/spatial.rs Normal file
View File