1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::{
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use crate::synchronized_poller_macro;
use futures::{executor::block_on, Future};
use super::SynchronizedConfig;
use crate::{
builder::ReceiverSubscriberBuilder,
error::{Error, SendError, StdSyncSendError},
receiver::{Action, Event, ReciveTypedReceiver, SendTypedReceiver, SendUntypedReceiver},
receivers::{fix_type, Request},
Bus, Message, SynchronizedHandler, Untyped,
};
use tokio::sync::{mpsc, Mutex};
synchronized_poller_macro! {
T,
SynchronizedHandler,
|mid, msg, bus, ut: Arc<Mutex<T>>| async move {
(mid, tokio::task::spawn_blocking(move || {
block_on(ut.lock()).handle(msg, &bus)
})
.await
.unwrap())
},
|bus, ut: Arc<Mutex<T>>| async move {
tokio::task::spawn_blocking(move || block_on(ut.lock()).sync(&bus))
.await
.unwrap()
}
}
pub struct SynchronizedSync<M, R, E>
where
M: Message,
R: Message,
E: StdSyncSendError,
{
tx: mpsc::UnboundedSender<Request<M>>,
srx: parking_lot::Mutex<mpsc::UnboundedReceiver<Event<R, E>>>,
}
impl<T, M, R, E> ReceiverSubscriberBuilder<T, M, R, E> for SynchronizedSync<M, R, E>
where
T: SynchronizedHandler<M, Response = R, Error = E> + 'static,
R: Message,
M: Message,
E: StdSyncSendError,
{
type Config = SynchronizedConfig;
fn build(
_cfg: Self::Config,
) -> (
Self,
Box<
dyn FnOnce(Untyped) -> Box<dyn FnOnce(Bus) -> Pin<Box<dyn Future<Output = ()> + Send>>>,
>,
) {
let (stx, srx) = mpsc::unbounded_channel();
let (tx, rx) = mpsc::unbounded_channel();
let poller = Box::new(move |ut| {
Box::new(move |bus| {
Box::pin(synchronized_poller::<T, M, R>(rx, bus, ut, stx))
as Pin<Box<dyn Future<Output = ()> + Send>>
}) as Box<dyn FnOnce(Bus) -> Pin<Box<dyn Future<Output = ()> + Send>>>
});
(
SynchronizedSync::<M, R, E> {
tx,
srx: parking_lot::Mutex::new(srx),
},
poller,
)
}
}
impl<M, R, E> SendUntypedReceiver for SynchronizedSync<M, R, E>
where
M: Message,
R: Message,
E: StdSyncSendError,
{
fn send(&self, msg: Action, _bus: &Bus) -> Result<(), SendError<Action>> {
match self.tx.send(Request::Action(msg)) {
Ok(_) => Ok(()),
Err(mpsc::error::SendError(Request::Action(msg))) => Err(SendError::Closed(msg)),
_ => unimplemented!(),
}
}
}
impl<M, R, E> SendTypedReceiver<M> for SynchronizedSync<M, R, E>
where
M: Message,
R: Message,
E: StdSyncSendError,
{
fn send(&self, mid: u64, m: M, _bus: &Bus) -> Result<(), SendError<M>> {
match self.tx.send(Request::Request(mid, m)) {
Ok(_) => Ok(()),
Err(mpsc::error::SendError(Request::Request(_, msg))) => Err(SendError::Closed(msg)),
_ => unimplemented!(),
}
}
}
impl<M, R, E> ReciveTypedReceiver<R, E> for SynchronizedSync<M, R, E>
where
M: Message,
R: Message,
E: StdSyncSendError,
{
fn poll_events(&self, ctx: &mut Context<'_>, _bus: &Bus) -> Poll<Event<R, E>> {
let poll = self.srx.lock().poll_recv(ctx);
match poll {
Poll::Pending => Poll::Pending,
Poll::Ready(Some(event)) => Poll::Ready(event),
Poll::Ready(None) => Poll::Ready(Event::Exited),
}
}
}