agntcy-slim-session 0.3.0

SLIM session internal implementation.
Documentation
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0

use std::{
    collections::{HashMap, HashSet},
    time::Duration,
};

use display_error_chain::ErrorChainExt;
use slim_datapath::api::{
    CommandPayload, NameId, ProtoMessage as Message, ProtoName, ProtoSessionMessageType,
    ProtoSessionType,
};
use tokio::sync::mpsc::Sender;
use tracing::debug;

use crate::{
    SessionError,
    common::{SessionMessage, SessionOutput},
    timer::Timer,
    timer_factory::{TimerFactory, TimerSettings},
};

/// Ping interval.
pub const PING_INTERVAL: Duration = Duration::from_secs(10);
/// Maximum number of consecutive ping failures before a participant is considered disconnected.
const MAX_PING_FAILURE: u32 = 3;
/// Synthetic moderator name used by participants to track ping reception
static MODERATOR_NAME: std::sync::LazyLock<ProtoName> =
    std::sync::LazyLock::new(|| ProtoName::from_strings(["agntcy", "ns", "moderator"]));

/// used a result in OnMessage function
#[derive(PartialEq, Clone, Debug)]
enum ControllerSenderDrainStatus {
    NotDraining,
    Initiated,
    Completed,
}

struct PendingReply {
    /// Missing replies
    /// Keep track of the names so that if we get  multiple acks from
    /// the same endpoint we don't count it twice
    missing_replies: HashSet<ProtoName>,

    /// Message to resend in case of timeout
    message: Message,

    /// the timer
    timer: Timer,
}

struct PingState {
    /// Current pending ping
    /// Maybe empty if none is connected to the channel
    /// Used only if this endpoint is sending ping messages
    ping: Option<PendingReply>,

    /// This indicates if at least one ping message was received
    /// during the last ping timer. It is used only by participants
    /// and set to true on the ping reception
    received_ping: bool,

    /// Ping timer factor set to create ping related timers
    #[allow(dead_code)]
    ping_timer_factory: TimerFactory,

    /// List of potential disconnected endpoint
    /// Initiation/Moderator mode:
    /// If an endpoint does not reply to latest N pings it is considered
    /// disconnected and the session controller is notified.
    /// The map keeps track of the name and the number of missing ping replies
    /// Participant mode:
    /// The map keeps track only of the moderator and checks for how many
    /// interval the moderator was silent
    missing_pings: HashMap<ProtoName, u32>,

    /// The ping timer
    /// this timer is used only for the pings and it is not connected to
    /// a specific message, it is used to send new pings periodically
    ping_timer: Timer,
}

pub struct ControllerSender {
    /// timer factory to crate timers for acks
    timer_factory: TimerFactory,

    /// local name to be removed in the missing replies set
    local_name: ProtoName,

    /// group name is set on the first join request message
    /// in p2p session is equal to the remote name of the join request
    /// while in multicast session is specified in the payload
    group_name: Option<ProtoName>,

    /// session type
    session_type: ProtoSessionType,

    /// session id
    session_id: u32,

    /// list of pending replies for each control message
    pending_replies: HashMap<u32, PendingReply>,

    /// ping state
    /// by default is None, start only if a duration is set
    ping_state: Option<PingState>,

    /// set to true if the participant is an initiator
    initiator: bool,

    /// group list
    /// list of participants to the group
    group_list: HashSet<ProtoName>,

    /// send message to the session controller
    tx_session: Sender<SessionMessage>,

    /// drain state - when true, no new messages from app are accepted
    draining_state: ControllerSenderDrainStatus,
}

impl ControllerSender {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        timer_settings: TimerSettings,
        local_name: ProtoName,
        session_type: ProtoSessionType,
        session_id: u32,
        ping_interval: Option<Duration>,
        initiator: bool,
        tx_signals: Sender<SessionMessage>,
    ) -> Self {
        let mut list = HashSet::new();
        list.insert(local_name.clone());

        let ping_state = if let Some(interval) = ping_interval {
            // we need to setup the timer for the ping
            let settings =
                TimerSettings::new(interval, None, None, crate::timer::TimerType::Constant);
            let ping_timer_factory = TimerFactory::new(settings, tx_signals.clone());
            let ping_timer = ping_timer_factory.create_and_start_timer(
                rand::random::<u32>(),
                slim_datapath::api::ProtoSessionMessageType::Ping,
                None,
            );
            Some(PingState {
                ping: None,
                received_ping: false,
                ping_timer_factory,
                missing_pings: HashMap::new(),
                ping_timer,
            })
        } else {
            None
        };

        ControllerSender {
            timer_factory: TimerFactory::new(timer_settings, tx_signals.clone()),
            local_name,
            group_name: None,
            session_type,
            session_id,
            pending_replies: HashMap::new(),
            ping_state,
            initiator,
            group_list: list,
            tx_session: tx_signals,
            draining_state: ControllerSenderDrainStatus::NotDraining,
        }
    }

    // helper function to update local state based on the message type received
    fn update_local_state(&mut self, message: &Message) -> Result<(), SessionError> {
        match message.get_session_message_type() {
            slim_datapath::api::ProtoSessionMessageType::GroupWelcome => {
                // update the group list on welcome messages
                // adding the new participant to the list
                debug!(
                    participant = %message.get_dst(),
                    "adding participant to group on welcome message"
                );
                self.group_list.insert(message.get_dst());
            }
            slim_datapath::api::ProtoSessionMessageType::LeaveRequest => {
                // update the group list on leave requests
                // removing the participant from the list
                debug!(
                    participant = %message.get_dst(),
                    "removing participant from group on leave request"
                );
                self.group_list.remove(&message.get_dst());

                // remove also the missing_pings state if present
                if let Some(ps) = self.ping_state.as_mut() {
                    ps.missing_pings.remove(&message.get_dst());
                }
            }
            slim_datapath::api::ProtoSessionMessageType::JoinRequest => {
                // setup the group name if not set yet
                if self.group_name.is_none() {
                    if self.session_type == ProtoSessionType::PointToPoint {
                        // in p2p session the group name is equal to the remote name
                        // in the join request message. Data and control messages
                        // are distributed using the same name.
                        debug!(
                            destination = %message.get_dst(),
                            "update group name on join request message for p2p session",
                        );
                        self.group_name = Some(message.get_dst());
                    } else {
                        // in multicast session the group name is specified in the
                        // payload of the message
                        let mut group_name = message
                            .extract_join_request()?
                            .channel
                            .as_ref()
                            .ok_or(SessionError::MissingGroupNameInJoinRequest)?
                            .clone();
                        group_name.set_id(NameId::CONTROL_CHANNEL_ID);
                        debug!(
                            destination = %group_name,
                            "update group name on join request message for multicast session",
                        );
                        self.group_name = Some(group_name);
                    }
                }
            }
            _ => {}
        }
        Ok(())
    }

    pub fn on_message(&mut self, message: &Message) -> Result<SessionOutput, SessionError> {
        if self.draining_state == ControllerSenderDrainStatus::Completed {
            return Err(SessionError::SessionDrainingDrop);
        }

        let mut output = SessionOutput::new();

        match message.get_session_message_type() {
            slim_datapath::api::ProtoSessionMessageType::DiscoveryRequest
            | slim_datapath::api::ProtoSessionMessageType::JoinRequest
            | slim_datapath::api::ProtoSessionMessageType::LeaveRequest
            | slim_datapath::api::ProtoSessionMessageType::GroupWelcome => {
                if self.draining_state == ControllerSenderDrainStatus::Initiated {
                    // draining period started; reject new messages
                    return Err(SessionError::SessionDrainingDrop);
                }

                // create the set of missing replies
                let mut missing_replies = HashSet::new();
                let mut name = message.get_dst();
                if message.get_session_message_type()
                    == slim_datapath::api::ProtoSessionMessageType::DiscoveryRequest
                {
                    // the discovery request should be sent to an unknown destination id.
                    // if the id is present remove it for consistency on the ack return.
                    // this affects only the ack registration and not the forwarding behaviour.
                    name.reset_id();
                }
                missing_replies.insert(name);

                // update local state
                self.update_local_state(message)?;

                // send the message and setup the required timers
                output.extend(self.on_send_message(message, missing_replies)?);
            }
            slim_datapath::api::ProtoSessionMessageType::DiscoveryReply
            | slim_datapath::api::ProtoSessionMessageType::JoinReply
            | slim_datapath::api::ProtoSessionMessageType::LeaveReply
            | slim_datapath::api::ProtoSessionMessageType::GroupAck => {
                self.on_reply_message(message);
            }
            slim_datapath::api::ProtoSessionMessageType::GroupNack => {
                // in case on Nack we stop the timer as for the Acks
                // and we leave the application/controller decide what
                // to do to handle it
                self.on_reply_message(message);
            }
            slim_datapath::api::ProtoSessionMessageType::Ping => self.on_ping_message(message),
            slim_datapath::api::ProtoSessionMessageType::GroupAdd => {
                // compute the list of participants that needs to send an ack
                // remove the local name as we are not waiting for any reply from the local name
                let missing_replies = self
                    .group_list
                    .iter()
                    .filter(|name| *name != &self.local_name)
                    .cloned()
                    .collect::<HashSet<_>>();

                output.extend(self.on_send_message(message, missing_replies)?);
            }
            slim_datapath::api::ProtoSessionMessageType::GroupRemove => {
                // compute the list of participants that needs to send an ack
                // the participant that we are removing will get the update
                // so we can use the group list as is, removing only the local name
                let missing_replies = self
                    .group_list
                    .iter()
                    .filter(|name| *name != &self.local_name)
                    .cloned()
                    .collect::<HashSet<_>>();

                // remove the endpoint also from the group list
                let payload = message.extract_group_remove()?;

                let to_remove = payload
                    .removed_participant
                    .as_ref()
                    .ok_or(SessionError::MissingRemovedParticipantInGroupRemove)?
                    .clone();

                self.group_list.remove(&to_remove);

                output.extend(self.on_send_message(message, missing_replies)?);
            }
            slim_datapath::api::ProtoSessionMessageType::GroupClose => {
                // compute the list of participants that needs to send an ack
                let missing_replies = self
                    .group_list
                    .iter()
                    .filter(|name| *name != &self.local_name)
                    .cloned()
                    .collect::<HashSet<_>>();

                output.extend(self.on_send_message(message, missing_replies)?);
            }
            slim_datapath::api::ProtoSessionMessageType::GroupProposal => todo!(),
            _ => {
                debug!("unexpected message type");
            }
        }

        Ok(output)
    }

    fn on_send_message(
        &mut self,
        message: &Message,
        missing_replies: HashSet<ProtoName>,
    ) -> Result<SessionOutput, SessionError> {
        let id = message.get_id();

        debug!(
            %id, ?missing_replies,
            "create a new timer for message, waiting responses",
        );
        let pending = PendingReply {
            missing_replies,
            message: message.clone(),
            timer: self.timer_factory.create_and_start_timer(
                id,
                message.get_session_message_type(),
                None,
            ),
        };

        self.pending_replies.insert(id, pending);

        let mut output = SessionOutput::new();
        output.push_slim(message.clone());
        Ok(output)
    }

    fn on_reply_message(&mut self, message: &Message) {
        let id = message.get_id();
        debug!(
            %id,
            source = %message.get_source(),
            "receive reply for message",
        );

        let mut delete = false;
        if let Some(pending) = self.pending_replies.get_mut(&id) {
            debug!(%id, "try to remove from pending acks");
            let mut name = message.get_source();
            if message.get_session_message_type()
                == slim_datapath::api::ProtoSessionMessageType::DiscoveryReply
            {
                name.reset_id();
            }
            pending.missing_replies.remove(&name);
            if pending.missing_replies.is_empty() {
                debug!("all replies received, remove timer");
                pending.timer.stop();
                delete = true;
            }
        }

        if delete {
            self.pending_replies.remove(&id);
        }
    }

    fn on_ping_message(&mut self, message: &Message) {
        debug!(id = %message.get_id(), "received ping message");
        if self.initiator {
            // if this is an initiator update the missing acks
            if let Some(ping_state) = &mut self.ping_state
                && let Some(ping) = &mut ping_state.ping
                && ping.timer.get_id() == message.get_id()
            {
                ping.missing_replies.remove(&message.get_source());
                if ping.missing_replies.is_empty() {
                    debug!("stop ping retransmissions for id {}", message.get_id());
                    ping.timer.stop()
                }
                return;
            }
        } else {
            // if this is a participant mark the reception of the message
            if let Some(ping_state) = &mut self.ping_state {
                ping_state.received_ping = true;
                return;
            }
        }

        debug!(id = %message.get_id(), "received a ping but the state is not set, ignore the message");
    }

    pub fn is_still_pending(&self, message_id: u32) -> bool {
        self.pending_replies.contains_key(&message_id)
    }

    pub(crate) fn on_timer_timeout(
        &mut self,
        id: u32,
        msg_type: ProtoSessionMessageType,
    ) -> Result<SessionOutput, SessionError> {
        debug!(%id, ?msg_type, "timeout for message");

        // check if the timeout is related to a ping
        if self.ping_state.is_some() && msg_type == ProtoSessionMessageType::Ping {
            return self.handle_ping_timeout(id);
        }

        // the timer is not related to a ping, resent the message if possible
        if let Some(pending) = self.pending_replies.get(&id) {
            let mut output = SessionOutput::new();
            output.push_slim(pending.message.clone());
            return Ok(output);
        };

        Err(SessionError::TimerNotFound(id))
    }

    fn handle_ping_timeout(&mut self, id: u32) -> Result<SessionOutput, SessionError> {
        // If this is a participant check if a message was received
        // during the last ping time
        if !self.initiator
            && let Some(ping_state) = &mut self.ping_state
        {
            if ping_state.received_ping {
                // reset the state
                debug!(%id, "received at least on ping message, reset the state");
                ping_state.received_ping = false;
                ping_state.missing_pings.clear();
            } else {
                // update the missing ping map and detect moderator disconnection
                debug!(%id, "missing ping message from moderator");
                let val = ping_state
                    .missing_pings
                    .entry(MODERATOR_NAME.clone())
                    .or_insert(0);
                *val += 1;
                if *val >= MAX_PING_FAILURE {
                    debug!("moderator got disconnected");
                    if let Err(e) = self
                        .tx_session
                        .try_send(SessionMessage::ParticipantDisconnected { name: None })
                    {
                        debug!(error = %e.chain(), "failed to send participant disconnected message");
                    }
                }
            }
            return Ok(SessionOutput::new());
        }

        // This is a initiator
        // Check if we need to handle ping timeout
        let should_handle_ping_interval = self
            .ping_state
            .as_ref()
            .map(|ps| ps.ping_timer.get_id() == id)
            .ok_or(SessionError::PingStateNotInitialized)?;

        if should_handle_ping_interval {
            debug!("ping interval timeout, check current group state");
            // the timeout is related to the ping interval timer
            // check if we sent a ping before and if there are still pending acks to the ping
            self.handle_ping_state();

            // completely reset the ping if needed
            self.ping_state.as_mut().map(|s| s.ping.take());

            if self.group_list.len() > 1
                && let Some(group_name) = &self.group_name
            {
                // someone is connected to the channel, send the ping
                // create the message
                let ping_id = rand::random::<u32>();
                let mut builder = Message::builder()
                    .source(self.local_name.clone())
                    .destination(group_name.clone())
                    .identity("")
                    .session_type(self.session_type)
                    .session_message_type(ProtoSessionMessageType::Ping)
                    .session_id(self.session_id)
                    .message_id(ping_id)
                    .payload(CommandPayload::builder().ping().as_content());

                if self.session_type == ProtoSessionType::Multicast {
                    builder = builder.fanout(256);
                }

                let ping = builder.build_publish()?;

                debug!(id = %ping_id, "send a new ping");

                // set the ping missing replies state
                let missing_replies = self
                    .group_list
                    .iter()
                    .filter(|name| *name != &self.local_name)
                    .cloned()
                    .collect::<HashSet<_>>();

                let mut output = SessionOutput::new();
                output.push_slim(ping.clone());

                if let Some(ping_state) = self.ping_state.as_mut() {
                    ping_state.ping = Some(PendingReply {
                        missing_replies,
                        message: ping,
                        // the ping message should be resent like all the other command message
                        // if some remote participant do no replies. The ping_state.ping_timer_factory
                        // is used only to recreate the message periodically
                        timer: self.timer_factory.create_and_start_timer(
                            ping_id,
                            ProtoSessionMessageType::Ping,
                            None,
                        ),
                    });
                }

                return Ok(output);
            }
        } else {
            // most likely the timeout is related to the ping message itself so
            // we need to send it again
            let message_to_send = self
                .ping_state
                .as_ref()
                .and_then(|ps| ps.ping.as_ref())
                .map(|p| p.message.clone());

            if let Some(ping_message) = message_to_send {
                debug!(%id, "ping message timeout, send it again");
                // simply resend the message
                let mut output = SessionOutput::new();
                output.push_slim(ping_message);
                return Ok(output);
            }
        }

        Ok(SessionOutput::new())
    }

    /// Handle ping state by updating missing_pings and checking for disconnections
    fn handle_ping_state(&mut self) {
        let ping_state = self
            .ping_state
            .as_mut()
            .expect("ping_state should be initialized");
        let Some(mut ping) = ping_state.ping.take() else {
            return;
        };

        // stop the timer for ping retransmission
        ping.timer.stop();

        // if all participants replied to the ping, reset the
        // missing_pings map otherwise try to see if someone got disconnected
        if ping.missing_replies.is_empty() {
            debug!("all ping received, nobody got disconnected");
            ping_state.missing_pings.clear();
        } else {
            // update missing_pings
            for p in &ping.missing_replies {
                debug!(from = %p, "missing ping reply from");
                // add the non reply participant to the missing pings map
                // only if it is still connected to the group
                if self.group_list.contains(p) {
                    *ping_state.missing_pings.entry(p.clone()).or_insert(0) += 1;
                }
            }

            // check for disconnected participants and notify, then remove them
            ping_state.missing_pings.retain(|k, v| {
                if *v >= MAX_PING_FAILURE {
                    debug!(participant = %k, "participant got disconnected");
                    self.group_list.remove(k);
                    if let Err(e) =
                        self.tx_session
                            .try_send(SessionMessage::ParticipantDisconnected {
                                name: Some(k.clone()),
                            })
                    {
                        debug!(error = %e.chain(), "failed to send participant disconnected message");
                    }
                    false // remove from missing_pings
                } else {
                    true // keep in missing_pings
                }
            });
        }
    }

    pub fn on_failure(&mut self, id: u32, msg_type: ProtoSessionMessageType) {
        if msg_type == ProtoSessionMessageType::Ping {
            // the only timer that can fail is the one related to the ping retransmissions
            let should_handle = if let Some(ping_state) = &self.ping_state {
                ping_state
                    .ping
                    .as_ref()
                    .map(|ping| ping.timer.get_id() == id)
                    .unwrap_or(false)
            } else {
                false
            };

            if should_handle {
                // reset the pending ping state and wait for the next one to be sent
                debug!(%id, "ping message timer failure, update ping state");
                self.handle_ping_state();
            } else {
                debug!("got message failure for unknown ping, ignore it");
                return;
            }
        }

        if let Some(gt) = self.pending_replies.get_mut(&id) {
            gt.timer.stop();
        }

        self.pending_replies.remove(&id);
    }

    pub fn clear_timers(&mut self) {
        for (_, mut p) in self.pending_replies.drain() {
            p.timer.stop();
        }
        self.pending_replies.clear();
    }

    pub fn start_drain(&mut self) {
        // set only initiated to true because we may need send request leave
        debug!("controller sender drain initiated");
        self.draining_state = ControllerSenderDrainStatus::Initiated;
    }

    pub fn remove_participant(&mut self, name: &ProtoName) {
        // this is used only by the moderator when a participant closes its
        // session remotely. This remove is needed so that the moderator
        // will not expect acks from the participant that is leaving the
        // group during the group update phase
        if self.initiator {
            self.group_list.remove(name);
        }
    }

    pub fn drain_completed(&self) -> bool {
        // Drain is complete if we're draining and no pending acks remain
        if self.draining_state == ControllerSenderDrainStatus::Completed
            || self.draining_state == ControllerSenderDrainStatus::Initiated
                && self.pending_replies.is_empty()
        {
            return true;
        }
        false
    }

    pub fn close(&mut self) {
        self.clear_timers();
        self.draining_state = ControllerSenderDrainStatus::Completed;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::{OutboundMessage, SessionOutput};
    use slim_datapath::api::{
        CommandPayload, Participant, ParticipantSettings, ProtoSessionMessageType, ProtoSessionType,
    };
    use std::time::Duration;
    use tokio::time::timeout;
    use tracing_test::traced_test;

    fn single_slim_message(output: SessionOutput) -> Message {
        let mut messages = output
            .messages
            .into_iter()
            .map(|message| match message {
                OutboundMessage::ToSlim(message) => message,
                OutboundMessage::ToApp(_) => panic!("Expected ToSlim message"),
            })
            .collect::<Vec<_>>();
        assert_eq!(messages.len(), 1, "Expected exactly one outbound message");
        messages.pop().unwrap()
    }

    fn assert_no_messages(output: SessionOutput) {
        assert!(output.messages.is_empty(), "Expected no outbound messages");
    }

    async fn expect_timeout(
        rx_signal: &mut tokio::sync::mpsc::Receiver<SessionMessage>,
        wait: Duration,
    ) -> (u32, ProtoSessionMessageType) {
        let timeout_msg = timeout(wait, rx_signal.recv())
            .await
            .expect("timeout waiting for timer signal")
            .expect("channel closed");

        match timeout_msg {
            SessionMessage::TimerTimeout {
                message_id,
                message_type,
                ..
            } => (message_id, message_type),
            other => panic!("Expected TimerTimeout message, got {:?}", other),
        }
    }

    #[tokio::test]
    #[traced_test]
    async fn test_on_discovery_request() {
        let settings = TimerSettings::constant(Duration::from_millis(200)).with_max_retries(3);
        let (tx_signal, mut rx_signal) = tokio::sync::mpsc::channel(10);

        let source = ProtoName::from_strings(["org", "ns", "source"]);
        let remote = ProtoName::from_strings(["org", "ns", "remote"]);
        let session_id = 1;

        let mut sender = ControllerSender::new(
            settings,
            source.clone(),
            ProtoSessionType::Multicast,
            session_id,
            None,
            false,
            tx_signal,
        );

        let request = Message::builder()
            .source(source.clone())
            .destination(remote.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::DiscoveryRequest)
            .session_id(session_id)
            .message_id(1)
            .payload(CommandPayload::builder().discovery_request().as_content())
            .build_publish()
            .unwrap();

        let sent = single_slim_message(sender.on_message(&request).expect("error sending message"));
        assert_eq!(sent, request);

        let (message_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(300)).await;
        assert_eq!(message_id, 1);
        assert_eq!(message_type, ProtoSessionMessageType::DiscoveryRequest);

        let resent = single_slim_message(
            sender
                .on_timer_timeout(1, ProtoSessionMessageType::DiscoveryRequest)
                .expect("error re-sending the request"),
        );
        assert_eq!(resent, request);

        let reply = Message::builder()
            .source(remote.clone())
            .destination(source.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::DiscoveryReply)
            .session_id(session_id)
            .message_id(1)
            .payload(CommandPayload::builder().discovery_reply().as_content())
            .build_publish()
            .unwrap();

        assert_no_messages(sender.on_message(&reply).expect("error sending reply"));
        assert!(
            timeout(Duration::from_millis(400), rx_signal.recv())
                .await
                .is_err()
        );
    }

    #[tokio::test]
    #[traced_test]
    async fn test_on_join_request() {
        let settings = TimerSettings::constant(Duration::from_millis(200)).with_max_retries(3);
        let (tx_signal, mut rx_signal) = tokio::sync::mpsc::channel(10);

        let source = ProtoName::from_strings(["org", "ns", "source"]);
        let remote = ProtoName::from_strings(["org", "ns", "remote"]);
        let channel = ProtoName::from_strings(["org", "ns", "channel"]);
        let session_id = 1;

        let mut sender = ControllerSender::new(
            settings,
            source.clone(),
            ProtoSessionType::Multicast,
            session_id,
            None,
            false,
            tx_signal,
        );

        let request = Message::builder()
            .source(source.clone())
            .destination(remote.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::JoinRequest)
            .session_id(session_id)
            .message_id(1)
            .payload(
                CommandPayload::builder()
                    .join_request(None, None, Some(channel.clone()), None)
                    .as_content(),
            )
            .build_publish()
            .unwrap();

        let sent = single_slim_message(sender.on_message(&request).expect("error sending message"));
        assert_eq!(sent, request);

        let (message_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(300)).await;
        assert_eq!(message_id, 1);
        assert_eq!(message_type, ProtoSessionMessageType::JoinRequest);

        let resent = single_slim_message(
            sender
                .on_timer_timeout(1, ProtoSessionMessageType::JoinRequest)
                .expect("error re-sending the request"),
        );
        assert_eq!(resent, request);

        let reply = Message::builder()
            .source(remote.clone())
            .destination(source.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::JoinReply)
            .session_id(session_id)
            .message_id(1)
            .payload(
                CommandPayload::builder()
                    .join_reply(
                        None,
                        Participant::new(remote.clone(), ParticipantSettings::bidirectional()),
                    )
                    .as_content(),
            )
            .build_publish()
            .unwrap();

        assert_no_messages(sender.on_message(&reply).expect("error sending reply"));
        assert!(
            timeout(Duration::from_millis(400), rx_signal.recv())
                .await
                .is_err()
        );
    }

    #[tokio::test]
    #[traced_test]
    async fn test_on_leave_request() {
        let settings = TimerSettings::constant(Duration::from_millis(200)).with_max_retries(3);
        let (tx_signal, mut rx_signal) = tokio::sync::mpsc::channel(10);

        let source = ProtoName::from_strings(["org", "ns", "source"]);
        let remote = ProtoName::from_strings(["org", "ns", "remote"]);
        let session_id = 1;

        let mut sender = ControllerSender::new(
            settings,
            source.clone(),
            ProtoSessionType::Multicast,
            session_id,
            None,
            false,
            tx_signal,
        );

        let request = Message::builder()
            .source(source.clone())
            .destination(remote.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::LeaveRequest)
            .session_id(session_id)
            .message_id(1)
            .payload(CommandPayload::builder().leave_request().as_content())
            .build_publish()
            .unwrap();

        let sent = single_slim_message(sender.on_message(&request).expect("error sending message"));
        assert_eq!(sent, request);

        let (message_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(300)).await;
        assert_eq!(message_id, 1);
        assert_eq!(message_type, ProtoSessionMessageType::LeaveRequest);

        let resent = single_slim_message(
            sender
                .on_timer_timeout(1, ProtoSessionMessageType::LeaveRequest)
                .expect("error re-sending the request"),
        );
        assert_eq!(resent, request);

        let reply = Message::builder()
            .source(remote.clone())
            .destination(source.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::LeaveReply)
            .session_id(session_id)
            .message_id(1)
            .payload(CommandPayload::builder().leave_reply().as_content())
            .build_publish()
            .unwrap();

        assert_no_messages(sender.on_message(&reply).expect("error sending reply"));
        assert!(
            timeout(Duration::from_millis(400), rx_signal.recv())
                .await
                .is_err()
        );
    }

    #[tokio::test]
    #[traced_test]
    async fn test_on_group_welcome() {
        let settings = TimerSettings::constant(Duration::from_millis(200)).with_max_retries(3);
        let (tx_signal, mut rx_signal) = tokio::sync::mpsc::channel(10);

        let source_name = ProtoName::from_strings(["org", "ns", "source"]);
        let remote_name = ProtoName::from_strings(["org", "ns", "remote"]);
        let source = Participant::new(source_name.clone(), ParticipantSettings::bidirectional());
        let remote = Participant::new(remote_name.clone(), ParticipantSettings::bidirectional());
        let session_id = 1;

        let mut sender = ControllerSender::new(
            settings,
            source_name.clone(),
            ProtoSessionType::Multicast,
            session_id,
            None,
            false,
            tx_signal,
        );

        let welcome = Message::builder()
            .source(source_name.clone())
            .destination(remote_name.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::GroupWelcome)
            .session_id(session_id)
            .message_id(1)
            .payload(
                CommandPayload::builder()
                    .group_welcome(vec![remote.clone(), source.clone()], None)
                    .as_content(),
            )
            .build_publish()
            .unwrap();

        let sent = single_slim_message(sender.on_message(&welcome).expect("error sending message"));
        assert_eq!(sent, welcome);

        let (message_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(300)).await;
        assert_eq!(message_id, 1);
        assert_eq!(message_type, ProtoSessionMessageType::GroupWelcome);

        let resent = single_slim_message(
            sender
                .on_timer_timeout(1, ProtoSessionMessageType::GroupWelcome)
                .expect("error re-sending the welcome"),
        );
        assert_eq!(resent, welcome);

        let ack = Message::builder()
            .source(remote_name.clone())
            .destination(source_name.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::GroupAck)
            .session_id(session_id)
            .message_id(1)
            .payload(CommandPayload::builder().group_ack().as_content())
            .build_publish()
            .unwrap();

        assert_no_messages(sender.on_message(&ack).expect("error sending ack"));
        assert!(
            timeout(Duration::from_millis(400), rx_signal.recv())
                .await
                .is_err()
        );
    }

    #[tokio::test]
    #[traced_test]
    async fn test_on_group_add_message() {
        let settings = TimerSettings::constant(Duration::from_millis(200)).with_max_retries(3);
        let (tx_signal, mut rx_signal) = tokio::sync::mpsc::channel(10);

        let source = ProtoName::from_strings(["org", "ns", "source"]);
        let group = ProtoName::from_strings(["org", "ns", "group"]);
        let session_id = 1;

        let mut sender = ControllerSender::new(
            settings,
            source.clone(),
            ProtoSessionType::Multicast,
            session_id,
            None,
            false,
            tx_signal,
        );

        let participant2 = ProtoName::from_strings(["org", "ns", "participant2"]);
        sender.group_list.insert(participant2.clone());

        let participant1 = ProtoName::from_strings(["org", "ns", "participant1"]);
        let update = Message::builder()
            .source(source.clone())
            .destination(group.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::GroupAdd)
            .session_id(session_id)
            .message_id(1)
            .payload(
                CommandPayload::builder()
                    .group_add(
                        Participant::new(
                            participant1.clone(),
                            ParticipantSettings::bidirectional(),
                        ),
                        vec![
                            Participant::new(
                                participant1.clone(),
                                ParticipantSettings::bidirectional(),
                            ),
                            Participant::new(
                                participant2.clone(),
                                ParticipantSettings::bidirectional(),
                            ),
                            Participant::new(source.clone(), ParticipantSettings::bidirectional()),
                        ],
                        None,
                    )
                    .as_content(),
            )
            .build_publish()
            .unwrap();

        let sent = single_slim_message(sender.on_message(&update).expect("error sending message"));
        assert_eq!(sent, update);

        let (message_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(300)).await;
        assert_eq!(message_id, 1);
        assert_eq!(message_type, ProtoSessionMessageType::GroupAdd);

        let resent = single_slim_message(
            sender
                .on_timer_timeout(1, ProtoSessionMessageType::GroupAdd)
                .expect("error re-sending the add"),
        );
        assert_eq!(resent, update);

        let ack1 = Message::builder()
            .source(participant1.clone())
            .destination(source.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::GroupAck)
            .session_id(session_id)
            .message_id(1)
            .payload(CommandPayload::builder().group_ack().as_content())
            .build_publish()
            .unwrap();
        assert_no_messages(sender.on_message(&ack1).expect("error sending ack"));
        assert!(sender.is_still_pending(1));

        let ack2 = Message::builder()
            .source(participant2.clone())
            .destination(source.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::GroupAck)
            .session_id(session_id)
            .message_id(1)
            .payload(CommandPayload::builder().group_ack().as_content())
            .build_publish()
            .unwrap();
        assert_no_messages(sender.on_message(&ack2).expect("error sending ack"));
        assert!(!sender.is_still_pending(1));
        assert!(
            timeout(Duration::from_millis(100), rx_signal.recv())
                .await
                .is_err()
        );
    }

    #[tokio::test]
    #[traced_test]
    async fn test_on_group_update_duplicate_acks() {
        let settings = TimerSettings::constant(Duration::from_millis(200)).with_max_retries(3);
        let (tx_signal, mut rx_signal) = tokio::sync::mpsc::channel(10);

        let source = ProtoName::from_strings(["org", "ns", "source"]);
        let remote = ProtoName::from_strings(["org", "ns", "remote"]);
        let session_id = 1;

        let mut sender = ControllerSender::new(
            settings,
            source.clone(),
            ProtoSessionType::Multicast,
            session_id,
            None,
            false,
            tx_signal,
        );

        let participant2 = ProtoName::from_strings(["org", "ns", "participant2"]);
        sender.group_list.insert(participant2.clone());

        let participant1 = ProtoName::from_strings(["org", "ns", "participant1"]);
        let update = Message::builder()
            .source(source.clone())
            .destination(remote.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::GroupAdd)
            .session_id(session_id)
            .message_id(1)
            .payload(
                CommandPayload::builder()
                    .group_add(
                        Participant::new(
                            participant1.clone(),
                            ParticipantSettings::bidirectional(),
                        ),
                        vec![
                            Participant::new(
                                participant1.clone(),
                                ParticipantSettings::bidirectional(),
                            ),
                            Participant::new(
                                participant2.clone(),
                                ParticipantSettings::bidirectional(),
                            ),
                            Participant::new(source.clone(), ParticipantSettings::bidirectional()),
                        ],
                        None,
                    )
                    .as_content(),
            )
            .build_publish()
            .unwrap();

        let sent = single_slim_message(sender.on_message(&update).expect("error sending message"));
        assert_eq!(sent, update);

        let (message_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(300)).await;
        assert_eq!(message_id, 1);
        assert_eq!(message_type, ProtoSessionMessageType::GroupAdd);

        let resent = single_slim_message(
            sender
                .on_timer_timeout(1, ProtoSessionMessageType::GroupAdd)
                .expect("error re-sending the add"),
        );
        assert_eq!(resent, update);

        let ack1 = Message::builder()
            .source(participant1.clone())
            .destination(source.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::GroupAck)
            .session_id(session_id)
            .message_id(1)
            .payload(CommandPayload::builder().group_ack().as_content())
            .build_publish()
            .unwrap();
        assert_no_messages(sender.on_message(&ack1).expect("error sending ack"));
        assert!(sender.is_still_pending(1));

        assert_no_messages(
            sender
                .on_message(&ack1)
                .expect("error sending duplicate ack"),
        );
        assert!(sender.is_still_pending(1));

        let (message_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(300)).await;
        assert_eq!(message_id, 1);
        assert_eq!(message_type, ProtoSessionMessageType::GroupAdd);

        let retransmitted = single_slim_message(
            sender
                .on_timer_timeout(1, ProtoSessionMessageType::GroupAdd)
                .expect("error re-sending the add"),
        );
        assert_eq!(retransmitted, update);

        let ack2 = Message::builder()
            .source(participant2.clone())
            .destination(source.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::GroupAck)
            .session_id(session_id)
            .message_id(1)
            .payload(CommandPayload::builder().group_ack().as_content())
            .build_publish()
            .unwrap();
        assert_no_messages(sender.on_message(&ack2).expect("error sending ack"));
        assert!(!sender.is_still_pending(1));
        assert!(
            timeout(Duration::from_millis(100), rx_signal.recv())
                .await
                .is_err()
        );
    }

    #[tokio::test]
    #[traced_test]
    async fn test_ping_with_retransmissions_and_disconnection() {
        let settings = TimerSettings::constant(Duration::from_millis(400)).with_max_retries(3);
        let ping_interval = Duration::from_millis(1000);
        let (tx_signal, mut rx_signal) = tokio::sync::mpsc::channel(100);

        let source = ProtoName::from_strings(["org", "ns", "source"]);
        let participant = ProtoName::from_strings(["org", "ns", "participant"]);
        let session_id = 1;

        let mut sender = ControllerSender::new(
            settings,
            source.clone(),
            ProtoSessionType::Multicast,
            session_id,
            Some(ping_interval),
            true,
            tx_signal,
        );

        sender.group_list.insert(participant.clone());
        sender.group_name = Some(participant.clone());

        let (first_ping_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(1100)).await;
        assert_eq!(message_type, ProtoSessionMessageType::Ping);
        let first_ping = single_slim_message(
            sender
                .on_timer_timeout(first_ping_id, ProtoSessionMessageType::Ping)
                .expect("error sending first ping"),
        );
        assert_eq!(
            first_ping.get_session_message_type(),
            ProtoSessionMessageType::Ping
        );

        let ping_reply = Message::builder()
            .source(participant.clone())
            .destination(source.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::Ping)
            .session_id(session_id)
            .message_id(first_ping.get_id())
            .payload(CommandPayload::builder().ping().as_content())
            .build_publish()
            .unwrap();
        sender.on_ping_message(&ping_reply);
        assert!(
            timeout(Duration::from_millis(500), rx_signal.recv())
                .await
                .is_err()
        );

        let (second_ping_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(1100)).await;
        assert_eq!(message_type, ProtoSessionMessageType::Ping);
        let second_ping = single_slim_message(
            sender
                .on_timer_timeout(second_ping_id, ProtoSessionMessageType::Ping)
                .expect("error sending second ping"),
        );
        assert_eq!(
            second_ping.get_session_message_type(),
            ProtoSessionMessageType::Ping
        );

        for _ in 0..2 {
            let (message_id, message_type) =
                expect_timeout(&mut rx_signal, Duration::from_millis(500)).await;
            assert_eq!(message_id, second_ping.get_id());
            assert_eq!(message_type, ProtoSessionMessageType::Ping);
            let retransmitted = single_slim_message(
                sender
                    .on_timer_timeout(second_ping.get_id(), ProtoSessionMessageType::Ping)
                    .expect("error retransmitting ping"),
            );
            assert_eq!(retransmitted.get_id(), second_ping.get_id());
        }

        let (third_ping_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(1100)).await;
        assert_eq!(message_type, ProtoSessionMessageType::Ping);
        let third_ping = single_slim_message(
            sender
                .on_timer_timeout(third_ping_id, ProtoSessionMessageType::Ping)
                .expect("error sending third ping"),
        );
        assert_eq!(
            third_ping.get_session_message_type(),
            ProtoSessionMessageType::Ping
        );

        for _ in 0..2 {
            let (message_id, message_type) =
                expect_timeout(&mut rx_signal, Duration::from_millis(500)).await;
            assert_eq!(message_id, third_ping.get_id());
            assert_eq!(message_type, ProtoSessionMessageType::Ping);
            let retransmitted = single_slim_message(
                sender
                    .on_timer_timeout(third_ping.get_id(), ProtoSessionMessageType::Ping)
                    .expect("error retransmitting ping"),
            );
            assert_eq!(retransmitted.get_id(), third_ping.get_id());
        }

        let (fourth_ping_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(1100)).await;
        assert_eq!(message_type, ProtoSessionMessageType::Ping);
        let fourth_ping = single_slim_message(
            sender
                .on_timer_timeout(fourth_ping_id, ProtoSessionMessageType::Ping)
                .expect("error sending fourth ping"),
        );
        assert_eq!(
            fourth_ping.get_session_message_type(),
            ProtoSessionMessageType::Ping
        );

        for _ in 0..2 {
            let (message_id, message_type) =
                expect_timeout(&mut rx_signal, Duration::from_millis(500)).await;
            assert_eq!(message_id, fourth_ping.get_id());
            assert_eq!(message_type, ProtoSessionMessageType::Ping);
            let retransmitted = single_slim_message(
                sender
                    .on_timer_timeout(fourth_ping.get_id(), ProtoSessionMessageType::Ping)
                    .expect("error retransmitting ping"),
            );
            assert_eq!(retransmitted.get_id(), fourth_ping.get_id());
        }

        let (fifth_ping_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(1100)).await;
        assert_eq!(message_type, ProtoSessionMessageType::Ping);
        assert_no_messages(
            sender
                .on_timer_timeout(fifth_ping_id, ProtoSessionMessageType::Ping)
                .expect("error handling fifth ping interval"),
        );

        if let Some(ping_state) = &sender.ping_state {
            assert_eq!(ping_state.missing_pings.get(&participant), None);
        } else {
            panic!("Ping state should be initialized");
        }
        assert!(!sender.group_list.contains(&participant));
    }

    #[tokio::test]
    #[traced_test]
    async fn test_participant_detects_moderator_disconnection() {
        let settings = TimerSettings::constant(Duration::from_millis(400)).with_max_retries(3);
        let ping_interval = Duration::from_millis(1000);
        let (tx_signal, mut rx_signal) = tokio::sync::mpsc::channel(100);

        let participant_name = ProtoName::from_strings(["org", "ns", "participant"]);
        let moderator_name = ProtoName::from_strings(["org", "ns", "moderator"]);
        let channel_name = ProtoName::from_strings(["org", "ns", "channel"]);
        let session_id = 1;

        let mut sender = ControllerSender::new(
            settings,
            participant_name.clone(),
            ProtoSessionType::Multicast,
            session_id,
            Some(ping_interval),
            false,
            tx_signal,
        );

        let (first_ping_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(1100)).await;
        assert_eq!(message_type, ProtoSessionMessageType::Ping);

        let ping_from_moderator = Message::builder()
            .source(moderator_name.clone())
            .destination(channel_name.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::Ping)
            .session_id(session_id)
            .message_id(rand::random::<u32>())
            .payload(CommandPayload::builder().ping().as_content())
            .build_publish()
            .unwrap();
        sender.on_ping_message(&ping_from_moderator);
        assert_no_messages(
            sender
                .on_timer_timeout(first_ping_id, ProtoSessionMessageType::Ping)
                .expect("error handling first ping timeout"),
        );

        if let Some(ping_state) = &sender.ping_state {
            assert_eq!(ping_state.missing_pings.len(), 0);
        }

        let (second_ping_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(1100)).await;
        assert_eq!(message_type, ProtoSessionMessageType::Ping);
        assert_no_messages(
            sender
                .on_timer_timeout(second_ping_id, ProtoSessionMessageType::Ping)
                .expect("error handling second ping timeout"),
        );
        if let Some(ping_state) = &sender.ping_state {
            assert_eq!(
                ping_state.missing_pings.get(&MODERATOR_NAME).copied(),
                Some(1)
            );
        }

        let (third_ping_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(1100)).await;
        assert_eq!(message_type, ProtoSessionMessageType::Ping);
        assert_no_messages(
            sender
                .on_timer_timeout(third_ping_id, ProtoSessionMessageType::Ping)
                .expect("error handling third ping timeout"),
        );
        if let Some(ping_state) = &sender.ping_state {
            assert_eq!(
                ping_state.missing_pings.get(&MODERATOR_NAME).copied(),
                Some(2)
            );
        }

        let (fourth_ping_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(1100)).await;
        assert_eq!(message_type, ProtoSessionMessageType::Ping);
        assert_no_messages(
            sender
                .on_timer_timeout(fourth_ping_id, ProtoSessionMessageType::Ping)
                .expect("error handling fourth ping timeout"),
        );

        match rx_signal.try_recv() {
            Ok(SessionMessage::ParticipantDisconnected { name }) => assert_eq!(name, None),
            Ok(other) => panic!("Expected ParticipantDisconnected message, got {:?}", other),
            Err(e) => panic!(
                "Expected ParticipantDisconnected message, channel error: {:?}",
                e
            ),
        }
    }

    #[tokio::test]
    #[traced_test]
    async fn test_ping_with_two_participants_one_removed_before_reply() {
        let settings = TimerSettings::constant(Duration::from_secs(1000)).with_max_retries(3);
        let ping_interval = Duration::from_millis(1000);
        let (tx_signal, mut rx_signal) = tokio::sync::mpsc::channel(100);

        let moderator = ProtoName::from_strings(["org", "ns", "moderator"]);
        let participant1 = ProtoName::from_strings(["org", "ns", "participant1"]);
        let participant2 = ProtoName::from_strings(["org", "ns", "participant2"]);
        let group_name = ProtoName::from_strings(["org", "ns", "group"]);
        let session_id = 1;

        let mut sender = ControllerSender::new(
            settings,
            moderator.clone(),
            ProtoSessionType::Multicast,
            session_id,
            Some(ping_interval),
            true,
            tx_signal,
        );

        sender.group_name = Some(group_name.clone());
        sender.group_list.insert(moderator.clone());
        sender.group_list.insert(participant1.clone());
        sender.group_list.insert(participant2.clone());

        let (first_ping_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(1100)).await;
        assert_eq!(message_type, ProtoSessionMessageType::Ping);
        let ping_msg = single_slim_message(
            sender
                .on_timer_timeout(first_ping_id, ProtoSessionMessageType::Ping)
                .expect("error sending first ping"),
        );
        assert_eq!(
            ping_msg.get_session_message_type(),
            ProtoSessionMessageType::Ping
        );
        let ping_message_id = ping_msg.get_id();

        if let Some(ping_state) = &sender.ping_state {
            if let Some(ping) = &ping_state.ping {
                assert_eq!(ping.missing_replies.len(), 2);
                assert!(ping.missing_replies.contains(&participant1));
                assert!(ping.missing_replies.contains(&participant2));
            } else {
                panic!("Ping should be set");
            }
        } else {
            panic!("Ping state should be initialized");
        }

        sender.remove_participant(&participant1);
        assert!(!sender.group_list.contains(&participant1));
        assert!(sender.group_list.contains(&participant2));

        let ping_reply = Message::builder()
            .source(participant2.clone())
            .destination(moderator.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::Ping)
            .session_id(session_id)
            .message_id(ping_message_id)
            .payload(CommandPayload::builder().ping().as_content())
            .build_publish()
            .unwrap();
        sender.on_ping_message(&ping_reply);

        if let Some(ping_state) = &sender.ping_state {
            if let Some(ping) = &ping_state.ping {
                assert_eq!(ping.missing_replies.len(), 1);
                assert!(ping.missing_replies.contains(&participant1));
                assert!(!ping.missing_replies.contains(&participant2));
            } else {
                panic!("Ping should still be set");
            }
        } else {
            panic!("Ping state should be initialized");
        }

        let (second_ping_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(1100)).await;
        assert_eq!(message_type, ProtoSessionMessageType::Ping);
        let second_ping = single_slim_message(
            sender
                .on_timer_timeout(second_ping_id, ProtoSessionMessageType::Ping)
                .expect("error handling second ping interval"),
        );

        if let Some(ping_state) = &sender.ping_state {
            assert_eq!(ping_state.missing_pings.get(&participant1), None);
            assert_eq!(ping_state.missing_pings.len(), 0);
            if let Some(ping) = &ping_state.ping {
                assert_eq!(ping.missing_replies.len(), 1);
                assert!(ping.missing_replies.contains(&participant2));
                assert!(!ping.missing_replies.contains(&participant1));
            } else {
                panic!("Ping should be set");
            }
        } else {
            panic!("Ping state should be initialized");
        }

        assert_eq!(
            second_ping.get_session_message_type(),
            ProtoSessionMessageType::Ping
        );
    }

    #[tokio::test]
    #[traced_test]
    async fn test_ping_destination_p2p_session() {
        let settings = TimerSettings::constant(Duration::from_millis(400)).with_max_retries(3);
        let ping_interval = Duration::from_millis(1000);
        let (tx_signal, mut rx_signal) = tokio::sync::mpsc::channel(100);

        let source = ProtoName::from_strings(["org", "ns", "source"]);
        let remote = ProtoName::from_strings(["org", "ns", "remote"]);
        let session_id = 1;

        let mut sender = ControllerSender::new(
            settings,
            source.clone(),
            ProtoSessionType::PointToPoint,
            session_id,
            Some(ping_interval),
            true,
            tx_signal,
        );

        let join_request = Message::builder()
            .source(source.clone())
            .destination(remote.clone())
            .identity("")
            .session_type(ProtoSessionType::PointToPoint)
            .session_message_type(ProtoSessionMessageType::JoinRequest)
            .session_id(session_id)
            .message_id(1)
            .payload(
                CommandPayload::builder()
                    .join_request(None, None, None, None)
                    .as_content(),
            )
            .build_publish()
            .unwrap();
        let join_msg = single_slim_message(
            sender
                .on_message(&join_request)
                .expect("error sending join request"),
        );
        assert_eq!(sender.group_name, Some(remote.clone()));

        let join_reply = Message::builder()
            .source(remote.clone())
            .destination(source.clone())
            .identity("")
            .session_type(ProtoSessionType::PointToPoint)
            .session_message_type(ProtoSessionMessageType::JoinReply)
            .session_id(session_id)
            .message_id(join_msg.get_id())
            .payload(
                CommandPayload::builder()
                    .join_reply(
                        None,
                        Participant::new(remote.clone(), ParticipantSettings::bidirectional()),
                    )
                    .as_content(),
            )
            .build_publish()
            .unwrap();
        assert_no_messages(
            sender
                .on_message(&join_reply)
                .expect("error sending join reply"),
        );

        sender.group_list.insert(remote.clone());

        let (ping_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(1100)).await;
        assert_eq!(message_type, ProtoSessionMessageType::Ping);
        let ping = single_slim_message(
            sender
                .on_timer_timeout(ping_id, ProtoSessionMessageType::Ping)
                .expect("error sending ping"),
        );
        assert_eq!(
            ping.get_session_message_type(),
            ProtoSessionMessageType::Ping
        );
        assert_eq!(ping.get_dst(), remote);
    }

    #[tokio::test]
    #[traced_test]
    async fn test_ping_destination_multicast_session() {
        let settings = TimerSettings::constant(Duration::from_millis(400)).with_max_retries(3);
        let ping_interval = Duration::from_millis(1000);
        let (tx_signal, mut rx_signal) = tokio::sync::mpsc::channel(100);

        let source = ProtoName::from_strings(["org", "ns", "source"]);
        let data_channel_name =
            ProtoName::from_strings(["org", "ns", "channel"]).with_id(NameId::DATA_CHANNEL_ID);
        let control_channel_name =
            ProtoName::from_strings(["org", "ns", "channel"]).with_id(NameId::CONTROL_CHANNEL_ID);
        let participant = ProtoName::from_strings(["org", "ns", "participant"]);
        let session_id = 1;

        let mut sender = ControllerSender::new(
            settings,
            source.clone(),
            ProtoSessionType::Multicast,
            session_id,
            Some(ping_interval),
            true,
            tx_signal,
        );

        let join_request = Message::builder()
            .source(source.clone())
            .destination(participant.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::JoinRequest)
            .session_id(session_id)
            .message_id(1)
            .fanout(256)
            .payload(
                CommandPayload::builder()
                    .join_request(None, None, Some(data_channel_name.clone()), None)
                    .as_content(),
            )
            .build_publish()
            .unwrap();
        let join_msg = single_slim_message(
            sender
                .on_message(&join_request)
                .expect("error sending join request"),
        );
        assert_eq!(sender.group_name, Some(control_channel_name.clone()));

        let join_reply = Message::builder()
            .source(participant.clone())
            .destination(source.clone())
            .identity("")
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::JoinReply)
            .session_id(session_id)
            .message_id(join_msg.get_id())
            .fanout(256)
            .payload(
                CommandPayload::builder()
                    .join_reply(
                        None,
                        Participant::new(participant.clone(), ParticipantSettings::bidirectional()),
                    )
                    .as_content(),
            )
            .build_publish()
            .unwrap();
        assert_no_messages(
            sender
                .on_message(&join_reply)
                .expect("error sending join reply"),
        );

        sender.group_list.insert(participant.clone());

        let (ping_id, message_type) =
            expect_timeout(&mut rx_signal, Duration::from_millis(1100)).await;
        assert_eq!(message_type, ProtoSessionMessageType::Ping);
        let ping = single_slim_message(
            sender
                .on_timer_timeout(ping_id, ProtoSessionMessageType::Ping)
                .expect("error sending ping"),
        );
        assert_eq!(
            ping.get_session_message_type(),
            ProtoSessionMessageType::Ping
        );
        assert_eq!(ping.get_dst(), control_channel_name);
    }
}