1
1
use std:: { collections:: HashMap , sync:: Arc , time:: Duration } ;
2
2
3
3
use once_cell:: sync:: Lazy ;
4
- use ott_balancer_protocol:: collector:: BalancerState ;
4
+ use ott_balancer_protocol:: { collector:: BalancerState , RoomName } ;
5
5
use ott_common:: discovery:: { ConnectionConfig , ServiceDiscoveryMsg } ;
6
6
use rocket:: futures:: StreamExt ;
7
7
use serde:: Deserialize ;
@@ -205,6 +205,8 @@ fn should_send(event: &str) -> bool {
205
205
#[ non_exhaustive]
206
206
enum Event {
207
207
Ws ( EventWebsocketMessage ) ,
208
+ Proxy ( EventProxyRequest ) ,
209
+ Broadcast ( EventBroadcast ) ,
208
210
}
209
211
210
212
/// Indicates that a message was sent or received on a websocket connection to a balancer.
@@ -213,6 +215,22 @@ enum Event {
213
215
struct EventWebsocketMessage {
214
216
node_id : Uuid ,
215
217
direction : MsgDirection ,
218
+ room : Option < RoomName > ,
219
+ }
220
+
221
+ #[ derive( Debug , Deserialize ) ]
222
+ #[ allow( dead_code) ]
223
+ struct EventProxyRequest {
224
+ node_id : Uuid ,
225
+ direction : MsgDirection ,
226
+ room : Option < RoomName > ,
227
+ }
228
+
229
+ #[ derive( Debug , Deserialize ) ]
230
+ #[ allow( dead_code) ]
231
+ struct EventBroadcast {
232
+ node_id : String ,
233
+ direction : MsgDirection ,
216
234
}
217
235
218
236
#[ derive( Debug , Deserialize ) ]
@@ -228,7 +246,7 @@ mod tests {
228
246
use uuid:: uuid;
229
247
230
248
#[ test]
231
- fn test_deserialize_event ( ) {
249
+ fn test_deserialize_event_ws ( ) {
232
250
let event =
233
251
r#"{"event":"ws","node_id":"f47ac10b-58cc-4372-a567-0e02b2c3d479", "direction": "tx"}"# ;
234
252
let event: Event = serde_json:: from_str ( event) . unwrap ( ) ;
@@ -242,4 +260,66 @@ mod tests {
242
260
}
243
261
}
244
262
}
263
+
264
+ #[ test]
265
+ fn test_deserialize_event_ws2 ( ) {
266
+ let event = r#"{"event":"ws","node_id":"f47ac10b-58cc-4372-a567-0e02b2c3d479", "room": "foo", "direction": "tx"}"# ;
267
+ let event: Event = serde_json:: from_str ( event) . unwrap ( ) ;
268
+ #[ allow( unreachable_patterns) ]
269
+ match event {
270
+ Event :: Ws ( EventWebsocketMessage { node_id, room, .. } ) => {
271
+ assert_eq ! ( node_id, uuid!( "f47ac10b-58cc-4372-a567-0e02b2c3d479" ) ) ;
272
+ assert_eq ! ( room, Some ( "foo" . into( ) ) ) ;
273
+ }
274
+ _ => {
275
+ panic ! ( "unexpected event type: {:?}" , event) ;
276
+ }
277
+ }
278
+ }
279
+
280
+ #[ test]
281
+ fn test_deserialize_event_proxy ( ) {
282
+ let event = r#"{"event":"proxy","node_id":"f47ac10b-58cc-4372-a567-0e02b2c3d479", "direction": "tx"}"# ;
283
+ let event: Event = serde_json:: from_str ( event) . unwrap ( ) ;
284
+ #[ allow( unreachable_patterns) ]
285
+ match event {
286
+ Event :: Proxy ( EventProxyRequest { node_id, .. } ) => {
287
+ assert_eq ! ( node_id, uuid!( "f47ac10b-58cc-4372-a567-0e02b2c3d479" ) ) ;
288
+ }
289
+ _ => {
290
+ panic ! ( "unexpected event type: {:?}" , event) ;
291
+ }
292
+ }
293
+ }
294
+
295
+ #[ test]
296
+ fn test_deserialize_event_proxy2 ( ) {
297
+ let event = r#"{"event":"proxy","node_id":"f47ac10b-58cc-4372-a567-0e02b2c3d479", "room": "foo", "direction": "tx"}"# ;
298
+ let event: Event = serde_json:: from_str ( event) . unwrap ( ) ;
299
+ #[ allow( unreachable_patterns) ]
300
+ match event {
301
+ Event :: Proxy ( EventProxyRequest { node_id, room, .. } ) => {
302
+ assert_eq ! ( node_id, uuid!( "f47ac10b-58cc-4372-a567-0e02b2c3d479" ) ) ;
303
+ assert_eq ! ( room, Some ( "foo" . into( ) ) ) ;
304
+ }
305
+ _ => {
306
+ panic ! ( "unexpected event type: {:?}" , event) ;
307
+ }
308
+ }
309
+ }
310
+
311
+ #[ test]
312
+ fn test_deserialize_event_broadcast ( ) {
313
+ let event = r#"{"event":"broadcast","node_id":"foo", "direction": "tx"}"# ;
314
+ let event: Event = serde_json:: from_str ( event) . unwrap ( ) ;
315
+ #[ allow( unreachable_patterns) ]
316
+ match event {
317
+ Event :: Broadcast ( EventBroadcast { node_id, .. } ) => {
318
+ assert_eq ! ( node_id, "foo" ) ;
319
+ }
320
+ _ => {
321
+ panic ! ( "unexpected event type: {:?}" , event) ;
322
+ }
323
+ }
324
+ }
245
325
}
0 commit comments