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
68
src/posix/send.rs
Normal file
68
src/posix/send.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
use anyhow::Result;
|
||||
use chrono::DateTime;
|
||||
use clap::Clap;
|
||||
use humantime::Duration;
|
||||
|
||||
/// Send a message to a message queue
|
||||
#[derive(Clap, Debug)]
|
||||
pub struct Send {
|
||||
/// Set a different priority, priority >= 0
|
||||
#[clap(short, long, default_value = "0")]
|
||||
pub priority: u32,
|
||||
/// Do not block
|
||||
#[clap(short, long)]
|
||||
pub non_blocking: bool,
|
||||
/// Timeout, example "5h 23min 42ms"
|
||||
#[clap(short = 'o', long, conflicts_with = "deadline")]
|
||||
pub timeout: Option<String>,
|
||||
/// Deadline until messages are sent (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,
|
||||
/// Message to be sent to the queue
|
||||
#[clap(value_name = "MESSAGE")]
|
||||
pub msg: String,
|
||||
}
|
||||
|
||||
fn print_verbose(verbose: bool, msg: &str, queue: &str) {
|
||||
if verbose {
|
||||
println!("Sent message: \"{}\" to queue: {}", &msg, &queue);
|
||||
}
|
||||
}
|
||||
|
||||
impl Send {
|
||||
pub fn run(&self, verbose: bool) -> Result<()> {
|
||||
let mq = &mut posixmq::OpenOptions::writeonly();
|
||||
|
||||
if self.non_blocking {
|
||||
mq.nonblocking();
|
||||
}
|
||||
|
||||
if let Some(timeout) = &self.timeout {
|
||||
mq.open(&self.queue)?.send_timeout(
|
||||
self.priority,
|
||||
&self.msg.as_bytes(),
|
||||
*timeout.parse::<Duration>()?,
|
||||
)?;
|
||||
|
||||
print_verbose(verbose, &self.msg, &self.queue);
|
||||
} else if let Some(deadline) = &self.deadline {
|
||||
mq.open(&self.queue)?.send_deadline(
|
||||
self.priority,
|
||||
&self.msg.as_bytes(),
|
||||
DateTime::parse_from_str(deadline, "%Y-%m-%d %H:%M:%S")?.into(),
|
||||
)?;
|
||||
|
||||
print_verbose(verbose, &self.msg, &self.queue);
|
||||
} else {
|
||||
mq.open(&self.queue)?
|
||||
.send(self.priority, &self.msg.as_bytes())?;
|
||||
|
||||
print_verbose(verbose, &self.msg, &self.queue);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue