-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.rs
108 lines (72 loc) · 2.36 KB
/
simple.rs
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
#[tokio::main]
async fn main() {
let(shutdown_sender, shutdown_recv) = channel();
// -----------------------------------
//
// Producer -> ProducerConsumer -> Consumer
//
// ------------------------------------
// Run Consumer
let log_chan = ConsumerRunnable::new(Box::new(Log)).run(100);
// Run ProducerConsumer
let filter_chan = ProducerConsumerRunnable::new(Box::new(FilterByAge),
vec![log_chan],
Some(DispatcherType::RoundRobin)
).unwrap().run(100);
// Run Producer
let _ = ProducerRunnable::new(Box::new(Prod),
vec![filter_chan],
None,
100,
shutdown_recv).unwrap().run();
tokio::time::sleep(Duration::from_secs(10)).await;
}
#[derive(Clone)]
struct ProdEvent {
pub funame: String,
pub age: i32
}
struct Prod;
#[async_trait]
impl Producer<ProdEvent> for Prod {
async fn init(&mut self) {}
async fn terminate(&mut self) {}
async fn handle_demand(&mut self, max_demand: usize) -> Vec<ProdEvent> {
(0..max_demand as i32)
.into_iter()
.map(|i| {
ProdEvent {
funame: format!("DanyalMh-{}", i),
age: (i + 30) % 35
}
})
.collect()
}
}
// -------------------------------------------
struct FilterByAge;
#[async_trait]
impl ProducerConsumer<ProdEvent, ProdEvent> for FilterByAge {
async fn init(&mut self) {}
async fn terminate(&mut self) {}
async fn handle_events(&mut self, events: Vec<ProdEvent>) -> Vec<ProdEvent> {
events
.into_iter()
.filter(|pe| pe.age > 25 && pe.age < 32)
.collect()
}
}
struct Log;
#[async_trait]
impl Consumer<ProdEvent> for Log {
async fn init(&mut self) {}
async fn terminate(&mut self) {}
async fn handle_events(&mut self, events: Vec<ProdEvent>) -> State<ProdEvent> {
events
.into_iter()
.for_each(|pe| {
println!("==> {} -> {}", pe.funame, pe.age)
});
State::Continue
}
}