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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::fmt::Debug;
use futures::{future, prelude::*, stream::Fuse};
use tari_broadcast_channel::{bounded, Publisher, Subscriber};
#[derive(Debug)]
pub struct TopicPayload<T, M> {
topic: T,
message: M,
}
impl<T, M> TopicPayload<T, M> {
pub fn new(topic: T, message: M) -> Self {
Self { topic, message }
}
pub fn topic(&self) -> &T {
&self.topic
}
pub fn message(&self) -> &M {
&self.message
}
}
pub type TopicPublisher<T, M> = Publisher<TopicPayload<T, M>>;
pub type TopicSubscriber<T, M> = Subscriber<TopicPayload<T, M>>;
pub struct TopicSubscriptionFactory<T, M> {
subscriber: TopicSubscriber<T, M>,
}
impl<T, M> TopicSubscriptionFactory<T, M>
where
T: Eq + Send,
M: Clone + Send,
{
pub fn new(subscriber: TopicSubscriber<T, M>) -> Self {
TopicSubscriptionFactory { subscriber }
}
pub fn get_subscription(&self, topic: T) -> impl Stream<Item = M> {
self.subscriber.clone().filter_map(move |item| {
let result = if item.topic() == &topic {
Some(item.message.clone())
} else {
None
};
future::ready(result)
})
}
pub fn get_subscription_fused(&self, topic: T) -> Fuse<impl Stream<Item = M>> {
self.get_subscription(topic).fuse()
}
}
pub fn pubsub_channel_with_id<T: Send + Eq, M: Send + Clone>(
size: usize,
receiver_id: usize,
) -> (TopicPublisher<T, M>, TopicSubscriptionFactory<T, M>) {
let (publisher, subscriber): (TopicPublisher<T, M>, TopicSubscriber<T, M>) = bounded(size, receiver_id);
(publisher, TopicSubscriptionFactory::new(subscriber))
}
pub fn pubsub_channel<T: Send + Eq, M: Send + Clone>(
size: usize,
) -> (TopicPublisher<T, M>, TopicSubscriptionFactory<T, M>) {
pubsub_channel_with_id(size, 1)
}
#[cfg(test)]
mod test {
use futures::executor::block_on;
use super::*;
#[test]
fn topic_pub_sub() {
let (mut publisher, subscriber_factory) = pubsub_channel(10);
#[derive(Debug, Clone)]
struct Dummy {
a: u32,
b: String,
}
let messages = vec![
TopicPayload::new("Topic1", Dummy {
a: 1u32,
b: "one".to_string(),
}),
TopicPayload::new("Topic2", Dummy {
a: 2u32,
b: "two".to_string(),
}),
TopicPayload::new("Topic1", Dummy {
a: 3u32,
b: "three".to_string(),
}),
TopicPayload::new("Topic2", Dummy {
a: 4u32,
b: "four".to_string(),
}),
TopicPayload::new("Topic1", Dummy {
a: 5u32,
b: "five".to_string(),
}),
TopicPayload::new("Topic2", Dummy {
a: 6u32,
b: "size".to_string(),
}),
TopicPayload::new("Topic1", Dummy {
a: 7u32,
b: "seven".to_string(),
}),
];
block_on(async {
for m in messages {
publisher.send(m).await.unwrap();
}
});
let mut sub1 = subscriber_factory.get_subscription("Topic1").fuse();
let topic1a = block_on(async {
let mut result = Vec::new();
loop {
futures::select!(
item = sub1.select_next_some() => result.push(item),
default => break,
);
}
result
});
assert_eq!(topic1a.len(), 4);
assert_eq!(topic1a[0].a, 1);
assert_eq!(topic1a[1].a, 3);
assert_eq!(topic1a[2].a, 5);
assert_eq!(topic1a[3].a, 7);
let messages2 = vec![
TopicPayload::new("Topic1", Dummy {
a: 11u32,
b: "one one".to_string(),
}),
TopicPayload::new("Topic2", Dummy {
a: 22u32,
b: "two two".to_string(),
}),
TopicPayload::new("Topic1", Dummy {
a: 33u32,
b: "three three".to_string(),
}),
];
block_on(async move {
stream::iter(messages2).map(Ok).forward(publisher).await.unwrap();
});
let topic1b = block_on(async { sub1.collect::<Vec<Dummy>>().await });
assert_eq!(topic1b.len(), 2);
assert_eq!(topic1b[0].a, 11);
assert_eq!(topic1b[1].a, 33);
let sub2 = subscriber_factory.get_subscription("Topic2");
let topic2 = block_on(async { sub2.collect::<Vec<Dummy>>().await });
assert_eq!(topic2.len(), 4);
assert_eq!(topic2[0].a, 2);
assert_eq!(topic2[1].a, 4);
assert_eq!(topic2[2].a, 6);
assert_eq!(topic2[3].a, 22);
}
}