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
use std::boxed::Box;
use std::collections::HashMap;
use std::thread;
use std::vec::Vec;
use crossbeam::crossbeam_channel::{unbounded, Select};
use crossbeam::{Receiver,Sender};
use queue::Queue;
use crate::serializers::*;
use crate::node::thread_pool::ThreadMessage;
use super::Label;
impl Label {
pub fn new<F>( default_fun: F, input_channel : Receiver<Vec<u8>> ) -> Label
where F: FnMut(Vec<u8>) + Send + Sync + 'static,{
let (s,r) = unbounded();
Label {
input_channel: input_channel,
default_fun : Some( Box::new( default_fun)),
map : HashMap::new(),
fn_receiver : r,
fn_sender : s,
queue_by_label : HashMap::new(),
pending : 0,
stoped : false,
}
}
pub fn start( mut self, thread_pool_channel : Sender<ThreadMessage>){
// initialize queue
self.queue_by_label.insert(None, Queue::new());
for k in self.map.keys(){
self.queue_by_label.insert( Some(k.clone()), Queue::new());
}
let fn_receiver_cloned = self.fn_receiver.clone();
let input_channel_cloned = self.input_channel.clone();
thread::spawn(move || {
let mut sel = Select::new();
sel.recv( &fn_receiver_cloned );
sel.recv( &input_channel_cloned);
loop{
match sel.ready() {
0 => {
// From threadPool
let res = self.fn_receiver.try_recv();
if let Err(e) = res {
if e.is_empty() { continue; }
}
let (option_label, f) = res.unwrap();
self.receive_handler(thread_pool_channel.clone(), option_label, f);
if self.stoped && self.pending == 0{
return ;
}
},
_ => {
// From outside
let res = self.input_channel.try_recv();
if let Err(e) = res {
if e.is_empty() {
continue;
}else{
self.stoped = true;
if self.pending == 0{
return;
}
}
};
let vec = res.unwrap();
let (label,msg) = deserialize_label_message(vec);
match self.map.remove(&label) {
None => {
match self.default_fun{
Some(fun) =>{
thread_pool_channel.send( ThreadMessage::new_with_stateful( fun, msg, None, self.fn_sender.clone())).unwrap();
self.default_fun = None;
}
None =>{
self.queue_by_label.get_mut(&None).unwrap().queue(msg).unwrap();
self.pending += 1;
}
}
},
Some(option_fun) =>{
match option_fun {
Some(fun) =>{
self.map.insert( label.clone(), None);
thread_pool_channel.send( ThreadMessage::new_with_stateful(fun, msg, Some(label.clone()), self.fn_sender.clone())).unwrap();
},
None =>{
self.map.insert( label.clone(), None);
self.queue_by_label.get_mut(&Some(label)).unwrap().queue( msg ).unwrap();
self.pending += 1;
}
}
},
};
},
}
}
});
}
fn receive_handler(&mut self , thread_pool_channel : Sender<ThreadMessage>, option_label : Option<String>, fun : Box<FnMut(Vec<u8>) + Send + Sync + 'static>){
let optional_vec = self.queue_by_label.get_mut(&option_label).unwrap().dequeue();
match option_label {
None =>
match optional_vec {
None =>
self.default_fun = Some(fun),
Some(vec) =>{
thread_pool_channel.send( ThreadMessage::new_with_stateful(fun, vec, None, self.fn_sender.clone())).unwrap();
self.pending -= 1;
}
},
Some(label) =>{
match optional_vec{
None =>{
self.map.insert( label, Some(fun));
},
Some(vec) =>{
thread_pool_channel.send( ThreadMessage::new_with_stateful(fun, vec, Some(label), self.fn_sender.clone())).unwrap();
self.pending -= 1;
}
}
}
}
}
// API //
pub fn add_handlers(&mut self, list : Vec<( String, Box<FnMut(Vec<u8>) + Send + Sync + 'static >)>){
for (l,f) in list {
self.map.insert(l, Some(f));
}
}
}