-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ed2bcb
commit 3bf138f
Showing
7 changed files
with
414 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ pub mod content; | |
pub mod exchange; | ||
pub mod open_close; | ||
pub mod queue; | ||
pub mod router; | ||
pub mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,134 @@ | ||
use log::warn; | ||
use metalmq_codec::codec::Frame; | ||
use metalmq_codec::frame; | ||
use uuid::Uuid; | ||
|
||
use crate::client::channel::types::Channel; | ||
use crate::error::Result; | ||
use crate::error::{ChannelError, Result}; | ||
use crate::exchange::manager::{BindQueueCommand, UnbindQueueCommand}; | ||
use crate::{exchange, queue}; | ||
|
||
impl Channel { | ||
pub async fn queue_declare(&mut self, channel: Channel, args: frame::QueueDeclareArgs) -> Result<()> { | ||
pub async fn handle_queue_declare(&mut self, args: frame::QueueDeclareArgs) -> Result<()> { | ||
let mut queue_name = args.name.clone(); | ||
if queue_name.is_empty() { | ||
queue_name = Uuid::new_v4().hyphenated().to_string(); | ||
} | ||
|
||
let passive = args.flags.contains(frame::QueueDeclareFlags::PASSIVE); | ||
|
||
let cmd = queue::manager::QueueDeclareCommand { | ||
conn_id: self.source_connection.clone(), | ||
channel: self.number, | ||
queue: args.into(), | ||
passive, | ||
}; | ||
let (message_count, consumer_count) = queue::manager::declare_queue(&self.qm, cmd).await?; | ||
|
||
self.send_frame(Frame::Frame( | ||
frame::QueueDeclareOkArgs::default() | ||
.name(&queue_name) | ||
.message_count(message_count) | ||
.consumer_count(consumer_count) | ||
.frame(self.number), | ||
)) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn queue_bind(&mut self, channel: Channel, args: frame::QueueBindArgs) -> Result<()> { | ||
Ok(()) | ||
pub async fn handle_queue_bind(&mut self, args: frame::QueueBindArgs) -> Result<()> { | ||
let cmd = queue::manager::GetQueueSinkQuery { | ||
channel: self.number, | ||
queue_name: args.queue_name.clone(), | ||
}; | ||
|
||
match queue::manager::get_command_sink(&self.qm, cmd).await { | ||
Ok(sink) => { | ||
let cmd = BindQueueCommand { | ||
conn_id: self.source_connection.clone(), | ||
channel: self.number, | ||
exchange_name: args.exchange_name, | ||
queue_name: args.queue_name, | ||
routing_key: args.routing_key, | ||
args: args.args, | ||
queue_sink: sink, | ||
}; | ||
|
||
// TODO now we can use let patter = x else {} so we can drop this macro | ||
exchange::manager::bind_queue(&self.em, cmd).await?; | ||
|
||
self.send_frame(Frame::Frame(frame::queue_bind_ok(self.number))).await | ||
} | ||
Err(e) => { | ||
warn!("{:?}", e); | ||
|
||
ChannelError::NotFound.to_result(self.number, frame::QUEUE_BIND, "Queue not found") | ||
} | ||
} | ||
} | ||
|
||
pub async fn queue_delete(&mut self, channel: Channel, args: frame::QueueDeleteArgs) -> Result<()> { | ||
pub async fn handle_queue_delete(&mut self, args: frame::QueueDeleteArgs) -> Result<()> { | ||
let cmd = queue::manager::QueueDeleteCommand { | ||
conn_id: self.source_connection.clone(), | ||
channel: self.number, | ||
queue_name: args.queue_name, | ||
if_unused: args.flags.contains(frame::QueueDeleteFlags::IF_UNUSED), | ||
if_empty: args.flags.contains(frame::QueueDeleteFlags::IF_EMPTY), | ||
}; | ||
|
||
let message_count = queue::manager::delete_queue(&self.qm, cmd).await?; | ||
|
||
self.send_frame(Frame::Frame( | ||
frame::QueueDeleteOkArgs::default() | ||
.message_count(message_count) | ||
.frame(self.number), | ||
)) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn queue_unbind(&mut self, channel: Channel, args: frame::QueueUnbindArgs) -> Result<()> { | ||
pub async fn handle_queue_unbind(&mut self, args: frame::QueueUnbindArgs) -> Result<()> { | ||
let cmd = UnbindQueueCommand { | ||
conn_id: self.source_connection.clone(), | ||
channel: self.number, | ||
exchange_name: args.exchange_name, | ||
queue_name: args.queue_name, | ||
routing_key: args.routing_key, | ||
}; | ||
|
||
exchange::manager::unbind_queue(&self.em, cmd).await?; | ||
|
||
self.send_frame(Frame::Frame(frame::AMQPFrame::Method( | ||
self.number, | ||
frame::QUEUE_UNBIND_OK, | ||
frame::MethodFrameArgs::QueueUnbindOk, | ||
))) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn queue_purge(&mut self, channel: Channel, args: frame::QueuePurgeArgs) -> Result<()> { | ||
pub async fn handle_queue_purge(&mut self, args: frame::QueuePurgeArgs) -> Result<()> { | ||
// Purge the not-yet sent messages from the queue. Queue gives back the number of purged | ||
// messages, so this operation is sync. | ||
|
||
let cmd = queue::manager::GetQueueSinkQuery { | ||
channel: self.number, | ||
queue_name: args.queue_name, | ||
}; | ||
|
||
let queue_sink = queue::manager::get_command_sink(&self.qm, cmd).await?; | ||
let message_count = queue::handler::purge(self.source_connection.clone(), self.number, &queue_sink).await?; | ||
|
||
self.send_frame(Frame::Frame( | ||
frame::QueuePurgeOkArgs::default() | ||
.message_count(message_count) | ||
.frame(self.number), | ||
)) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.