From 9a26dad304a66320ff6d888bb20845d5f38814ac Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Wed, 16 Apr 2025 15:31:23 +0200 Subject: [PATCH] use channels in pm3 function --- src/main.rs | 15 ++++++++++++--- src/pm3.rs | 33 ++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9dd4a7d..3fa406a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,18 @@ -use pm3::run_pm3; +use pm3::{pm3_mock, run_pm3}; +use std::{sync::mpsc::channel, thread}; +mod id_store; mod parser; mod pm3; -mod id_store; fn main() { - run_pm3().unwrap(); + let (sender, receiver) = channel(); + thread::spawn(move || { + // run_pm3(sender); + pm3_mock(sender); + }); + + while true { + println!("{}", receiver.recv().unwrap()); + } } diff --git a/src/pm3.rs b/src/pm3.rs index ba00350..73548a7 100644 --- a/src/pm3.rs +++ b/src/pm3.rs @@ -1,8 +1,10 @@ use std::error::Error; -use std::process::{Command, Stdio}; use std::io::{self, BufRead}; +use std::process::{Command, Stdio}; +use std::sync::mpsc::Sender; +use std::{thread, time}; -pub fn run_pm3() -> Result<(), Box> { +pub fn run_pm3(sender: Sender) -> Result<(), Box> { let mut cmd = Command::new("stdbuf") .arg("-oL") .arg("pm3") @@ -19,11 +21,16 @@ pub fn run_pm3() -> Result<(), Box> { Ok(line) => { let parse_result = super::parser::parse_line(&line); if let Some(uid) = parse_result { - println!("UID: {}",uid); + match sender.send(uid) { + Ok(_) => {} + Err(e) => { + eprintln!("Failed to send to channel: {}", e); + } + } } } Err(e) => { - eprintln!("{}",e); + eprintln!("{}", e); } } } @@ -32,7 +39,23 @@ pub fn run_pm3() -> Result<(), Box> { if status.success() { Ok(()) - }else { + } else { Err("pm3 had non zero exit code".into()) } } + +pub fn pm3_mock(sender: Sender) -> Result<(), Box> { + #![allow(while_true)] + while true { + match sender.send("F1409618".to_owned()) { + Ok(()) => {} + Err(e) => { + eprintln!("Failed to send to channel: {}", e); + } + } + + thread::sleep(time::Duration::from_secs(2)); + } + + Ok(()) +}