From 36d9e8c02c509dcfe0f0b312c879e8914d5d8d3a Mon Sep 17 00:00:00 2001 From: finga Date: Mon, 14 Jun 2021 23:00:06 +0200 Subject: [PATCH] Send a message to a queue --- src/cli.rs | 3 ++- src/main.rs | 2 ++ src/send.rs | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/send.rs diff --git a/src/cli.rs b/src/cli.rs index 5ef375c..8568937 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,4 @@ -use crate::{create::Create, unlink::Unlink, info::Info}; +use crate::{create::Create, info::Info, send::Send, unlink::Unlink}; use clap::{crate_authors, crate_version, AppSettings, Clap}; #[derive(Clap, Debug)] @@ -22,4 +22,5 @@ pub enum Command { Create(Create), Info(Info), Unlink(Unlink), + Send(Send), } diff --git a/src/main.rs b/src/main.rs index ec3e3c6..e361ffe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use clap::Clap; mod cli; mod create; mod info; +mod send; mod unlink; use cli::{Command, Opts}; @@ -15,6 +16,7 @@ fn main() -> Result<()> { Command::Create(c) => c.run(opts.verbose)?, Command::Info(i) => i.run()?, Command::Unlink(u) => u.run(opts.verbose)?, + Command::Send(s) => s.run(opts.verbose)?, } Ok(()) diff --git a/src/send.rs b/src/send.rs new file mode 100644 index 0000000..7b8a6e8 --- /dev/null +++ b/src/send.rs @@ -0,0 +1,38 @@ +use anyhow::Result; +use clap::Clap; + +/// Send a message to a message queue +#[derive(Clap, Debug)] +pub struct Send { + /// Use priority PRIO, PRIO >= 0 + #[clap(short, long, default_value = "0")] + pub priority: u32, + /// Do not block + #[clap(short, long)] + pub non_blocking: bool, + /// Name of the queue + #[clap(value_name = "QNAME")] + pub queue: String, + /// Message to be sent to the queue + #[clap(value_name = "MESSAGE")] + pub msg: String, +} + +impl Send { + pub fn run(&self, verbose: bool) -> Result<()> { + let mq = &mut posixmq::OpenOptions::writeonly(); + + if self.non_blocking { + mq.nonblocking(); + } + + mq.open(&self.queue)? + .send(self.priority, &self.msg.as_bytes())?; + + if verbose { + println!("Sent message: \"{}\" to queue: {}", &self.msg, &self.queue); + } + + Ok(()) + } +}