dual server baybee

This commit is contained in:
Alex Janka 2022-09-01 14:09:41 +10:00
parent f47b37ac92
commit 2dee00761b
3 changed files with 1430 additions and 70 deletions

1347
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -11,4 +11,5 @@ serde = { version = "1.0", features = ["derive"] }
bincode = "1.3" bincode = "1.3"
csv = "1.1" csv = "1.1"
chrono = "0.4" chrono = "0.4"
rocket = { version = "0.5.0-rc.2", features = ["json"] }
fh5-common = { path = "fh5-common", version="0.1" } fh5-common = { path = "fh5-common", version="0.1" }

View file

@ -1,17 +1,19 @@
use fh5_common::{Filename, Telemetry}; use fh5_common::{Filename, Telemetry};
#[macro_use]
extern crate rocket;
use bincode;
use chrono::Local;
use clap::Parser;
use rocket::State;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::Write; use std::io::Write;
use std::net::UdpSocket; use std::net::UdpSocket;
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
use std::os::unix::fs::OpenOptionsExt; use std::os::unix::fs::OpenOptionsExt;
use std::path::Path; use std::path::Path;
use std::sync::{Arc, Mutex};
use std::{fs, net}; use std::{fs, net};
use bincode;
use chrono::Local;
use clap::Parser;
macro_rules! verbose_print { macro_rules! verbose_print {
($args:expr, $($tts:tt)*) => { ($args:expr, $($tts:tt)*) => {
if($args.verbose) { if($args.verbose) {
@ -55,9 +57,24 @@ fn listen(socket: &net::UdpSocket, mut buffer: &mut [u8]) -> usize {
number_of_bytes number_of_bytes
} }
fn main() { struct DataStruct {
data: Arc<Mutex<[f32; 3]>>,
}
#[rocket::main]
async fn main() {
let args = Args::parse(); let args = Args::parse();
let ip = format!("0.0.0.0:{}", args.port.to_string()); let ip = format!("0.0.0.0:{}", args.port.to_string());
let data = Arc::new(Mutex::new([0. as f32, 0. as f32, 0. as f32]));
let data_for_thread = data.clone();
let _listener = std::thread::spawn(move || {
let mut data = data_for_thread.lock().unwrap();
data[0] = 1.;
data[1] = 2.;
data[2] = 3.;
drop(data);
verbose_print!( verbose_print!(
args, args,
"{}: Starting server", "{}: Starting server",
@ -80,6 +97,7 @@ fn main() {
next: None, next: None,
position: 0, position: 0,
}; };
'listener: while listen(&socket, &mut buf) != 0 { 'listener: while listen(&socket, &mut buf) != 0 {
let deserialised: Telemetry = bincode::deserialize(&buf).expect("error parsing packet"); let deserialised: Telemetry = bincode::deserialize(&buf).expect("error parsing packet");
if deserialised.is_race_on == 0 { if deserialised.is_race_on == 0 {
@ -90,6 +108,12 @@ fn main() {
status.position = deserialised.race_position; status.position = deserialised.race_position;
verbose_print!(args, "now position {}", status.position); verbose_print!(args, "now position {}", status.position);
} }
let mut data = data_for_thread.lock().unwrap();
data[0] = deserialised.position_x;
data[1] = deserialised.position_y;
data[2] = deserialised.position_z;
drop(data);
// status.next. // status.next.
if deserialised.race_position == 0 { if deserialised.race_position == 0 {
status.next.take().and_then(|next| -> Option<NextFile> { status.next.take().and_then(|next| -> Option<NextFile> {
@ -121,6 +145,18 @@ fn main() {
} }
} }
} }
});
let _result = rocket::custom(rocket::Config::figment().merge(("port", 1111)))
.mount("/", routes![index])
.manage(DataStruct { data })
.launch()
.await;
}
#[get("/")]
fn index(data: &State<DataStruct>) -> String {
let get = data.data.lock().unwrap();
format!("x: {}, y: {}, z: {}", get[0], get[1], get[2])
} }
fn begin_race(deserialised: &Telemetry) -> NextFile { fn begin_race(deserialised: &Telemetry) -> NextFile {