Remove pub
declarations and move to posix::*
This is in order to prepare also for SysV message queues.
This commit is contained in:
parent
606b4de524
commit
c2ad74ff15
7 changed files with 24 additions and 18 deletions
86
src/recv.rs
86
src/recv.rs
|
@ -1,86 +0,0 @@
|
|||
use anyhow::Result;
|
||||
use chrono::{DateTime, Local};
|
||||
use clap::Clap;
|
||||
use humantime::Duration;
|
||||
use posixmq::PosixMq;
|
||||
use std::str;
|
||||
|
||||
/// Receive and print a message from a message queue
|
||||
#[derive(Clap, Debug)]
|
||||
pub struct Recv {
|
||||
/// Do not block
|
||||
#[clap(short, long)]
|
||||
pub non_blocking: bool,
|
||||
/// Print messages as they are received
|
||||
#[clap(short, long)]
|
||||
pub follow: bool,
|
||||
/// Print a timestamp before each message
|
||||
#[clap(short, long)]
|
||||
pub timestamp: bool,
|
||||
/// Timeout, example "5h 23min 42ms"
|
||||
#[clap(short = 'o', long, conflicts_with = "deadline")]
|
||||
pub timeout: Option<String>,
|
||||
/// Deadline until messages are received (format: "%Y-%m-%d %H:%M:%S")
|
||||
#[clap(short, long, conflicts_with = "timeout")]
|
||||
pub deadline: Option<String>,
|
||||
/// Name of the queue
|
||||
#[clap(value_name = "QUEUE")]
|
||||
pub queue: String,
|
||||
}
|
||||
|
||||
fn print_message(verbose: bool, priority: u32, length: usize, timestamp: bool, msg: &str) {
|
||||
if verbose {
|
||||
println!("Priority: {}, length: {}", priority, length);
|
||||
}
|
||||
|
||||
if timestamp {
|
||||
println!("{}", Local::now());
|
||||
}
|
||||
|
||||
println!("{}", msg);
|
||||
}
|
||||
|
||||
impl Recv {
|
||||
fn receive(&self, verbose: bool, mq: &PosixMq) -> Result<()> {
|
||||
let mut buf = vec![0; mq.attributes()?.max_msg_len];
|
||||
|
||||
if let Some(timeout) = &self.timeout {
|
||||
let (prio, len) = mq.recv_timeout(&mut buf, *timeout.parse::<Duration>()?)?;
|
||||
|
||||
print_message(verbose, prio, len, self.timestamp, str::from_utf8(&buf)?);
|
||||
} else if let Some(deadline) = &self.deadline {
|
||||
let (prio, len) = mq.recv_deadline(
|
||||
&mut buf,
|
||||
DateTime::parse_from_str(deadline, "%Y-%m-%d %H:%M:%S")?.into(),
|
||||
)?;
|
||||
|
||||
print_message(verbose, prio, len, self.timestamp, str::from_utf8(&buf)?);
|
||||
} else {
|
||||
let (prio, len) = mq.recv(&mut buf)?;
|
||||
|
||||
print_message(verbose, prio, len, self.timestamp, str::from_utf8(&buf)?);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run(&self, verbose: bool) -> Result<()> {
|
||||
let mq = &mut posixmq::OpenOptions::readonly();
|
||||
|
||||
if self.non_blocking {
|
||||
mq.nonblocking();
|
||||
}
|
||||
|
||||
let mq = mq.open(&self.queue)?;
|
||||
|
||||
if self.follow {
|
||||
loop {
|
||||
self.receive(verbose, &mq)?;
|
||||
}
|
||||
} else {
|
||||
self.receive(verbose, &mq)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue