consume prototype

This commit is contained in:
nora 2022-03-01 21:52:00 +01:00
parent beb2187cd6
commit 93ce632b5d
21 changed files with 328 additions and 108 deletions

View file

@ -1,9 +1,13 @@
use crate::{newtype_id, GlobalData, Handle, Queue};
use crate::methods::Method;
use crate::{methods, newtype_id, GlobalData, Handle, Queue};
use bytes::Bytes;
use parking_lot::Mutex;
use smallvec::SmallVec;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::sync::mpsc;
newtype_id!(pub ConnectionId);
newtype_id!(pub ChannelId);
@ -48,14 +52,25 @@ pub struct Connection {
pub global_data: GlobalData,
pub channels: HashMap<ChannelNum, ChannelHandle>,
pub exclusive_queues: Vec<Queue>,
_method_queue: MethodSender,
}
#[derive(Debug)]
pub enum QueuedMethod {
Normal(Method),
WithContent(Method, ContentHeader, SmallVec<[Bytes; 1]>),
}
pub type MethodSender = mpsc::Sender<(ChannelNum, QueuedMethod)>;
pub type MethodReceiver = mpsc::Receiver<(ChannelNum, QueuedMethod)>;
impl Connection {
#[must_use]
pub fn new_handle(
id: ConnectionId,
peer_addr: SocketAddr,
global_data: GlobalData,
method_queue: MethodSender,
) -> ConnectionHandle {
Arc::new(Mutex::new(Self {
id,
@ -63,6 +78,7 @@ impl Connection {
global_data,
channels: HashMap::new(),
exclusive_queues: vec![],
_method_queue: method_queue,
}))
}
@ -77,24 +93,27 @@ pub type ChannelHandle = Handle<Channel>;
#[derive(Debug)]
pub struct Channel {
pub id: ChannelId,
pub num: u16,
pub num: ChannelNum,
pub connection: ConnectionHandle,
pub global_data: GlobalData,
method_queue: MethodSender,
}
impl Channel {
#[must_use]
pub fn new_handle(
id: ChannelId,
num: u16,
num: ChannelNum,
connection: ConnectionHandle,
global_data: GlobalData,
method_queue: MethodSender,
) -> ChannelHandle {
Arc::new(Mutex::new(Self {
id,
num,
connection,
global_data,
method_queue,
}))
}
@ -102,4 +121,19 @@ impl Channel {
let mut global_data = self.global_data.lock();
global_data.channels.remove(&self.id);
}
pub fn queue_method(&self, method: QueuedMethod) {
// todo: this is a horrible hack around the lock chaos
self.method_queue
.try_send((self.num, method))
.expect("could not send method to channel, RIP");
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ContentHeader {
pub class_id: u16,
pub weight: u16,
pub body_size: u64,
pub property_fields: methods::Table,
}

12
amqp_core/src/consumer.rs Normal file
View file

@ -0,0 +1,12 @@
use crate::{newtype_id, ChannelHandle};
newtype_id!(
pub ConsumerId
);
#[derive(Debug)]
pub struct Consumer {
pub id: ConsumerId,
pub tag: String,
pub channel: ChannelHandle,
}

View file

@ -1,6 +1,7 @@
#![warn(rust_2018_idioms)]
pub mod connection;
pub mod consumer;
pub mod error;
mod macros;
pub mod message;
@ -13,6 +14,7 @@ use connection::{ChannelId, ConnectionId};
use parking_lot::Mutex;
use std::collections::HashMap;
use std::sync::Arc;
use uuid::Uuid;
type Handle<T> = Arc<Mutex<T>>;
@ -48,3 +50,7 @@ pub struct GlobalDataInner {
/// Todo: This is just for testing and will be removed later!
pub default_exchange: HashMap<String, Queue>,
}
pub fn random_uuid() -> Uuid {
Uuid::from_bytes(rand::random())
}

View file

@ -1,6 +1,7 @@
#[macro_export]
macro_rules! newtype_id {
($vis:vis $name:ident) => {
($(#[$meta:meta])* $vis:vis $name:ident) => {
$(#[$meta])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
$vis struct $name(::uuid::Uuid);

View file

@ -1,6 +1,4 @@
#![allow(dead_code)]
use crate::methods;
use crate::connection::ContentHeader;
use crate::newtype_id;
use bytes::Bytes;
use smallvec::SmallVec;
@ -13,7 +11,7 @@ newtype_id!(pub MessageId);
#[derive(Debug)]
pub struct RawMessage {
pub id: MessageId,
pub properties: methods::Table,
pub header: ContentHeader,
pub routing: RoutingInformation,
pub content: SmallVec<[Bytes; 1]>,
}

View file

@ -1,6 +1,8 @@
use crate::consumer::Consumer;
use crate::message::Message;
use crate::{newtype, newtype_id, ChannelId};
use parking_lot::Mutex;
use std::borrow::Borrow;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
@ -14,6 +16,12 @@ newtype!(
pub QueueName: Arc<str>
);
impl Borrow<str> for QueueName {
fn borrow(&self) -> &str {
std::borrow::Borrow::borrow(&self.0)
}
}
#[derive(Debug)]
pub struct RawQueue {
pub id: QueueId,
@ -25,6 +33,7 @@ pub struct RawQueue {
/// The queue can always be manually deleted.
/// If auto-delete is enabled, it keeps track of the consumer count.
pub deletion: QueueDeletion,
pub consumers: Mutex<Vec<Consumer>>,
}
#[derive(Debug)]