some cleanup

This commit is contained in:
nora 2022-02-26 22:26:35 +01:00
parent 8532d454c3
commit 6d944e1265
12 changed files with 190 additions and 151 deletions

View file

@ -0,0 +1,99 @@
use crate::{newtype_id, GlobalData, Handle, Queue};
use parking_lot::Mutex;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::net::SocketAddr;
use std::sync::Arc;
newtype_id!(pub ConnectionId);
newtype_id!(pub ChannelId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ChannelNum(u16);
impl ChannelNum {
pub fn new(num: u16) -> Self {
Self(num)
}
pub fn num(self) -> u16 {
self.0
}
pub fn is_zero(self) -> bool {
self.0 == 0
}
pub fn zero() -> Self {
Self(0)
}
}
impl Display for ChannelNum {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
pub type ConnectionHandle = Handle<Connection>;
#[derive(Debug)]
pub struct Connection {
pub id: ConnectionId,
pub peer_addr: SocketAddr,
pub global_data: GlobalData,
pub channels: HashMap<u16, ChannelHandle>,
pub exclusive_queues: Vec<Queue>,
}
impl Connection {
pub fn new_handle(
id: ConnectionId,
peer_addr: SocketAddr,
global_data: GlobalData,
) -> ConnectionHandle {
Arc::new(Mutex::new(Self {
id,
peer_addr,
global_data,
channels: HashMap::new(),
exclusive_queues: vec![],
}))
}
pub fn close(&self) {
let mut global_data = self.global_data.lock();
global_data.connections.remove(&self.id);
}
}
pub type ChannelHandle = Handle<Channel>;
#[derive(Debug)]
pub struct Channel {
pub id: ChannelId,
pub num: u16,
pub connection: ConnectionHandle,
pub global_data: GlobalData,
}
impl Channel {
pub fn new_handle(
id: ChannelId,
num: u16,
connection: ConnectionHandle,
global_data: GlobalData,
) -> ChannelHandle {
Arc::new(Mutex::new(Self {
id,
num,
connection,
global_data,
}))
}
pub fn close(&self) {
let mut global_data = self.global_data.lock();
global_data.channels.remove(&self.id);
}
}

View file

@ -1,16 +1,18 @@
#![warn(rust_2018_idioms)]
pub mod connection;
pub mod error;
mod macros;
pub mod message;
pub mod methods;
pub mod queue;
use crate::queue::Queue;
use crate::connection::{ChannelHandle, ConnectionHandle};
use crate::queue::{Queue, QueueId};
use connection::{ChannelId, ConnectionId};
use parking_lot::Mutex;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use uuid::Uuid;
type Handle<T> = Arc<Mutex<T>>;
@ -40,83 +42,9 @@ impl GlobalData {
#[derive(Debug)]
pub struct GlobalDataInner {
pub connections: HashMap<Uuid, ConnectionHandle>,
pub channels: HashMap<Uuid, ChannelHandle>,
pub queues: HashMap<Uuid, Queue>,
pub connections: HashMap<ConnectionId, ConnectionHandle>,
pub channels: HashMap<ChannelId, ChannelHandle>,
pub queues: HashMap<QueueId, Queue>,
/// Todo: This is just for testing and will be removed later!
pub default_exchange: HashMap<String, Queue>,
}
pub type ConnectionHandle = Handle<Connection>;
#[derive(Debug)]
pub struct Connection {
pub id: Uuid,
pub peer_addr: SocketAddr,
pub global_data: GlobalData,
pub channels: HashMap<u16, ChannelHandle>,
pub exclusive_queues: Vec<Queue>,
}
impl Connection {
pub fn new_handle(
id: Uuid,
peer_addr: SocketAddr,
global_data: GlobalData,
) -> ConnectionHandle {
Arc::new(Mutex::new(Self {
id,
peer_addr,
global_data,
channels: HashMap::new(),
exclusive_queues: vec![],
}))
}
pub fn close(&self) {
let mut global_data = self.global_data.lock();
global_data.connections.remove(&self.id);
}
}
pub type ChannelHandle = Handle<Channel>;
#[derive(Debug)]
pub struct Channel {
pub id: Uuid,
pub num: u16,
pub connection: ConnectionHandle,
pub global_data: GlobalData,
}
impl Channel {
pub fn new_handle(
id: Uuid,
num: u16,
connection: ConnectionHandle,
global_data: GlobalData,
) -> ChannelHandle {
Arc::new(Mutex::new(Self {
id,
num,
connection,
global_data,
}))
}
pub fn close(&self) {
let mut global_data = self.global_data.lock();
global_data.channels.remove(&self.id);
}
}
pub fn gen_uuid() -> Uuid {
Uuid::from_bytes(rand::random())
}
#[macro_export]
macro_rules! amqp_todo {
() => {
return Err(::amqp_core::error::ConException::NotImplemented.into())
};
}

32
amqp_core/src/macros.rs Normal file
View file

@ -0,0 +1,32 @@
#[macro_export]
macro_rules! newtype_id {
($vis:vis $name:ident) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
$vis struct $name(::uuid::Uuid);
impl $name {
pub fn random() -> Self {
::rand::random()
}
}
impl ::std::fmt::Display for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
self.0.fmt(f)
}
}
impl ::rand::prelude::Distribution<$name> for ::rand::distributions::Standard {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> $name {
$name(::uuid::Uuid::from_bytes(rng.gen()))
}
}
};
}
#[macro_export]
macro_rules! amqp_todo {
() => {
return Err(::amqp_core::error::ConException::NotImplemented.into())
};
}

View file

@ -1,16 +1,18 @@
#![allow(dead_code)]
use crate::methods;
use crate::newtype_id;
use bytes::Bytes;
use smallvec::SmallVec;
use std::sync::Arc;
use uuid::Uuid;
pub type Message = Arc<RawMessage>;
newtype_id!(pub MessageId);
#[derive(Debug)]
pub struct RawMessage {
pub id: Uuid,
pub id: MessageId,
pub properties: methods::Table,
pub routing: RoutingInformation,
pub content: SmallVec<[Bytes; 1]>,

View file

@ -1,18 +1,20 @@
use crate::message::Message;
use crate::{newtype_id, ChannelId};
use parking_lot::Mutex;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
use uuid::Uuid;
pub type Queue = Arc<RawQueue>;
newtype_id!(pub QueueId);
#[derive(Debug)]
pub struct RawQueue {
pub id: Uuid,
pub id: QueueId,
pub name: String,
pub messages: Mutex<Vec<Message>>, // use a concurrent linked list???
pub durable: bool,
pub exclusive: Option<Uuid>,
pub exclusive: Option<ChannelId>,
/// Whether the queue will automatically be deleted when no consumers uses it anymore.
/// The queue can always be manually deleted.
/// If auto-delete is enabled, it keeps track of the consumer count.