before changing to tokio udp

This commit is contained in:
Lukas Bachschwell 2020-01-26 18:28:35 +01:00
parent 4bccd18bd6
commit 0d084cf287
Signed by: lbsadmin
GPG Key ID: CCC6AA87CC8DF425
1 changed files with 78 additions and 65 deletions

View File

@ -8,72 +8,86 @@ use std::i64;
use std::io; use std::io;
use std::io::Write; use std::io::Write;
use std::net; use std::net;
use std::thread::spawn;
use std::time::Instant; use std::time::Instant;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio_tungstenite::connect_async; use tokio_tungstenite::connect_async;
use tungstenite::protocol::Message; use tungstenite::protocol::Message;
use url::Url; use url::Url;
fn listen(socket: &net::UdpSocket) { async fn listen(senddata: futures::channel::mpsc::UnboundedReceiver<String>) {
let mut buf: [u8; 20] = [0; 20]; // Setup the UDP Socket
let mut result: Vec<u8> = Vec::new(); let udpsocket = net::UdpSocket::bind("0.0.0.0:0").expect("failed to bind host udp socket"); // local bind port
match socket.recv_from(&mut buf) {
Ok((number_of_bytes, _)) => {
result = Vec::from(&buf[0..number_of_bytes]);
}
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
return ();
}
Err(e) => println!("failed listening {:?}", e),
}
let display_result = result.clone(); let msg = String::from("ok").into_bytes();
let result_str = String::from_utf8(display_result).unwrap(); udpsocket
println!("received message: {:?}", result_str); .send_to(&msg, "127.0.0.1:9000")
.expect("cannot send");
if result_str.contains("S0R1") { loop {
write_file("Race active".to_string(), "racestate.txt"); // Handle Sending part
write_file("0".to_string(), "rx1.txt"); //senddata
write_file("0".to_string(), "rx2.txt"); /*
write_file("0".to_string(), "rx3.txt"); let msg = String::from("ok").into_bytes();
} udpsocket
if result_str.contains("S0R0") { .send_to(&msg, "192.168.0.141:9000")
write_file("Race inactive".to_string(), "racestate.txt"); .expect("cannot send");*/
} // -------------
let mut buf: [u8; 20] = [0; 20];
let mut result: Vec<u8> = Vec::new();
match udpsocket.recv_from(&mut buf).await {
Ok((number_of_bytes, _)) => {
result = Vec::from(&buf[0..number_of_bytes]);
}
Err(e) => println!("failed listening {:?}", e),
}
if result_str.contains("S0L") { let display_result = result.clone();
// zb sS1L0000000DAF let result_str = String::from_utf8(display_result).unwrap();
let lap_time = i64::from_str_radix(&result_str[5..13], 16).unwrap_or(-1); println!("received message: {:?}", result_str);
if lap_time != -1 {
let lap_seconds = (lap_time as f64) / (1000 as f64); if result_str.contains("S0R1") {
write_file(lap_seconds.to_string(), "rx1_laptime.txt"); write_file("Race active".to_string(), "racestate.txt");
write_file("0".to_string(), "rx1.txt");
write_file("0".to_string(), "rx2.txt");
write_file("0".to_string(), "rx3.txt");
} }
let intval = &result_str[3..5].parse::<i32>().unwrap_or(-1); if result_str.contains("S0R0") {
if *intval != -1 { write_file("Race inactive".to_string(), "racestate.txt");
write_file((intval + 1).to_string(), "rx1.txt");
} }
}
if result_str.contains("S1L") { if result_str.contains("S0L") {
let lap_time = i64::from_str_radix(&result_str[5..13], 16).unwrap_or(-1); // zb sS1L0000000DAF
if lap_time != -1 { let lap_time = i64::from_str_radix(&result_str[5..13], 16).unwrap_or(-1);
let lap_seconds = (lap_time as f64) / (1000 as f64); if lap_time != -1 {
write_file(lap_seconds.to_string(), "rx2_laptime.txt"); let lap_seconds = (lap_time as f64) / (1000 as f64);
write_file(lap_seconds.to_string(), "rx1_laptime.txt");
}
let intval = &result_str[3..5].parse::<i32>().unwrap_or(-1);
if *intval != -1 {
write_file((intval + 1).to_string(), "rx1.txt");
}
} }
let intval = &result_str[3..5].parse::<i32>().unwrap_or(-1); if result_str.contains("S1L") {
if *intval != -1 { let lap_time = i64::from_str_radix(&result_str[5..13], 16).unwrap_or(-1);
write_file((intval + 1).to_string(), "rx2.txt"); if lap_time != -1 {
let lap_seconds = (lap_time as f64) / (1000 as f64);
write_file(lap_seconds.to_string(), "rx2_laptime.txt");
}
let intval = &result_str[3..5].parse::<i32>().unwrap_or(-1);
if *intval != -1 {
write_file((intval + 1).to_string(), "rx2.txt");
}
} }
} if result_str.contains("S2L") {
if result_str.contains("S2L") { let lap_time = i64::from_str_radix(&result_str[5..13], 16).unwrap_or(-1);
let lap_time = i64::from_str_radix(&result_str[5..13], 16).unwrap_or(-1); if lap_time != -1 {
if lap_time != -1 { let lap_seconds = (lap_time as f64) / (1000 as f64);
let lap_seconds = (lap_time as f64) / (1000 as f64); write_file(lap_seconds.to_string(), "rx3_laptime.txt");
write_file(lap_seconds.to_string(), "rx3_laptime.txt"); }
} let intval = &result_str[3..5].parse::<i32>().unwrap_or(-1);
let intval = &result_str[3..5].parse::<i32>().unwrap_or(-1); if *intval != -1 {
if *intval != -1 { write_file((intval + 1).to_string(), "rx3.txt");
write_file((intval + 1).to_string(), "rx3.txt"); }
} }
} }
} }
@ -153,31 +167,29 @@ async fn main() {
let (write, read) = ws_stream.split(); let (write, read) = ws_stream.split();
let (obstx, obsrx) = futures::channel::mpsc::unbounded(); let (obstx, obsrx) = futures::channel::mpsc::unbounded();
let (udpsockettx, udpsocketrx) = futures::channel::mpsc::unbounded();
let ws_to_stdout = { let ws_to_stdout = {
read.for_each(|message| { read.for_each(|message| {
async { async {
let data = message.unwrap().into_data(); let data = message.unwrap().into_data();
println!("Messg");
tokio::io::stdout().write_all(&data).await.unwrap(); tokio::io::stdout().write_all(&data).await.unwrap();
} }
}) })
}; };
let stdin_to_ws = obsrx.map(Ok).forward(write); let programm_to_ws = obsrx.map(Ok).forward(write);
pin_mut!(stdin_to_ws, ws_to_stdout);
future::select(stdin_to_ws, ws_to_stdout).await;
// Setup the UDP Socket pin_mut!(programm_to_ws, ws_to_stdout);
let udpsocket = net::UdpSocket::bind("0.0.0.0:0").expect("failed to bind host udp socket"); // local bind port
udpsocket.set_nonblocking(true).unwrap();
let msg = String::from("ok").into_bytes(); tokio::spawn(listen(udpsocketrx));
udpsocket
.send_to(&msg, "192.168.0.141:9000") println!("Will wait now");
.expect("cannot send"); future::select(programm_to_ws, ws_to_stdout).await;
/*
loop { loop {
listen(&udpsocket);
if now.elapsed().as_secs() >= 5 { if now.elapsed().as_secs() >= 5 {
let request = json!({"request-type":"SetTextFreetype2Properties", "source":source_id,"message-id": random::<f64>().to_string(), "text": now.elapsed().as_millis().to_string() }); let request = json!({"request-type":"SetTextFreetype2Properties", "source":source_id,"message-id": random::<f64>().to_string(), "text": now.elapsed().as_millis().to_string() });
obstx obstx
@ -187,4 +199,5 @@ async fn main() {
now = Instant::now(); now = Instant::now();
} }
} }
*/
} }