2022-12-11 23:41:22 +01:00
|
|
|
use bevy::prelude::*;
|
|
|
|
|
2022-12-12 00:07:42 +01:00
|
|
|
extern crate bytes;
|
2022-12-12 23:18:34 +01:00
|
|
|
extern crate vrpn;
|
|
|
|
|
2022-12-14 16:56:52 +01:00
|
|
|
// use futures_lite::future;
|
|
|
|
use rand::Rng;
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
2022-12-12 23:18:34 +01:00
|
|
|
use bytes::{Bytes, BytesMut};
|
|
|
|
use std::{
|
2022-12-13 20:35:22 +01:00
|
|
|
io::{self, Read},
|
2022-12-12 23:18:34 +01:00
|
|
|
net::{SocketAddr, TcpStream},
|
|
|
|
};
|
2022-12-11 23:41:22 +01:00
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
use std::sync::mpsc::{Receiver,channel};
|
|
|
|
|
2022-12-11 23:41:22 +01:00
|
|
|
use vrpn::{
|
2022-12-12 23:18:34 +01:00
|
|
|
codec::peek_u32,
|
2022-12-12 00:07:42 +01:00
|
|
|
constants::MAGIC_DATA,
|
|
|
|
cookie::check_ver_nonfile_compatible,
|
2022-12-14 16:56:52 +01:00
|
|
|
handler::{HandlerCode, TypedHandler},
|
2022-12-12 23:18:34 +01:00
|
|
|
message::MessageSize,
|
2022-12-14 16:56:52 +01:00
|
|
|
sync_io::{read_cookie, write_cookie, EndpointSyncTcp},
|
2022-12-13 20:35:22 +01:00
|
|
|
tracker::PoseReport,
|
2022-12-14 22:55:26 +01:00
|
|
|
unbuffer, CookieData, Message, Result, SequencedGenericMessage, TypeDispatcher, Unbuffer, TypedBodylessHandler,
|
2022-12-11 23:41:22 +01:00
|
|
|
};
|
|
|
|
|
2022-12-14 16:56:52 +01:00
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
struct TrackerProxy;
|
2022-12-11 23:41:22 +01:00
|
|
|
|
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
#[derive(Resource,Debug)]
|
|
|
|
struct Tracker;
|
|
|
|
|
|
|
|
impl Tracker {
|
|
|
|
|
|
|
|
pub fn start_tracker(&self) {
|
|
|
|
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
|
|
|
|
let (sender, receiver): (_, Receiver<PoseReport>) = channel();
|
|
|
|
|
|
|
|
|
|
|
|
let addr: SocketAddr = "127.0.0.1:3883".parse().unwrap();
|
|
|
|
let mut stream = TcpStream::connect(addr).unwrap();
|
|
|
|
stream.set_nodelay(true).unwrap();
|
|
|
|
|
|
|
|
// We first write our cookie, then read and check the server's cookie, before the loop.
|
|
|
|
write_cookie(&mut stream, CookieData::from(MAGIC_DATA)).unwrap();
|
|
|
|
let cookie_buf = read_cookie(&mut stream).unwrap();
|
|
|
|
let mut cookie_buf = Bytes::from(&cookie_buf[..]);
|
|
|
|
|
|
|
|
CookieData::unbuffer_ref(&mut cookie_buf)
|
|
|
|
.and_then(|msg| check_ver_nonfile_compatible(msg.version)).unwrap();
|
|
|
|
|
|
|
|
let mut endpoint = EndpointSyncTcp::new(stream);
|
|
|
|
let mut dispatcher = TypeDispatcher::new();
|
|
|
|
|
|
|
|
//let _ = dispatcher.add_typed_handler(Box::new(TrackerHandler {}), None).unwrap();
|
|
|
|
|
|
|
|
let _ = dispatcher.add_typed_handler(Box::new( Tracker {} ), None).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
// setup a channel
|
|
|
|
// let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
|
|
|
// vrpn implementation is rather verbose - we need to see what specific errors
|
|
|
|
// should be handled properly!
|
|
|
|
endpoint.poll_endpoint(&mut dispatcher).unwrap_or_else(|err| {
|
|
|
|
info!("error from tracker thread {:?}",err);
|
|
|
|
})
|
|
|
|
|
|
|
|
// dispatcher.han
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// impl TypedBodylessHandler for Tracker {
|
2022-12-12 23:18:34 +01:00
|
|
|
// type Item = PoseReport;
|
2022-12-14 22:55:26 +01:00
|
|
|
// fn handle_typed_bodyless(&mut self, header: &vrpn::MessageHeader) -> Result<HandlerCode> {
|
|
|
|
|
2022-12-12 23:18:34 +01:00
|
|
|
// Ok(HandlerCode::ContinueProcessing)
|
|
|
|
// }
|
|
|
|
// }
|
2022-12-11 23:41:22 +01:00
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
impl TypedHandler for Tracker {
|
|
|
|
type Item = PoseReport;
|
|
|
|
fn handle_typed(&mut self, msg: &Message<PoseReport>) -> Result<HandlerCode> {
|
|
|
|
println!("{:?}\n {:?}", msg.header, msg.body);
|
|
|
|
Ok(HandlerCode::ContinueProcessing)
|
|
|
|
}
|
|
|
|
}
|
2022-12-12 00:07:42 +01:00
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
impl FromWorld for Tracker {
|
|
|
|
fn from_world(world: &mut World) -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
}
|
2022-12-12 00:07:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-12-12 23:18:34 +01:00
|
|
|
|
2022-12-12 03:12:56 +01:00
|
|
|
|
2022-12-14 13:00:18 +01:00
|
|
|
|
2022-12-12 00:07:42 +01:00
|
|
|
|
|
|
|
|
2022-12-12 23:18:34 +01:00
|
|
|
|
|
|
|
|
2022-12-13 18:13:59 +01:00
|
|
|
|
|
|
|
|
2022-12-11 23:41:22 +01:00
|
|
|
|
|
|
|
|
2022-12-12 03:12:56 +01:00
|
|
|
|
2022-12-13 20:35:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-12-11 23:41:22 +01:00
|
|
|
|
2022-12-12 23:18:34 +01:00
|
|
|
|
2022-12-11 23:41:22 +01:00
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct TrackerHandler;
|
|
|
|
impl TypedHandler for TrackerHandler {
|
|
|
|
type Item = PoseReport;
|
|
|
|
fn handle_typed(&mut self, msg: &Message<PoseReport>) -> Result<HandlerCode> {
|
|
|
|
println!("{:?}\n {:?}", msg.header, msg.body);
|
|
|
|
Ok(HandlerCode::ContinueProcessing)
|
2022-12-13 18:13:59 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-14 16:56:52 +01:00
|
|
|
|
|
|
|
pub fn setup_threaded_tracker(mut commands: Commands) {
|
|
|
|
// let (tx, rx) = bounded::<u32>(10);
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
let addr: SocketAddr = "127.0.0.1:3883".parse().unwrap();
|
|
|
|
let mut stream = TcpStream::connect(addr).unwrap();
|
|
|
|
stream.set_nodelay(true).unwrap();
|
|
|
|
|
|
|
|
// We first write our cookie, then read and check the server's cookie, before the loop.
|
|
|
|
write_cookie(&mut stream, CookieData::from(MAGIC_DATA)).unwrap();
|
|
|
|
let cookie_buf = read_cookie(&mut stream).unwrap();
|
|
|
|
let mut cookie_buf = Bytes::from(&cookie_buf[..]);
|
|
|
|
|
|
|
|
CookieData::unbuffer_ref(&mut cookie_buf)
|
|
|
|
.and_then(|msg| check_ver_nonfile_compatible(msg.version)).unwrap();
|
|
|
|
|
|
|
|
let mut endpoint = EndpointSyncTcp::new(stream);
|
|
|
|
let mut dispatcher = TypeDispatcher::new();
|
2022-12-14 22:55:26 +01:00
|
|
|
|
2022-12-14 16:56:52 +01:00
|
|
|
let _ = dispatcher.add_typed_handler(Box::new(TrackerHandler {}), None).unwrap();
|
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
|
|
|
|
// setup a channel
|
|
|
|
// let (tx, rx) = mpsc::channel();
|
|
|
|
|
2022-12-14 16:56:52 +01:00
|
|
|
loop {
|
|
|
|
|
|
|
|
// vrpn implementation is rather verbose - we need to see what specific errors
|
|
|
|
// should be handled properly!
|
|
|
|
endpoint.poll_endpoint(&mut dispatcher).unwrap_or_else(|err| {
|
|
|
|
info!("error from tracker thread {:?}",err);
|
|
|
|
})
|
2022-12-14 22:55:26 +01:00
|
|
|
|
|
|
|
// dispatcher.han
|
|
|
|
|
2022-12-14 16:56:52 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|