|
|
|
@ -6,67 +6,88 @@ use super::{
@@ -6,67 +6,88 @@ use super::{
|
|
|
|
|
}; |
|
|
|
|
use crate::{Message, Publication, Subscription}; |
|
|
|
|
|
|
|
|
|
pub(crate) trait BrokerRequestInternal { |
|
|
|
|
fn handle_request(self: Box<Self>, registry: &mut Registry); |
|
|
|
|
pub trait BrokerRequestInternal { |
|
|
|
|
fn run_request(self: Box<Self>, registry: &mut Registry); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub(crate) trait BrokerRequest: BrokerRequestInternal {} |
|
|
|
|
impl<T: BrokerRequestInternal> BrokerRequest for T {} |
|
|
|
|
pub trait RequestHandler { |
|
|
|
|
type Response; |
|
|
|
|
|
|
|
|
|
pub(crate) type BrokerRequestBox = Box<dyn BrokerRequest + Send + Sync>; |
|
|
|
|
pub(crate) type BrokerRequestSender = mpsc::Sender<BrokerRequestBox>; |
|
|
|
|
fn handle(self, registry: &mut Registry) -> Self::Response; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub(crate) struct SubscribeRequest<T> { |
|
|
|
|
msg_type: BasicMessageType<T>, |
|
|
|
|
response_tx: oneshot::Sender<BrokerResult<Subscription<T>>>, |
|
|
|
|
pub struct BrokerRequest<H: RequestHandler> { |
|
|
|
|
handler: H, |
|
|
|
|
response_tx: oneshot::Sender<H::Response>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<T: Message> BrokerRequestInternal for SubscribeRequest<T> { |
|
|
|
|
fn handle_request(self: Box<Self>, registry: &mut Registry) { |
|
|
|
|
let sender = registry.get_sender_for_type::<T>(&self.msg_type); |
|
|
|
|
let subscription = sender.map(|sender| { |
|
|
|
|
let receiver = sender.subscribe(); |
|
|
|
|
Subscription::new(receiver) |
|
|
|
|
}); |
|
|
|
|
let _ = self.response_tx.send(subscription); |
|
|
|
|
impl<H: RequestHandler> BrokerRequestInternal for BrokerRequest<H> { |
|
|
|
|
fn run_request(self: Box<Self>, registry: &mut Registry) { |
|
|
|
|
let response = self.handler.handle(registry); |
|
|
|
|
let _ = self.response_tx.send(response); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<T: Message> SubscribeRequest<T> { |
|
|
|
|
pub(crate) fn new() -> (Box<Self>, oneshot::Receiver<BrokerResult<Subscription<T>>>) { |
|
|
|
|
impl<H: RequestHandler + Default> BrokerRequest<H> { |
|
|
|
|
fn create(response_tx: oneshot::Sender<H::Response>) -> Box<Self> { |
|
|
|
|
Box::new(Self { |
|
|
|
|
handler: H::default(), |
|
|
|
|
response_tx, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub(crate) fn new() -> (Box<Self>, oneshot::Receiver<H::Response>) { |
|
|
|
|
let (response_tx, response_rx) = oneshot::channel(); |
|
|
|
|
( |
|
|
|
|
Box::new(Self { |
|
|
|
|
msg_type: Default::default(), |
|
|
|
|
response_tx, |
|
|
|
|
}), |
|
|
|
|
response_rx, |
|
|
|
|
) |
|
|
|
|
(Self::create(response_tx), response_rx) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub(crate) struct AdvertiseRequest<T> { |
|
|
|
|
pub type BrokerRequestBox = Box<dyn BrokerRequestInternal + Send + Sync>; |
|
|
|
|
pub type BrokerRequestSender = mpsc::Sender<BrokerRequestBox>; |
|
|
|
|
|
|
|
|
|
pub type SubscribeRequest<T> = BrokerRequest<Subscribe<T>>; |
|
|
|
|
pub struct Subscribe<T: Message> { |
|
|
|
|
msg_type: BasicMessageType<T>, |
|
|
|
|
response_tx: oneshot::Sender<BrokerResult<Publication<T>>>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<T: Message> BrokerRequestInternal for AdvertiseRequest<T> { |
|
|
|
|
fn handle_request(self: Box<Self>, registry: &mut Registry) { |
|
|
|
|
impl<T: Message> Default for Subscribe<T> { |
|
|
|
|
fn default() -> Self { |
|
|
|
|
Self { |
|
|
|
|
msg_type: BasicMessageType::<T>::default(), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<T: Message> RequestHandler for Subscribe<T> { |
|
|
|
|
type Response = BrokerResult<Subscription<T>>; |
|
|
|
|
|
|
|
|
|
fn handle(self, registry: &mut Registry) -> Self::Response { |
|
|
|
|
let sender = registry.get_sender_for_type::<T>(&self.msg_type); |
|
|
|
|
let publication = sender.map(|sender| Publication::new(sender.clone())); |
|
|
|
|
let _ = self.response_tx.send(publication); |
|
|
|
|
sender.map(|sender| { |
|
|
|
|
let receiver = sender.subscribe(); |
|
|
|
|
Subscription::new(receiver) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<T: Message> AdvertiseRequest<T> { |
|
|
|
|
pub(crate) fn new() -> (Box<Self>, oneshot::Receiver<BrokerResult<Publication<T>>>) { |
|
|
|
|
let (response_tx, response_rx) = oneshot::channel(); |
|
|
|
|
( |
|
|
|
|
Box::new(Self { |
|
|
|
|
msg_type: Default::default(), |
|
|
|
|
response_tx, |
|
|
|
|
}), |
|
|
|
|
response_rx, |
|
|
|
|
) |
|
|
|
|
pub type AdvertiseRequest<T> = BrokerRequest<Advertise<T>>; |
|
|
|
|
pub struct Advertise<T: Message> { |
|
|
|
|
msg_type: BasicMessageType<T>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<T: Message> Default for Advertise<T> { |
|
|
|
|
fn default() -> Self { |
|
|
|
|
Self { |
|
|
|
|
msg_type: BasicMessageType::<T>::default(), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<T: Message> RequestHandler for Advertise<T> { |
|
|
|
|
type Response = BrokerResult<Publication<T>>; |
|
|
|
|
|
|
|
|
|
fn handle(self, registry: &mut Registry) -> Self::Response { |
|
|
|
|
let sender = registry.get_sender_for_type::<T>(&self.msg_type); |
|
|
|
|
sender.map(|sender| Publication::new(sender.clone())) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|