Alex Mikhalev
4 years ago
4 changed files with 158 additions and 145 deletions
@ -0,0 +1,94 @@ |
|||||||
|
use super::{ |
||||||
|
error::{BrokerError, BrokerResult}, |
||||||
|
registry::{BasicMessageType, Registry}, |
||||||
|
request::{BrokerRequest, BrokerRequestInternal, MakeBrokerRequest, RequestHandler}, |
||||||
|
}; |
||||||
|
|
||||||
|
use crate::{mpsc, Message}; |
||||||
|
|
||||||
|
use std::marker::PhantomData; |
||||||
|
use tokio::sync::oneshot; |
||||||
|
|
||||||
|
pub type ClaimReceiverRequest<T> = BrokerRequest<ClaimReceiver<T>>; |
||||||
|
|
||||||
|
pub enum ClaimReceiverError { |
||||||
|
AlreadyClaimed, |
||||||
|
Broker(BrokerError), |
||||||
|
} |
||||||
|
|
||||||
|
pub struct ClaimReceiver<T: Message> { |
||||||
|
msg_type: BasicMessageType<T>, |
||||||
|
} |
||||||
|
|
||||||
|
impl<T: Message> Default for ClaimReceiver<T> { |
||||||
|
fn default() -> Self { |
||||||
|
Self { |
||||||
|
msg_type: BasicMessageType::<T>::default(), |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
impl<T: Message> RequestHandler for ClaimReceiver<T> { |
||||||
|
type Response = Result<mpsc::Receiver<T>, ClaimReceiverError>; |
||||||
|
|
||||||
|
fn handle(self, registry: &mut Registry) -> Self::Response { |
||||||
|
registry |
||||||
|
.get_mpsc_sender::<T>(&self.msg_type) |
||||||
|
.map_err(ClaimReceiverError::Broker) |
||||||
|
.and_then(|sender| { |
||||||
|
sender |
||||||
|
.claim_receiver() |
||||||
|
.ok_or(ClaimReceiverError::AlreadyClaimed) |
||||||
|
}) |
||||||
|
.map(mpsc::Receiver::new) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub struct SenderRequest<T, W> { |
||||||
|
msg_type: BasicMessageType<T>, |
||||||
|
response_tx: oneshot::Sender<BrokerResult<mpsc::Sender<T>>>, |
||||||
|
_wait: PhantomData<W>, |
||||||
|
} |
||||||
|
|
||||||
|
pub trait GetSenderWait: Send { |
||||||
|
const WAIT: bool; |
||||||
|
} |
||||||
|
|
||||||
|
pub struct NoWait; |
||||||
|
impl GetSenderWait for NoWait { |
||||||
|
const WAIT: bool = false; |
||||||
|
} |
||||||
|
pub type SenderRequestNoWait<T> = SenderRequest<T, NoWait>; |
||||||
|
|
||||||
|
pub struct Wait; |
||||||
|
impl GetSenderWait for Wait { |
||||||
|
const WAIT: bool = true; |
||||||
|
} |
||||||
|
pub type SenderRequestWait<T> = SenderRequest<T, Wait>; |
||||||
|
|
||||||
|
impl<T: Message, W: GetSenderWait> MakeBrokerRequest for SenderRequest<T, W> { |
||||||
|
type Response = BrokerResult<mpsc::Sender<T>>; |
||||||
|
|
||||||
|
fn create(response_tx: oneshot::Sender<Self::Response>) -> Box<Self> { |
||||||
|
Box::new(Self { |
||||||
|
msg_type: BasicMessageType::default(), |
||||||
|
response_tx, |
||||||
|
_wait: PhantomData, |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
impl<T: Message, W: GetSenderWait> BrokerRequestInternal for SenderRequest<T, W> { |
||||||
|
fn run_request(self: Box<Self>, registry: &mut Registry) { |
||||||
|
let response = match registry.get_mpsc_sender::<T>(&self.msg_type) { |
||||||
|
Ok(sender) => Ok(if W::WAIT { |
||||||
|
sender.clone_sender_or_wait(self.response_tx); |
||||||
|
return; |
||||||
|
} else { |
||||||
|
sender.clone_sender() |
||||||
|
}), |
||||||
|
Err(err) => Err(err), |
||||||
|
}; |
||||||
|
let _ = self.response_tx.send(response); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,53 @@ |
|||||||
|
use super::{ |
||||||
|
error::BrokerResult, |
||||||
|
registry::{BasicMessageType, Registry}, |
||||||
|
request::{BrokerRequest, RequestHandler}, |
||||||
|
}; |
||||||
|
use crate::{pub_sub, Message}; |
||||||
|
|
||||||
|
pub type SubscribeRequest<T> = BrokerRequest<Subscribe<T>>; |
||||||
|
pub struct Subscribe<T: Message> { |
||||||
|
msg_type: BasicMessageType<T>, |
||||||
|
} |
||||||
|
|
||||||
|
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<pub_sub::Subscription<T>>; |
||||||
|
|
||||||
|
fn handle(self, registry: &mut Registry) -> Self::Response { |
||||||
|
let sender = registry.get_broadcast_sender::<T>(&self.msg_type); |
||||||
|
sender.map(|sender| { |
||||||
|
let receiver = sender.subscribe(); |
||||||
|
pub_sub::Subscription::new(receiver) |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
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<pub_sub::Publication<T>>; |
||||||
|
|
||||||
|
fn handle(self, registry: &mut Registry) -> Self::Response { |
||||||
|
let sender = registry.get_broadcast_sender::<T>(&self.msg_type); |
||||||
|
sender.map(|sender| pub_sub::Publication::new(sender.clone())) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue