-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectivity.rs
251 lines (214 loc) · 7.34 KB
/
connectivity.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use std::sync::mpsc::{channel, Receiver};
use std::sync::{Arc, Barrier};
use std::thread::{sleep, spawn};
use std::time::Duration;
use libjuice_rs::{Agent, Handler, State};
include!("../src/test_util.rs");
#[test]
fn connectivity_no_trickle() {
logger_init();
let gathering_barrier = Arc::new(Barrier::new(3));
let (first_tx, first_rx) = channel();
let first_handler = Handler::default()
.gathering_done_handler({
let barrier = gathering_barrier.clone();
move || {
log::info!("first agent finished gathering");
barrier.wait();
}
})
.state_handler(move |state| log::info!("first changed state to: {:?}", state))
.recv_handler(move |packet| {
log::debug!("first received {:?}", packet);
let _ = first_tx.send(packet.to_vec());
});
let first = Agent::builder(first_handler).build().unwrap();
let (second_tx, second_rx) = channel();
let second_handler = Handler::default()
.gathering_done_handler({
let barrier = gathering_barrier.clone();
move || {
log::info!("second agent finished gathering");
barrier.wait();
}
})
.state_handler(move |state| log::info!("second changed state to: {:?}", state))
.recv_handler(move |packet| {
log::debug!("second received {:?}", packet);
let _ = second_tx.send(packet.to_vec());
});
let second = Agent::builder(second_handler)
.with_port_range(5000, 5010)
.build()
.unwrap();
first.gather_candidates().unwrap();
second.gather_candidates().unwrap();
gathering_barrier.wait();
let first_desc = first.get_local_description().unwrap();
second.set_remote_description(first_desc).unwrap();
let second_desc = second.get_local_description().unwrap();
first.set_remote_description(second_desc).unwrap();
sleep(Duration::from_secs(2));
assert_eq!(first.get_state(), State::Completed);
assert_eq!(second.get_state(), State::Completed);
log::info!(
"first selected candidates: {:?}",
first.get_selected_candidates()
);
log::info!(
"second selected candidates: {:?}",
second.get_selected_candidates()
);
log::info!(
"first selected addresses: {:?}",
first.get_selected_addresses()
);
log::info!(
"second selected addresses: {:?}",
second.get_selected_addresses()
);
first.send("hello".as_bytes()).unwrap();
assert_eq!(
second_rx.recv_timeout(Duration::from_secs(1)),
Ok("hello".into())
);
second.send("world".as_bytes()).unwrap();
assert_eq!(
first_rx.recv_timeout(Duration::from_secs(1)),
Ok("world".into())
);
}
enum TrickleEvent {
Candidate(String),
Eof,
}
// tricky trickle
fn trickle_signaling(ch: Receiver<TrickleEvent>, agent: Arc<Agent>) {
let mut counter = 0;
loop {
match ch.recv_timeout(Duration::from_secs(1)) {
Ok(TrickleEvent::Candidate(sdp)) => agent.add_remote_candidate(sdp).unwrap(),
Ok(TrickleEvent::Eof) => {
agent.set_remote_gathering_done().unwrap();
break;
}
Err(_) => {
counter += 1;
if counter == 3 {
break;
}
}
}
}
}
#[test]
fn connectivity_trickle() {
logger_init();
let gathering_barrier = Arc::new(Barrier::new(3));
let (first_tx, first_rx) = channel();
let (first_candidate_tx, first_candidate_rx) = channel();
let first_handler = Handler::default()
.gathering_done_handler({
let barrier = gathering_barrier.clone();
let first_candidate_tx = first_candidate_tx.clone();
move || {
log::info!("first agent finished gathering");
let _ = first_candidate_tx.send(TrickleEvent::Eof);
barrier.wait();
}
})
.state_handler(move |state| log::info!("first changed state to: {:?}", state))
.recv_handler(move |packet| {
log::debug!("first received {:?}", packet);
let _ = first_tx.send(packet.to_vec());
})
.candidate_handler({
move |sdp| {
log::debug!("first local candidate {:?}", sdp);
let _ = first_candidate_tx.send(TrickleEvent::Candidate(sdp));
}
});
let bind = "127.0.0.1".parse().unwrap();
let first = Arc::new(
Agent::builder(first_handler)
.with_bind_address(&bind)
.build()
.unwrap(),
);
let (second_tx, second_rx) = channel();
let (second_candidate_tx, second_candidate_rx) = channel();
let second_handler = Handler::default()
.gathering_done_handler({
let barrier = gathering_barrier.clone();
let second_candidate_tx = second_candidate_tx.clone();
move || {
log::info!("second agent finished gathering");
let _ = second_candidate_tx.send(TrickleEvent::Eof);
barrier.wait();
}
})
.state_handler(move |state| log::info!("second changed state to: {:?}", state))
.recv_handler(move |packet| {
log::debug!("second received {:?}", packet);
let _ = second_tx.send(packet.to_vec());
})
.candidate_handler(move |sdp| {
log::debug!("second local candidate {:?}", sdp);
let _ = second_candidate_tx.send(TrickleEvent::Candidate(sdp));
});
let second = Arc::new(Agent::builder(second_handler).build().unwrap());
let handle1 = {
let first = first.clone();
spawn(move || trickle_signaling(second_candidate_rx, first))
};
let handle2 = {
let second = second.clone();
spawn(move || trickle_signaling(first_candidate_rx, second))
};
// exchange descriptions
let first_desc = first.get_local_description().unwrap();
second.set_remote_description(first_desc).unwrap();
let second_desc = second.get_local_description().unwrap();
first.set_remote_description(second_desc).unwrap();
// and then start gathering
first.gather_candidates().unwrap();
second.gather_candidates().unwrap();
gathering_barrier.wait();
sleep(Duration::from_secs(2));
assert!(matches!(
first.get_state(),
State::Connected | State::Completed
));
assert!(matches!(
second.get_state(),
State::Connected | State::Completed
));
log::info!(
"first selected candidates: {:?}",
first.get_selected_candidates()
);
log::info!(
"second selected candidates: {:?}",
second.get_selected_candidates()
);
log::info!(
"first selected addresses: {:?}",
first.get_selected_addresses()
);
log::info!(
"second selected addresses: {:?}",
second.get_selected_addresses()
);
first.send("hello".as_bytes()).unwrap();
assert_eq!(
second_rx.recv_timeout(Duration::from_secs(1)),
Ok("hello".into())
);
second.send("world".as_bytes()).unwrap();
assert_eq!(
first_rx.recv_timeout(Duration::from_secs(1)),
Ok("world".into())
);
handle1.join().unwrap();
handle2.join().unwrap();
}