From 9e9699c45914969387557a3900473239de8c3074 Mon Sep 17 00:00:00 2001 From: John Goerzen Date: Sun, 20 Sep 2020 23:28:34 -0500 Subject: [PATCH] updates --- src/main.rs | 9 ++++++--- src/xb.rs | 10 +++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 37da8f1..d5cf473 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,7 +80,7 @@ fn main() { info!("xbnet starting"); let xbser = ser::XBSer::new(opt.port).expect("Failed to initialize serial port"); - let (xb, xbeesender) = xb::XB::new(xbser, opt.initfile); + let (xb, xbeesender, writerthread) = xb::XB::new(xbser, opt.initfile); let mut xbreframer = xbrx::XBReframer::new(); @@ -95,8 +95,11 @@ fn main() { }, Command::Pipe{dest} => { let dest_u64:u64 = u64::from_str_radix(&dest, 16).expect("Invalid destination"); - thread::spawn(move || pipe::stdin_processor(dest_u64, 1600, xbeesender).expect("Failure in stdin_processor")); - pipe::stdout_processor(&mut xbreframer, &xb.ser).expect("Failure in stdout_processor"); + thread::spawn(move || pipe::stdout_processor(&mut xbreframer, &xb.ser).expect("Failure in stdout_processor")); + pipe::stdin_processor(dest_u64, 1600, xbeesender).expect("Failure in stdin_processor"); + // Make sure queued up data is sent + writerthread.join(); + }, } } diff --git a/src/xb.rs b/src/xb.rs index d107f07..3b76431 100644 --- a/src/xb.rs +++ b/src/xb.rs @@ -44,7 +44,6 @@ pub enum XBTX { } /// Main XBeeNet struct -#[derive(Clone)] pub struct XB { pub ser: XBSer, @@ -70,14 +69,14 @@ pub fn assert_response(resp: String, expected: String) -> io::Result<()> { impl XB { /** Creates a new XB. Returns an instance to be used for reading, as well as a separate sender to be used in a separate thread to handle - outgoing frames. This will spawn a thread to handle the writing to XBee. + outgoing frames. This will spawn a thread to handle the writing to XBee, which is returned. If initfile is given, its lines will be sent to the radio, one at a time, expecting OK after each one, to initialize it. May panic if an error occurs during initialization. */ - pub fn new(mut ser: XBSer, initfile: Option) -> (XB, crossbeam_channel::Sender) { + pub fn new(mut ser: XBSer, initfile: Option) -> (XB, crossbeam_channel::Sender, thread::JoinHandle<()>) { // FIXME: make this maximum of 5 configurable let (writertx, writerrx) = crossbeam_channel::bounded(5); @@ -144,14 +143,15 @@ impl XB { debug!("Radio configuration complete"); let ser2 = ser.clone(); - thread::spawn(move || writerthread(ser2, maxpacketsize, writerrx)); + let writerthread = thread::spawn(move || writerthread(ser2, maxpacketsize, writerrx)); (XB { ser, mymac, maxpacketsize, - }, writertx) + }, writertx, writerthread) } + } fn writerthread(ser: XBSer, maxpacketsize: usize,