mirror of
https://github.com/valmojr/armatak.git
synced 2026-06-13 21:23:30 +00:00
formatted some rust files for linting porpuses
This commit is contained in:
@@ -9,116 +9,119 @@ use std::thread;
|
||||
use crate::cot;
|
||||
|
||||
pub enum UdpCommand {
|
||||
SendMessage(String, Context),
|
||||
Stop,
|
||||
SendMessage(String, Context),
|
||||
Stop,
|
||||
}
|
||||
|
||||
pub struct UdpClient {
|
||||
pub(crate) tx: Sender<UdpCommand>,
|
||||
pub(crate) tx: Sender<UdpCommand>,
|
||||
}
|
||||
|
||||
impl UdpClient {
|
||||
pub fn start(&self, address: String, rx: Receiver<UdpCommand>, ctx: Context) {
|
||||
if let Some(ref client) = *UDP_CLIENT.lock().unwrap() {
|
||||
client.stop();
|
||||
pub fn start(&self, address: String, rx: Receiver<UdpCommand>, ctx: Context) {
|
||||
if let Some(ref client) = *UDP_CLIENT.lock().unwrap() {
|
||||
client.stop();
|
||||
}
|
||||
|
||||
thread::spawn(move || {
|
||||
let socket = match UdpSocket::bind("0.0.0.0:0") {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
let _ = ctx.callback_data(
|
||||
"UDP SOCKET ERROR",
|
||||
"Failed to bind UDP socket",
|
||||
e.to_string(),
|
||||
);
|
||||
info!("Failed to bind UDP socket: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let _ = ctx.callback_data("UDP SOCKET", "EUD Connected", address.clone());
|
||||
|
||||
let mut running = true;
|
||||
while running {
|
||||
match rx.recv() {
|
||||
Ok(UdpCommand::SendMessage(message, context)) => {
|
||||
if let Err(e) = socket.send_to(message.as_bytes(), &address) {
|
||||
info!("Failed to send UDP message: {}", e);
|
||||
let _ = context.callback_data(
|
||||
"UDP SOCKET ERROR",
|
||||
"Failed to send UDP message",
|
||||
e.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(UdpCommand::Stop) => {
|
||||
running = false;
|
||||
info!("Stopping UDP client.");
|
||||
}
|
||||
Err(error) => {
|
||||
info!("Error receiving command: {}", error.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
thread::spawn(move || {
|
||||
let socket = match UdpSocket::bind("0.0.0.0:0") {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
let _ = ctx.callback_data(
|
||||
"UDP SOCKET ERROR",
|
||||
"Failed to bind UDP socket",
|
||||
e.to_string(),
|
||||
);
|
||||
info!("Failed to bind UDP socket: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
pub fn send_payload(&self, context: Context, payload: String) {
|
||||
let tx = self.tx.clone();
|
||||
thread::spawn(move || {
|
||||
tx.send(UdpCommand::SendMessage(payload, context)).unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
let _ = ctx.callback_data("UDP SOCKET", "EUD Connected", address.clone());
|
||||
|
||||
let mut running = true;
|
||||
while running {
|
||||
match rx.recv() {
|
||||
Ok(UdpCommand::SendMessage(message, context)) => {
|
||||
if let Err(e) = socket.send_to(message.as_bytes(), &address) {
|
||||
info!("Failed to send UDP message: {}", e);
|
||||
let _ = context.callback_data(
|
||||
"UDP SOCKET ERROR",
|
||||
"Failed to send UDP message",
|
||||
e.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(UdpCommand::Stop) => {
|
||||
running = false;
|
||||
info!("Stopping UDP client.");
|
||||
}
|
||||
Err(error) => {
|
||||
info!("Error receiving command: {}", error.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn send_payload(&self, context: Context, payload: String) {
|
||||
let tx = self.tx.clone();
|
||||
thread::spawn(move || {
|
||||
tx.send(UdpCommand::SendMessage(payload, context)).unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
pub fn stop(&self) {
|
||||
let tx = self.tx.clone();
|
||||
thread::spawn(move || {
|
||||
tx.send(UdpCommand::Stop).unwrap();
|
||||
});
|
||||
}
|
||||
pub fn stop(&self) {
|
||||
let tx = self.tx.clone();
|
||||
thread::spawn(move || {
|
||||
tx.send(UdpCommand::Stop).unwrap();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref UDP_CLIENT: Arc<Mutex<Option<UdpClient>>> = Arc::new(Mutex::new(None));
|
||||
static ref UDP_CLIENT: Arc<Mutex<Option<UdpClient>>> = Arc::new(Mutex::new(None));
|
||||
}
|
||||
|
||||
pub fn start(ctx: Context, address: String) -> &'static str {
|
||||
let (tx, rx): (Sender<UdpCommand>, Receiver<UdpCommand>) = mpsc::channel();
|
||||
let (tx, rx): (Sender<UdpCommand>, Receiver<UdpCommand>) = mpsc::channel();
|
||||
|
||||
let client = UdpClient { tx };
|
||||
client.start(address, rx, ctx);
|
||||
let client = UdpClient { tx };
|
||||
client.start(address, rx, ctx);
|
||||
|
||||
let mut client_guard = UDP_CLIENT.lock().unwrap();
|
||||
*client_guard = Some(client);
|
||||
let mut client_guard = UDP_CLIENT.lock().unwrap();
|
||||
*client_guard = Some(client);
|
||||
|
||||
"Starting UDP Client"
|
||||
"Starting UDP Client"
|
||||
}
|
||||
|
||||
pub fn send_payload(ctx: Context, payload: String) -> &'static str {
|
||||
if let Some(ref client) = *UDP_CLIENT.lock().unwrap() {
|
||||
client.send_payload(ctx, payload);
|
||||
} else {
|
||||
let _ = ctx.callback_null("UDP SOCKET ERROR", "UDP Socket is not running");
|
||||
}
|
||||
if let Some(ref client) = *UDP_CLIENT.lock().unwrap() {
|
||||
client.send_payload(ctx, payload);
|
||||
} else {
|
||||
let _ = ctx.callback_null("UDP SOCKET ERROR", "UDP Socket is not running");
|
||||
}
|
||||
|
||||
"Sending payload to UDP server"
|
||||
"Sending payload to UDP server"
|
||||
}
|
||||
|
||||
pub fn send_gps_cot(ctx: Context, cursor_over_time: cot::gps::ExternalPositionPayload) -> &'static str {
|
||||
let payload = cursor_over_time.to_cot().convert_to_xml();
|
||||
send_payload(ctx, payload);
|
||||
pub fn send_gps_cot(
|
||||
ctx: Context,
|
||||
cursor_over_time: cot::gps::ExternalPositionPayload,
|
||||
) -> &'static str {
|
||||
let payload = cursor_over_time.to_cot().convert_to_xml();
|
||||
send_payload(ctx, payload);
|
||||
|
||||
"Sending GPS Cursor Over Time to UDP server"
|
||||
"Sending GPS Cursor Over Time to UDP server"
|
||||
}
|
||||
|
||||
pub fn stop(ctx: Context) -> &'static str {
|
||||
if let Some(ref client) = *UDP_CLIENT.lock().unwrap() {
|
||||
client.stop();
|
||||
let _ = ctx.callback_null("UDP SOCKET", "EUD Disconnected");
|
||||
} else {
|
||||
let _ = ctx.callback_null("UDP SOCKET ERROR", "UDP Socket is not running");
|
||||
}
|
||||
if let Some(ref client) = *UDP_CLIENT.lock().unwrap() {
|
||||
client.stop();
|
||||
let _ = ctx.callback_null("UDP SOCKET", "EUD Disconnected");
|
||||
} else {
|
||||
let _ = ctx.callback_null("UDP SOCKET ERROR", "UDP Socket is not running");
|
||||
}
|
||||
|
||||
"Stopping UDP Client"
|
||||
"Stopping UDP Client"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user