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
use http::{HttpResult, HttpError};
use http::connection::{
DataChunk,
EndStream,
};
use http::session::{
SessionState,
StreamDataChunk, StreamDataError,
Stream,
};
pub trait DataPrioritizer {
fn get_next_chunk(&mut self) -> HttpResult<Option<DataChunk>>;
}
pub struct SimplePrioritizer<'a, 'b, State> where State: SessionState + 'a {
state: &'a mut State,
buf: &'b mut [u8],
}
impl<'a, 'b, State> SimplePrioritizer<'a, 'b, State> where State: SessionState +'a {
pub fn new(state: &'a mut State, buf: &'b mut [u8]) -> SimplePrioritizer<'a, 'b, State> {
SimplePrioritizer {
state: state,
buf: buf,
}
}
}
impl<'a, 'b, State> DataPrioritizer for SimplePrioritizer<'a, 'b, State>
where State: SessionState +'a {
fn get_next_chunk(&mut self) -> HttpResult<Option<DataChunk>> {
for stream in self.state.iter().filter(|s| !s.is_closed_local()) {
let res = stream.get_data_chunk(self.buf);
match res {
Ok(StreamDataChunk::Last(total)) => {
return Ok(Some(DataChunk::new_borrowed(
&self.buf[..total], stream.id(), EndStream::Yes)));
},
Ok(StreamDataChunk::Chunk(total)) => {
return Ok(Some(DataChunk::new_borrowed(
&self.buf[..total], stream.id(), EndStream::No)));
},
Ok(StreamDataChunk::Unavailable) => {
},
Err(StreamDataError::Closed) => {
stream.close_local();
},
Err(StreamDataError::Other(e)) => {
return Err(HttpError::Other(e));
},
};
}
Ok(None)
}
}
#[cfg(test)]
mod tests {
use super::{DataPrioritizer, SimplePrioritizer};
use http::session::{DefaultSessionState, SessionState, Stream};
use http::tests::common::TestStream;
#[test]
fn test_simple_prioritizer() {
fn prepare_state() -> DefaultSessionState<TestStream> {
DefaultSessionState::new()
}
{
let mut buf = [0; 5];
let mut state = prepare_state();
let mut prioritizer = SimplePrioritizer::new(&mut state, &mut buf);
let chunk = prioritizer.get_next_chunk().unwrap();
assert!(chunk.is_none());
}
{
let mut buf = [0; 5];
let mut state = prepare_state();
let mut stream = TestStream::new(1);
stream.set_outgoing(vec![1, 2, 3]);
state.insert_stream(stream);
let mut prioritizer = SimplePrioritizer::new(&mut state, &mut buf);
{
let chunk = prioritizer.get_next_chunk().unwrap().unwrap();
assert_eq!(chunk.data, vec![1, 2, 3]);
}
assert!(prioritizer.get_next_chunk().unwrap().is_none());
}
{
let mut buf = [0; 2];
let mut state = prepare_state();
let mut stream = TestStream::new(1);
stream.set_outgoing(vec![1, 2, 3]);
state.insert_stream(stream);
let mut prioritizer = SimplePrioritizer::new(&mut state, &mut buf);
{
let chunk = prioritizer.get_next_chunk().unwrap().unwrap();
assert_eq!(chunk.data, vec![1, 2]);
}
{
let chunk = prioritizer.get_next_chunk().unwrap().unwrap();
assert_eq!(chunk.data, vec![3]);
}
assert!(prioritizer.get_next_chunk().unwrap().is_none());
}
{
let mut buf = [0; 10];
let mut state = prepare_state();
for id in 0..3 {
let mut stream = TestStream::new(id);
stream.set_outgoing(vec![1, 2, 3]);
state.insert_stream(stream);
}
for _ in 0..3 {
{
let mut prioritizer = SimplePrioritizer::new(&mut state, &mut buf);
let chunk = prioritizer.get_next_chunk().unwrap().unwrap();
assert_eq!(chunk.data, vec![1, 2, 3]);
}
for b in buf.iter_mut() { *b = 0; }
}
let mut prioritizer = SimplePrioritizer::new(&mut state, &mut buf);
assert!(prioritizer.get_next_chunk().unwrap().is_none());
}
}
}