consortium-crate 0.2.0

Core library — Rust implementation of ClusterShell
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
//! Inter-node messaging protocol.
//!
//! Rust implementation of `ClusterShell.Communication`.
//!
//! Messages are the fundamental units of communication between nodes in the
//! propagation tree. They are serialized to/from XML for transmission over
//! SSH channels. Each message has a type identifier, a unique message ID,
//! optional attributes, and an optional payload (base64-encoded bytes).
//!
//! The [`Channel`] struct manages bidirectional communication using an
//! [`XmlReader`] for parsing incoming XML and XML generation for outgoing
//! messages.

use std::collections::{HashMap, VecDeque};
use std::sync::atomic::{AtomicU64, Ordering};

use thiserror::Error;

// ============================================================================
// Constants
// ============================================================================

/// XML character encoding
pub const ENCODING: &str = "utf-8";

/// Default base64 line length for large payloads
pub const DEFAULT_B64_LINE_LENGTH: usize = 65536;

// Channel stream name constants
/// Stream name for the writer side of a channel
pub const SNAME_WRITER: &str = "ch-writer";
/// Stream name for the reader side of a channel
pub const SNAME_READER: &str = "ch-reader";
/// Stream name for the error side of a channel
pub const SNAME_ERROR: &str = "ch-error";

// Message type identifier constants
const IDENT_GEN: &str = "GEN";
const IDENT_CFG: &str = "CFG";
const IDENT_CTL: &str = "CTL";
const IDENT_ACK: &str = "ACK";
const IDENT_ERR: &str = "ERR";
const IDENT_OUT: &str = "OUT";
const IDENT_SER: &str = "SER";
const IDENT_RET: &str = "RET";
const IDENT_TIM: &str = "TIM";
const IDENT_RTR: &str = "RTR";
const IDENT_CHA: &str = "CHA";
const IDENT_END: &str = "END";

// ============================================================================
// Errors
// ============================================================================

/// Errors raised during message processing.
#[derive(Error, Debug)]
pub enum MessageProcessingError {
    /// An unknown or invalid message type was encountered.
    #[error("Unknown message type: {0}")]
    UnknownType(String),

    /// A required attribute is missing from a message.
    #[error("Invalid message attributes: missing key \"{0}\"")]
    MissingAttribute(String),

    /// An unexpected payload was found on a message that doesn't support one.
    #[error("Got unexpected payload for Message {0}")]
    UnexpectedPayload(String),

    /// The message payload is invalid or corrupted.
    #[error("Message {0} has an invalid payload")]
    InvalidPayload(String),

    /// XML parse error.
    #[error("Parse error: {0}")]
    ParseError(String),

    /// Invalid starting XML tag.
    #[error("Invalid starting tag {0}")]
    InvalidTag(String),
}

// ============================================================================
// Global message ID counter
// ============================================================================

static MSG_COUNTER: AtomicU64 = AtomicU64::new(0);

fn next_msg_id() -> u64 {
    MSG_COUNTER.fetch_add(1, Ordering::Relaxed)
}

/// Reset the global message counter (for testing only).
#[cfg(test)]
fn reset_msg_counter() {
    MSG_COUNTER.store(0, Ordering::Relaxed);
}

// ============================================================================
// Minimal base64
// ============================================================================

/// Simple base64 alphabet (standard RFC 4648).
const B64_CHARS: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/// Encode bytes to base64 string.
pub fn base64_encode(input: &[u8]) -> String {
    let mut out = String::with_capacity((input.len() + 2) / 3 * 4);
    for chunk in input.chunks(3) {
        let b0 = chunk[0] as u32;
        let b1 = if chunk.len() > 1 { chunk[1] as u32 } else { 0 };
        let b2 = if chunk.len() > 2 { chunk[2] as u32 } else { 0 };
        let triple = (b0 << 16) | (b1 << 8) | b2;

        out.push(B64_CHARS[((triple >> 18) & 0x3F) as usize] as char);
        out.push(B64_CHARS[((triple >> 12) & 0x3F) as usize] as char);
        if chunk.len() > 1 {
            out.push(B64_CHARS[((triple >> 6) & 0x3F) as usize] as char);
        } else {
            out.push('=');
        }
        if chunk.len() > 2 {
            out.push(B64_CHARS[(triple & 0x3F) as usize] as char);
        } else {
            out.push('=');
        }
    }
    out
}

/// Decode base64 string to bytes. Ignores whitespace/newlines (RFC 4648 relaxed).
pub fn base64_decode(input: &str) -> Result<Vec<u8>, MessageProcessingError> {
    fn b64_val(c: u8) -> Result<u8, MessageProcessingError> {
        match c {
            b'A'..=b'Z' => Ok(c - b'A'),
            b'a'..=b'z' => Ok(c - b'a' + 26),
            b'0'..=b'9' => Ok(c - b'0' + 52),
            b'+' => Ok(62),
            b'/' => Ok(63),
            _ => Err(MessageProcessingError::InvalidPayload(
                "invalid base64 character".into(),
            )),
        }
    }

    // Strip whitespace
    let clean: Vec<u8> = input.bytes().filter(|b| !b.is_ascii_whitespace()).collect();
    if clean.is_empty() {
        return Ok(Vec::new());
    }

    let mut out = Vec::with_capacity(clean.len() * 3 / 4);
    for chunk in clean.chunks(4) {
        if chunk.len() < 2 {
            return Err(MessageProcessingError::InvalidPayload(
                "truncated base64".into(),
            ));
        }
        let a = b64_val(chunk[0])?;
        let b = b64_val(chunk[1])?;
        out.push((a << 2) | (b >> 4));
        if chunk.len() > 2 && chunk[2] != b'=' {
            let c = b64_val(chunk[2])?;
            out.push((b << 4) | (c >> 2));
            if chunk.len() > 3 && chunk[3] != b'=' {
                let d = b64_val(chunk[3])?;
                out.push((c << 6) | d);
            }
        }
    }
    Ok(out)
}

// ============================================================================
// XML escape helpers
// ============================================================================

fn xml_escape(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            '\'' => out.push_str("&apos;"),
            _ => out.push(c),
        }
    }
    out
}

fn xml_unescape(s: &str) -> String {
    s.replace("&amp;", "&")
        .replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "\"")
        .replace("&apos;", "'")
}

// ============================================================================
// Message
// ============================================================================

/// A communication message in the propagation tree protocol.
///
/// Messages are tagged unions representing the different message types
/// exchanged between ClusterShell nodes. Each variant carries the fields
/// specific to that message type.
#[derive(Debug, Clone)]
pub enum Message {
    /// Generic base message.
    General { msgid: u64, data: Option<Vec<u8>> },

    /// Configuration propagation container (ident = "CFG").
    /// Has payload. Carries serialized topology.
    Configuration {
        msgid: u64,
        gateway: String,
        data: Option<Vec<u8>>,
    },

    /// Action request routed through the tree (ident = "CTL").
    /// Has payload. Carries serialized command data.
    Control {
        msgid: u64,
        srcid: u64,
        action: String,
        target: String,
        data: Option<Vec<u8>>,
    },

    /// Acknowledgement message (ident = "ACK").
    Ack { msgid: u64, ack: u64 },

    /// Error message (ident = "ERR").
    Error { msgid: u64, reason: String },

    /// Standard output container (ident = "OUT").
    /// Has payload. Carries encoded output data.
    StdOut {
        msgid: u64,
        srcid: u64,
        nodes: String,
        data: Option<Vec<u8>>,
    },

    /// Standard error container (ident = "SER").
    /// Has payload. Carries encoded stderr data.
    StdErr {
        msgid: u64,
        srcid: u64,
        nodes: String,
        data: Option<Vec<u8>>,
    },

    /// Return code container (ident = "RET").
    Retcode {
        msgid: u64,
        srcid: u64,
        retcode: i32,
        nodes: String,
    },

    /// Timeout notification (ident = "TIM").
    Timeout {
        msgid: u64,
        srcid: u64,
        nodes: String,
    },

    /// Routing notification (ident = "RTR").
    Routing {
        msgid: u64,
        srcid: u64,
        event: String,
        gateway: String,
        targets: String,
    },

    /// Start of channel communication (ident = "CHA").
    Start { msgid: u64 },

    /// End of channel communication (ident = "END").
    End { msgid: u64 },
}

impl Message {
    // -- Constructors --------------------------------------------------------

    /// Create a new General message.
    pub fn general() -> Self {
        Message::General {
            msgid: next_msg_id(),
            data: None,
        }
    }

    /// Create a new Configuration message.
    pub fn configuration(gateway: &str) -> Self {
        Message::Configuration {
            msgid: next_msg_id(),
            gateway: gateway.to_string(),
            data: None,
        }
    }

    /// Create a new Control message.
    pub fn control(srcid: u64) -> Self {
        Message::Control {
            msgid: next_msg_id(),
            srcid,
            action: String::new(),
            target: String::new(),
            data: None,
        }
    }

    /// Create a new Ack message.
    pub fn ack(ackid: u64) -> Self {
        Message::Ack {
            msgid: next_msg_id(),
            ack: ackid,
        }
    }

    /// Create a new Error message.
    pub fn error(reason: &str) -> Self {
        Message::Error {
            msgid: next_msg_id(),
            reason: reason.to_string(),
        }
    }

    /// Create a new StdOut message.
    pub fn stdout(nodes: &str, srcid: u64) -> Self {
        Message::StdOut {
            msgid: next_msg_id(),
            srcid,
            nodes: nodes.to_string(),
            data: None,
        }
    }

    /// Create a new StdErr message.
    pub fn stderr(nodes: &str, srcid: u64) -> Self {
        Message::StdErr {
            msgid: next_msg_id(),
            srcid,
            nodes: nodes.to_string(),
            data: None,
        }
    }

    /// Create a new Retcode message.
    pub fn retcode(nodes: &str, retcode: i32, srcid: u64) -> Self {
        Message::Retcode {
            msgid: next_msg_id(),
            srcid,
            retcode,
            nodes: nodes.to_string(),
        }
    }

    /// Create a new Timeout message.
    pub fn timeout(nodes: &str, srcid: u64) -> Self {
        Message::Timeout {
            msgid: next_msg_id(),
            srcid,
            nodes: nodes.to_string(),
        }
    }

    /// Create a new Routing message.
    pub fn routing(event: &str, gateway: &str, targets: &str, srcid: u64) -> Self {
        Message::Routing {
            msgid: next_msg_id(),
            srcid,
            event: event.to_string(),
            gateway: gateway.to_string(),
            targets: targets.to_string(),
        }
    }

    /// Create a Start message.
    pub fn start() -> Self {
        Message::Start {
            msgid: next_msg_id(),
        }
    }

    /// Create an End message.
    pub fn end() -> Self {
        Message::End {
            msgid: next_msg_id(),
        }
    }

    // -- Accessors -----------------------------------------------------------

    /// Get the message type identifier string.
    pub fn ident(&self) -> &str {
        match self {
            Message::General { .. } => IDENT_GEN,
            Message::Configuration { .. } => IDENT_CFG,
            Message::Control { .. } => IDENT_CTL,
            Message::Ack { .. } => IDENT_ACK,
            Message::Error { .. } => IDENT_ERR,
            Message::StdOut { .. } => IDENT_OUT,
            Message::StdErr { .. } => IDENT_SER,
            Message::Retcode { .. } => IDENT_RET,
            Message::Timeout { .. } => IDENT_TIM,
            Message::Routing { .. } => IDENT_RTR,
            Message::Start { .. } => IDENT_CHA,
            Message::End { .. } => IDENT_END,
        }
    }

    /// Get the message ID.
    pub fn msgid(&self) -> u64 {
        match self {
            Message::General { msgid, .. }
            | Message::Configuration { msgid, .. }
            | Message::Control { msgid, .. }
            | Message::Ack { msgid, .. }
            | Message::Error { msgid, .. }
            | Message::StdOut { msgid, .. }
            | Message::StdErr { msgid, .. }
            | Message::Retcode { msgid, .. }
            | Message::Timeout { msgid, .. }
            | Message::Routing { msgid, .. }
            | Message::Start { msgid, .. }
            | Message::End { msgid, .. } => *msgid,
        }
    }

    /// Whether this message type carries a payload.
    pub fn has_payload(&self) -> bool {
        matches!(
            self,
            Message::General { .. }
                | Message::Configuration { .. }
                | Message::Control { .. }
                | Message::StdOut { .. }
                | Message::StdErr { .. }
        )
    }

    /// Get a reference to the payload data, if any.
    pub fn data(&self) -> Option<&[u8]> {
        match self {
            Message::General { data, .. }
            | Message::Configuration { data, .. }
            | Message::Control { data, .. }
            | Message::StdOut { data, .. }
            | Message::StdErr { data, .. } => data.as_deref(),
            _ => None,
        }
    }

    /// Set the payload data (raw bytes, will be base64-encoded on serialization).
    pub fn set_data(&mut self, payload: Vec<u8>) {
        match self {
            Message::General { data, .. }
            | Message::Configuration { data, .. }
            | Message::Control { data, .. }
            | Message::StdOut { data, .. }
            | Message::StdErr { data, .. } => {
                *data = Some(payload);
            }
            _ => {}
        }
    }

    /// Encode arbitrary bytes as base64 and store as payload.
    pub fn data_encode(&mut self, raw: &[u8]) {
        let encoded = base64_encode(raw);
        self.set_data(encoded.into_bytes());
    }

    /// Decode the base64 payload back to raw bytes.
    pub fn data_decode(&self) -> Result<Vec<u8>, MessageProcessingError> {
        match self.data() {
            Some(d) => {
                let s = std::str::from_utf8(d)
                    .map_err(|_| MessageProcessingError::InvalidPayload(self.ident().into()))?;
                base64_decode(s)
            }
            None => Err(MessageProcessingError::InvalidPayload(self.ident().into())),
        }
    }

    /// Append data during incremental XML parsing.
    pub fn data_update(&mut self, raw: &[u8]) -> Result<(), MessageProcessingError> {
        if !self.has_payload() {
            return Err(MessageProcessingError::UnexpectedPayload(
                self.ident().into(),
            ));
        }
        match self {
            Message::General { data, .. }
            | Message::Configuration { data, .. }
            | Message::Control { data, .. }
            | Message::StdOut { data, .. }
            | Message::StdErr { data, .. } => {
                if let Some(ref mut d) = data {
                    d.extend_from_slice(raw);
                } else {
                    *data = Some(raw.to_vec());
                }
            }
            _ => {}
        }
        Ok(())
    }

    /// Get the srcid for routed messages, None for non-routed.
    pub fn srcid(&self) -> Option<u64> {
        match self {
            Message::Control { srcid, .. }
            | Message::StdOut { srcid, .. }
            | Message::StdErr { srcid, .. }
            | Message::Retcode { srcid, .. }
            | Message::Timeout { srcid, .. }
            | Message::Routing { srcid, .. } => Some(*srcid),
            _ => None,
        }
    }

    /// Get the nodes field if present.
    pub fn nodes(&self) -> Option<&str> {
        match self {
            Message::StdOut { nodes, .. }
            | Message::StdErr { nodes, .. }
            | Message::Retcode { nodes, .. }
            | Message::Timeout { nodes, .. } => Some(nodes.as_str()),
            _ => None,
        }
    }

    // -- XML serialization ---------------------------------------------------

    /// Serialize this message to XML bytes.
    ///
    /// Produces output like:
    /// `<message type="CFG" msgid="0" gateway="gw1">base64payload</message>`
    pub fn xml(&self) -> Vec<u8> {
        let mut attrs = Vec::new();

        // type and msgid are always present
        attrs.push(format!("type=\"{}\"", xml_escape(self.ident())));
        attrs.push(format!("msgid=\"{}\"", self.msgid()));

        // variant-specific attributes
        match self {
            Message::Configuration { gateway, .. } => {
                attrs.push(format!("gateway=\"{}\"", xml_escape(gateway)));
            }
            Message::Control {
                srcid,
                action,
                target,
                ..
            } => {
                attrs.push(format!("srcid=\"{}\"", srcid));
                attrs.push(format!("action=\"{}\"", xml_escape(action)));
                attrs.push(format!("target=\"{}\"", xml_escape(target)));
            }
            Message::Ack { ack, .. } => {
                attrs.push(format!("ack=\"{}\"", ack));
            }
            Message::Error { reason, .. } => {
                attrs.push(format!("reason=\"{}\"", xml_escape(reason)));
            }
            Message::StdOut { srcid, nodes, .. } => {
                attrs.push(format!("srcid=\"{}\"", srcid));
                attrs.push(format!("nodes=\"{}\"", xml_escape(nodes)));
            }
            Message::StdErr { srcid, nodes, .. } => {
                attrs.push(format!("srcid=\"{}\"", srcid));
                attrs.push(format!("nodes=\"{}\"", xml_escape(nodes)));
            }
            Message::Retcode {
                srcid,
                retcode,
                nodes,
                ..
            } => {
                attrs.push(format!("srcid=\"{}\"", srcid));
                attrs.push(format!("retcode=\"{}\"", retcode));
                attrs.push(format!("nodes=\"{}\"", xml_escape(nodes)));
            }
            Message::Timeout { srcid, nodes, .. } => {
                attrs.push(format!("srcid=\"{}\"", srcid));
                attrs.push(format!("nodes=\"{}\"", xml_escape(nodes)));
            }
            Message::Routing {
                srcid,
                event,
                gateway,
                targets,
                ..
            } => {
                attrs.push(format!("srcid=\"{}\"", srcid));
                attrs.push(format!("event=\"{}\"", xml_escape(event)));
                attrs.push(format!("gateway=\"{}\"", xml_escape(gateway)));
                attrs.push(format!("targets=\"{}\"", xml_escape(targets)));
            }
            _ => {}
        }

        let attr_str = attrs.join(" ");
        let payload = self.data().unwrap_or(&[]);

        if payload.is_empty() {
            format!("<message {}/>", attr_str).into_bytes()
        } else {
            let payload_str = String::from_utf8_lossy(payload);
            format!("<message {}>{}</message>", attr_str, payload_str).into_bytes()
        }
    }

    /// Build a message from parsed XML attributes.
    ///
    /// The message type is selected based on the "type" attribute, and all
    /// other attributes are read according to the variant's schema.
    pub fn from_attrs(attrs: &HashMap<String, String>) -> Result<Message, MessageProcessingError> {
        let msg_type = attrs
            .get("type")
            .ok_or_else(|| MessageProcessingError::MissingAttribute("type".into()))?;

        let msgid: u64 = attrs
            .get("msgid")
            .and_then(|v| v.parse().ok())
            .unwrap_or_else(next_msg_id);

        match msg_type.as_str() {
            IDENT_CFG => {
                let gateway = attrs.get("gateway").cloned().unwrap_or_default();
                Ok(Message::Configuration {
                    msgid,
                    gateway,
                    data: None,
                })
            }
            IDENT_CTL => {
                let srcid = attr_u64(attrs, "srcid")?;
                let action = attrs.get("action").cloned().unwrap_or_default();
                let target = attrs.get("target").cloned().unwrap_or_default();
                Ok(Message::Control {
                    msgid,
                    srcid,
                    action,
                    target,
                    data: None,
                })
            }
            IDENT_ACK => {
                let ack = attr_u64(attrs, "ack")?;
                Ok(Message::Ack { msgid, ack })
            }
            IDENT_ERR => {
                let reason = attrs.get("reason").cloned().unwrap_or_default();
                Ok(Message::Error { msgid, reason })
            }
            IDENT_OUT => {
                let srcid = attr_u64(attrs, "srcid")?;
                let nodes = attrs.get("nodes").cloned().unwrap_or_default();
                Ok(Message::StdOut {
                    msgid,
                    srcid,
                    nodes,
                    data: None,
                })
            }
            IDENT_SER => {
                let srcid = attr_u64(attrs, "srcid")?;
                let nodes = attrs.get("nodes").cloned().unwrap_or_default();
                Ok(Message::StdErr {
                    msgid,
                    srcid,
                    nodes,
                    data: None,
                })
            }
            IDENT_RET => {
                let srcid = attr_u64(attrs, "srcid")?;
                let retcode: i32 = attrs
                    .get("retcode")
                    .and_then(|v| v.parse().ok())
                    .ok_or_else(|| MessageProcessingError::MissingAttribute("retcode".into()))?;
                let nodes = attrs.get("nodes").cloned().unwrap_or_default();
                Ok(Message::Retcode {
                    msgid,
                    srcid,
                    retcode,
                    nodes,
                })
            }
            IDENT_TIM => {
                let srcid = attr_u64(attrs, "srcid")?;
                let nodes = attrs.get("nodes").cloned().unwrap_or_default();
                Ok(Message::Timeout {
                    msgid,
                    srcid,
                    nodes,
                })
            }
            IDENT_RTR => {
                let srcid = attr_u64(attrs, "srcid")?;
                let event = attrs.get("event").cloned().unwrap_or_default();
                let gateway = attrs.get("gateway").cloned().unwrap_or_default();
                let targets = attrs.get("targets").cloned().unwrap_or_default();
                Ok(Message::Routing {
                    msgid,
                    srcid,
                    event,
                    gateway,
                    targets,
                })
            }
            other => Err(MessageProcessingError::UnknownType(other.into())),
        }
    }
}

impl std::fmt::Display for Message {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Message {} (type: {}, msgid: {})",
            self.ident(),
            self.ident(),
            self.msgid()
        )
    }
}

/// Helper: parse a u64 attribute.
fn attr_u64(attrs: &HashMap<String, String>, key: &str) -> Result<u64, MessageProcessingError> {
    attrs
        .get(key)
        .and_then(|v| v.parse().ok())
        .ok_or_else(|| MessageProcessingError::MissingAttribute(key.into()))
}

// ============================================================================
// XmlReader — incremental SAX-style XML parser
// ============================================================================

/// Parser states for the simple XML state machine.
#[derive(Debug, Clone, Copy, PartialEq)]
enum ParserState {
    /// Outside any tag, looking for '<'.
    Text,
    /// Inside a tag name (after '<').
    TagName,
    /// Inside a closing tag (after '</').
    CloseTagName,
    /// Reading attributes inside an opening tag.
    Attributes,
    /// Inside a self-closing tag (seen '/').
    SelfClose,
}

/// Incremental XML reader that parses fragments and produces [`Message`] instances.
///
/// Modeled after Python's SAX `ContentHandler`. Feed XML data incrementally
/// via [`feed()`](XmlReader::feed) and retrieve parsed messages from the queue.
#[derive(Debug)]
pub struct XmlReader {
    msg_queue: VecDeque<Message>,
    /// Protocol version from `<channel version="...">`.
    pub version: Option<String>,
    /// Current message being assembled.
    draft: Option<Message>,
    // Parser internals
    state: ParserState,
    tag_buf: String,
    attr_name: String,
    attr_val: String,
    attrs: HashMap<String, String>,
    text_buf: String,
    in_attr_value: bool,
    attr_quote: char,
}

impl XmlReader {
    /// Create a new empty XmlReader.
    pub fn new() -> Self {
        Self {
            msg_queue: VecDeque::new(),
            version: None,
            draft: None,
            state: ParserState::Text,
            tag_buf: String::new(),
            attr_name: String::new(),
            attr_val: String::new(),
            attrs: HashMap::new(),
            text_buf: String::new(),
            in_attr_value: false,
            attr_quote: '"',
        }
    }

    /// Whether a complete message is available.
    pub fn msg_available(&self) -> bool {
        !self.msg_queue.is_empty()
    }

    /// Pop and return the oldest message from the queue.
    pub fn pop_msg(&mut self) -> Option<Message> {
        self.msg_queue.pop_front()
    }

    /// Feed XML data to the parser.
    ///
    /// This is the incremental entry point. Each call may produce zero or more
    /// messages that can be retrieved with [`pop_msg()`](XmlReader::pop_msg).
    pub fn feed(&mut self, data: &str) -> Result<(), MessageProcessingError> {
        for ch in data.chars() {
            match self.state {
                ParserState::Text => {
                    if ch == '<' {
                        // Flush any accumulated text as character data
                        if !self.text_buf.is_empty() {
                            let text = std::mem::take(&mut self.text_buf);
                            self.characters(&text)?;
                        }
                        self.tag_buf.clear();
                        self.state = ParserState::TagName;
                    } else {
                        self.text_buf.push(ch);
                    }
                }
                ParserState::TagName => {
                    if ch == '/' && self.tag_buf.is_empty() {
                        // Closing tag: </...>
                        self.tag_buf.clear();
                        self.state = ParserState::CloseTagName;
                    } else if ch == ' ' || ch == '\t' {
                        // End of tag name, start reading attributes
                        self.attrs.clear();
                        self.attr_name.clear();
                        self.state = ParserState::Attributes;
                    } else if ch == '>' {
                        // End of opening tag with no attributes
                        self.attrs.clear();
                        self.start_element(&self.tag_buf.clone(), &self.attrs.clone())?;
                        self.state = ParserState::Text;
                    } else if ch == '/' {
                        self.attrs.clear();
                        self.state = ParserState::SelfClose;
                    } else {
                        self.tag_buf.push(ch);
                    }
                }
                ParserState::CloseTagName => {
                    if ch == '>' {
                        self.end_element(&self.tag_buf.clone())?;
                        self.state = ParserState::Text;
                    } else {
                        self.tag_buf.push(ch);
                    }
                }
                ParserState::Attributes => {
                    if self.in_attr_value {
                        if ch == self.attr_quote {
                            // End of attribute value
                            let name = std::mem::take(&mut self.attr_name);
                            let val = xml_unescape(&std::mem::take(&mut self.attr_val));
                            self.attrs.insert(name, val);
                            self.in_attr_value = false;
                        } else {
                            self.attr_val.push(ch);
                        }
                    } else if ch == '=' {
                        // About to read attribute value
                    } else if ch == '"' || ch == '\'' {
                        self.attr_quote = ch;
                        self.attr_val.clear();
                        self.in_attr_value = true;
                    } else if ch == '>' {
                        // End of opening tag
                        self.start_element(&self.tag_buf.clone(), &self.attrs.clone())?;
                        self.state = ParserState::Text;
                    } else if ch == '/' {
                        self.state = ParserState::SelfClose;
                    } else if ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' {
                        // whitespace between attributes
                    } else {
                        self.attr_name.push(ch);
                    }
                }
                ParserState::SelfClose => {
                    if ch == '>' {
                        // Self-closing tag: treat as open + close
                        let tag = self.tag_buf.clone();
                        let attrs = self.attrs.clone();
                        self.start_element(&tag, &attrs)?;
                        self.end_element(&tag)?;
                        self.state = ParserState::Text;
                    }
                    // else: unexpected char, skip
                }
            }
        }
        Ok(())
    }

    fn start_element(
        &mut self,
        name: &str,
        attrs: &HashMap<String, String>,
    ) -> Result<(), MessageProcessingError> {
        match name {
            "channel" => {
                self.version = attrs.get("version").cloned();
                self.msg_queue.push_back(Message::start());
            }
            "message" => {
                let msg = Message::from_attrs(attrs)?;
                self.draft = Some(msg);
            }
            _ => {
                return Err(MessageProcessingError::InvalidTag(name.into()));
            }
        }
        Ok(())
    }

    fn end_element(&mut self, name: &str) -> Result<(), MessageProcessingError> {
        match name {
            "message" => {
                if let Some(msg) = self.draft.take() {
                    self.msg_queue.push_back(msg);
                }
            }
            "channel" => {
                self.msg_queue.push_back(Message::end());
            }
            _ => {}
        }
        Ok(())
    }

    fn characters(&mut self, content: &str) -> Result<(), MessageProcessingError> {
        if let Some(ref mut draft) = self.draft {
            let trimmed = content.trim();
            if !trimmed.is_empty() {
                draft.data_update(trimmed.as_bytes())?;
            }
        }
        Ok(())
    }
}

impl Default for XmlReader {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Channel
// ============================================================================

/// Trait for channel event handling (start/recv).
///
/// Implementors define the logic for initializing a channel and processing
/// incoming messages.
pub trait ChannelHandler: Send {
    /// Called when the channel is connected and ready.
    fn start(&mut self, channel: &mut Channel);

    /// Called when a complete message has been received.
    fn recv(&mut self, channel: &mut Channel, msg: Message);
}

/// Communication channel between nodes in the propagation tree.
///
/// A Channel manages XML-based message exchange over a transport (typically
/// an SSH pipe). It uses an [`XmlReader`] for incoming messages and XML
/// generation for outgoing messages.
#[derive(Debug)]
pub struct Channel {
    /// Whether the channel has been opened (received StartMessage).
    pub opened: bool,
    /// Whether the channel setup is complete (received ACK to config).
    pub setup: bool,
    /// Whether this end initiated the channel.
    pub initiator: bool,
    /// The XML reader for parsing incoming data.
    pub xml_reader: XmlReader,
    /// Outgoing message buffer.
    pub outbox: VecDeque<Vec<u8>>,
}

impl Channel {
    /// Create a new Channel.
    ///
    /// # Arguments
    /// * `initiator` - true if this end is initiating the channel
    pub fn new(initiator: bool) -> Self {
        Self {
            opened: false,
            setup: false,
            initiator,
            xml_reader: XmlReader::new(),
            outbox: VecDeque::new(),
        }
    }

    /// Generate the XML document header.
    pub fn init_xml(&self) -> Vec<u8> {
        b"<?xml version='1.0' encoding='UTF-8'?>".to_vec()
    }

    /// Generate the opening `<channel>` tag.
    pub fn open_xml(&self, version: &str) -> Vec<u8> {
        format!("<channel version=\"{}\">", xml_escape(version)).into_bytes()
    }

    /// Generate the closing `</channel>` tag.
    pub fn close_xml(&self) -> Vec<u8> {
        b"</channel>".to_vec()
    }

    /// Serialize a message and enqueue it for sending.
    pub fn send(&mut self, msg: &Message) {
        let mut xml = msg.xml();
        xml.push(b'\n');
        self.outbox.push_back(xml);
    }

    /// Feed incoming data to the XML reader and return any parsed messages.
    pub fn feed(&mut self, data: &str) -> Result<Vec<Message>, MessageProcessingError> {
        self.xml_reader.feed(data)?;
        let mut messages = Vec::new();
        while let Some(msg) = self.xml_reader.pop_msg() {
            messages.push(msg);
        }
        Ok(messages)
    }

    /// Take the next outgoing message from the outbox.
    pub fn take_outgoing(&mut self) -> Option<Vec<u8>> {
        self.outbox.pop_front()
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    // -- Base64 tests --------------------------------------------------------

    #[test]
    fn test_base64_roundtrip() {
        let data = b"Hello, ClusterShell!";
        let encoded = base64_encode(data);
        let decoded = base64_decode(&encoded).unwrap();
        assert_eq!(decoded, data);
    }

    #[test]
    fn test_base64_empty() {
        assert_eq!(base64_encode(b""), "");
        assert_eq!(base64_decode("").unwrap(), Vec::<u8>::new());
    }

    #[test]
    fn test_base64_padding() {
        // 1 byte -> 4 chars with ==
        let e1 = base64_encode(b"A");
        assert!(e1.ends_with("=="));
        assert_eq!(base64_decode(&e1).unwrap(), b"A");

        // 2 bytes -> 4 chars with =
        let e2 = base64_encode(b"AB");
        assert!(e2.ends_with('=') && !e2.ends_with("=="));
        assert_eq!(base64_decode(&e2).unwrap(), b"AB");

        // 3 bytes -> 4 chars, no padding
        let e3 = base64_encode(b"ABC");
        assert!(!e3.ends_with('='));
        assert_eq!(base64_decode(&e3).unwrap(), b"ABC");
    }

    #[test]
    fn test_base64_with_newlines() {
        // base64 decoder should ignore whitespace
        let encoded = "SGVs\nbG8=";
        let decoded = base64_decode(encoded).unwrap();
        assert_eq!(decoded, b"Hello");
    }

    // -- XML escape tests ----------------------------------------------------

    #[test]
    fn test_xml_escape() {
        assert_eq!(xml_escape("hello"), "hello");
        assert_eq!(xml_escape("a&b"), "a&amp;b");
        assert_eq!(xml_escape("<tag>"), "&lt;tag&gt;");
        assert_eq!(xml_escape("x=\"y\""), "x=&quot;y&quot;");
    }

    #[test]
    fn test_xml_unescape() {
        assert_eq!(xml_unescape("a&amp;b"), "a&b");
        assert_eq!(xml_unescape("&lt;tag&gt;"), "<tag>");
    }

    // -- Message construction tests ------------------------------------------

    #[test]
    fn test_message_idents() {
        assert_eq!(Message::general().ident(), "GEN");
        assert_eq!(Message::configuration("gw1").ident(), "CFG");
        assert_eq!(Message::control(0).ident(), "CTL");
        assert_eq!(Message::ack(0).ident(), "ACK");
        assert_eq!(Message::error("oops").ident(), "ERR");
        assert_eq!(Message::stdout("node1", 0).ident(), "OUT");
        assert_eq!(Message::stderr("node1", 0).ident(), "SER");
        assert_eq!(Message::retcode("node1", 0, 0).ident(), "RET");
        assert_eq!(Message::timeout("node1", 0).ident(), "TIM");
        assert_eq!(Message::routing("ev", "gw", "tgt", 0).ident(), "RTR");
        assert_eq!(Message::start().ident(), "CHA");
        assert_eq!(Message::end().ident(), "END");
    }

    #[test]
    fn test_message_has_payload() {
        assert!(Message::general().has_payload());
        assert!(Message::configuration("gw1").has_payload());
        assert!(Message::control(0).has_payload());
        assert!(Message::stdout("n1", 0).has_payload());
        assert!(Message::stderr("n1", 0).has_payload());
        assert!(!Message::ack(0).has_payload());
        assert!(!Message::error("x").has_payload());
        assert!(!Message::retcode("n1", 0, 0).has_payload());
        assert!(!Message::timeout("n1", 0).has_payload());
        assert!(!Message::start().has_payload());
        assert!(!Message::end().has_payload());
    }

    #[test]
    fn test_message_srcid() {
        assert_eq!(Message::control(42).srcid(), Some(42));
        assert_eq!(Message::stdout("n", 7).srcid(), Some(7));
        assert_eq!(Message::stderr("n", 8).srcid(), Some(8));
        assert_eq!(Message::retcode("n", 0, 9).srcid(), Some(9));
        assert_eq!(Message::timeout("n", 10).srcid(), Some(10));
        assert_eq!(Message::routing("e", "g", "t", 11).srcid(), Some(11));
        assert_eq!(Message::general().srcid(), None);
        assert_eq!(Message::ack(0).srcid(), None);
        assert_eq!(Message::configuration("gw").srcid(), None);
    }

    #[test]
    fn test_message_nodes() {
        assert_eq!(Message::stdout("node[1-3]", 0).nodes(), Some("node[1-3]"));
        assert_eq!(Message::stderr("node5", 0).nodes(), Some("node5"));
        assert_eq!(Message::retcode("node1", 0, 0).nodes(), Some("node1"));
        assert_eq!(Message::timeout("node2", 0).nodes(), Some("node2"));
        assert_eq!(Message::general().nodes(), None);
        assert_eq!(Message::ack(0).nodes(), None);
    }

    // -- Data encode/decode tests -------------------------------------------

    #[test]
    fn test_data_encode_decode() {
        let mut msg = Message::configuration("gw1");
        let payload = b"some topology data here";
        msg.data_encode(payload);
        assert!(msg.data().is_some());
        let decoded = msg.data_decode().unwrap();
        assert_eq!(decoded, payload);
    }

    #[test]
    fn test_data_update() {
        let mut msg = Message::configuration("gw");
        msg.data_update(b"SGVs").unwrap();
        msg.data_update(b"bG8=").unwrap();
        assert_eq!(msg.data().unwrap(), b"SGVsbG8=");
        let decoded = msg.data_decode().unwrap();
        assert_eq!(decoded, b"Hello");
    }

    #[test]
    fn test_data_update_unexpected() {
        let mut msg = Message::ack(0);
        let result = msg.data_update(b"data");
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            MessageProcessingError::UnexpectedPayload(_)
        ));
    }

    // -- XML serialization tests --------------------------------------------

    #[test]
    fn test_xml_ack() {
        let msg = Message::Ack { msgid: 5, ack: 3 };
        let xml = String::from_utf8(msg.xml()).unwrap();
        assert!(xml.contains("type=\"ACK\""));
        assert!(xml.contains("msgid=\"5\""));
        assert!(xml.contains("ack=\"3\""));
        assert!(xml.contains("/>") || xml.contains("</message>"));
    }

    #[test]
    fn test_xml_configuration_with_payload() {
        let mut msg = Message::Configuration {
            msgid: 0,
            gateway: "gateway1".into(),
            data: None,
        };
        msg.data_encode(b"topology");
        let xml = String::from_utf8(msg.xml()).unwrap();
        assert!(xml.contains("type=\"CFG\""));
        assert!(xml.contains("gateway=\"gateway1\""));
        assert!(xml.contains("</message>"));
        // Should have base64 content between tags
        assert!(xml.contains(&base64_encode(b"topology")));
    }

    #[test]
    fn test_xml_error() {
        let msg = Message::Error {
            msgid: 1,
            reason: "something broke".into(),
        };
        let xml = String::from_utf8(msg.xml()).unwrap();
        assert!(xml.contains("type=\"ERR\""));
        assert!(xml.contains("reason=\"something broke\""));
    }

    #[test]
    fn test_xml_retcode() {
        let msg = Message::Retcode {
            msgid: 2,
            srcid: 10,
            retcode: -1,
            nodes: "node[1-5]".into(),
        };
        let xml = String::from_utf8(msg.xml()).unwrap();
        assert!(xml.contains("type=\"RET\""));
        assert!(xml.contains("retcode=\"-1\""));
        assert!(xml.contains("nodes=\"node[1-5]\""));
    }

    #[test]
    fn test_xml_control() {
        let msg = Message::Control {
            msgid: 3,
            srcid: 1,
            action: "shell".into(),
            target: "node[1-10]".into(),
            data: None,
        };
        let xml = String::from_utf8(msg.xml()).unwrap();
        assert!(xml.contains("type=\"CTL\""));
        assert!(xml.contains("action=\"shell\""));
        assert!(xml.contains("target=\"node[1-10]\""));
    }

    #[test]
    fn test_xml_routing() {
        let msg = Message::Routing {
            msgid: 4,
            srcid: 2,
            event: "connect".into(),
            gateway: "gw2".into(),
            targets: "node[20-30]".into(),
        };
        let xml = String::from_utf8(msg.xml()).unwrap();
        assert!(xml.contains("type=\"RTR\""));
        assert!(xml.contains("event=\"connect\""));
        assert!(xml.contains("gateway=\"gw2\""));
        assert!(xml.contains("targets=\"node[20-30]\""));
    }

    // -- XmlReader tests ----------------------------------------------------

    #[test]
    fn test_xmlreader_channel_start_end() {
        let mut reader = XmlReader::new();
        reader.feed("<channel version=\"1.0\"></channel>").unwrap();
        assert!(reader.msg_available());
        let m1 = reader.pop_msg().unwrap();
        assert_eq!(m1.ident(), "CHA");
        assert_eq!(reader.version.as_deref(), Some("1.0"));

        let m2 = reader.pop_msg().unwrap();
        assert_eq!(m2.ident(), "END");
        assert!(!reader.msg_available());
    }

    #[test]
    fn test_xmlreader_ack_message() {
        let mut reader = XmlReader::new();
        reader
            .feed(
                "<channel version=\"1.0\"><message type=\"ACK\" msgid=\"0\" ack=\"5\"/></channel>",
            )
            .unwrap();

        let start = reader.pop_msg().unwrap();
        assert_eq!(start.ident(), "CHA");

        let ack = reader.pop_msg().unwrap();
        assert_eq!(ack.ident(), "ACK");
        if let Message::Ack { ack, msgid, .. } = ack {
            assert_eq!(ack, 5);
            assert_eq!(msgid, 0);
        } else {
            panic!("expected Ack variant");
        }

        let end = reader.pop_msg().unwrap();
        assert_eq!(end.ident(), "END");
    }

    #[test]
    fn test_xmlreader_message_with_payload() {
        let payload_b64 = base64_encode(b"hello world");
        let xml = format!(
            "<channel version=\"2.0\"><message type=\"CFG\" msgid=\"1\" gateway=\"gw1\">{}</message></channel>",
            payload_b64
        );

        let mut reader = XmlReader::new();
        reader.feed(&xml).unwrap();

        let _start = reader.pop_msg().unwrap();
        let cfg = reader.pop_msg().unwrap();
        assert_eq!(cfg.ident(), "CFG");
        let decoded = cfg.data_decode().unwrap();
        assert_eq!(decoded, b"hello world");

        if let Message::Configuration { gateway, .. } = &cfg {
            assert_eq!(gateway, "gw1");
        } else {
            panic!("expected Configuration variant");
        }
    }

    #[test]
    fn test_xmlreader_incremental_feed() {
        let mut reader = XmlReader::new();

        // Feed in chunks
        reader.feed("<chan").unwrap();
        assert!(!reader.msg_available());
        reader.feed("nel version=\"1.0\">").unwrap();
        assert!(reader.msg_available());
        let start = reader.pop_msg().unwrap();
        assert_eq!(start.ident(), "CHA");

        reader
            .feed("<message type=\"ACK\" msgid=\"0\" ack=\"1\"/>")
            .unwrap();
        let ack = reader.pop_msg().unwrap();
        assert_eq!(ack.ident(), "ACK");

        reader.feed("</channel>").unwrap();
        let end = reader.pop_msg().unwrap();
        assert_eq!(end.ident(), "END");
    }

    #[test]
    fn test_xmlreader_unknown_type() {
        let mut reader = XmlReader::new();
        let result =
            reader.feed("<channel version=\"1\"><message type=\"BOGUS\" msgid=\"0\"/></channel>");
        assert!(result.is_err());
    }

    #[test]
    fn test_xmlreader_error_message() {
        let mut reader = XmlReader::new();
        reader
            .feed("<channel version=\"1\"><message type=\"ERR\" msgid=\"0\" reason=\"parse failed\"/></channel>")
            .unwrap();

        let _start = reader.pop_msg().unwrap();
        let err = reader.pop_msg().unwrap();
        assert_eq!(err.ident(), "ERR");
        if let Message::Error { reason, .. } = &err {
            assert_eq!(reason, "parse failed");
        } else {
            panic!("expected Error variant");
        }
    }

    #[test]
    fn test_xmlreader_retcode() {
        let mut reader = XmlReader::new();
        reader
            .feed(
                "<message type=\"RET\" msgid=\"7\" srcid=\"3\" retcode=\"0\" nodes=\"node[1-5]\"/>",
            )
            .unwrap();

        let msg = reader.pop_msg().unwrap();
        if let Message::Retcode {
            srcid,
            retcode,
            nodes,
            ..
        } = &msg
        {
            assert_eq!(*srcid, 3);
            assert_eq!(*retcode, 0);
            assert_eq!(nodes, "node[1-5]");
        } else {
            panic!("expected Retcode variant");
        }
    }

    #[test]
    fn test_xmlreader_routing() {
        let mut reader = XmlReader::new();
        reader
            .feed("<message type=\"RTR\" msgid=\"0\" srcid=\"1\" event=\"connect\" gateway=\"gw1\" targets=\"node[1-10]\"/>")
            .unwrap();

        let msg = reader.pop_msg().unwrap();
        if let Message::Routing {
            event,
            gateway,
            targets,
            ..
        } = &msg
        {
            assert_eq!(event, "connect");
            assert_eq!(gateway, "gw1");
            assert_eq!(targets, "node[1-10]");
        } else {
            panic!("expected Routing variant");
        }
    }

    #[test]
    fn test_xmlreader_stdout_stderr() {
        let payload = base64_encode(b"output line");
        let xml = format!(
            "<message type=\"OUT\" msgid=\"0\" srcid=\"5\" nodes=\"node1\">{}</message>",
            payload
        );
        let mut reader = XmlReader::new();
        reader.feed(&xml).unwrap();

        let msg = reader.pop_msg().unwrap();
        assert_eq!(msg.ident(), "OUT");
        assert_eq!(msg.srcid(), Some(5));
        assert_eq!(msg.nodes(), Some("node1"));
        assert_eq!(msg.data_decode().unwrap(), b"output line");

        // StdErr
        let xml2 = format!(
            "<message type=\"SER\" msgid=\"1\" srcid=\"6\" nodes=\"node2\">{}</message>",
            base64_encode(b"error line")
        );
        reader.feed(&xml2).unwrap();
        let msg2 = reader.pop_msg().unwrap();
        assert_eq!(msg2.ident(), "SER");
        assert_eq!(msg2.data_decode().unwrap(), b"error line");
    }

    #[test]
    fn test_xmlreader_timeout() {
        let mut reader = XmlReader::new();
        reader
            .feed("<message type=\"TIM\" msgid=\"0\" srcid=\"1\" nodes=\"node[5-8]\"/>")
            .unwrap();
        let msg = reader.pop_msg().unwrap();
        assert_eq!(msg.ident(), "TIM");
        assert_eq!(msg.nodes(), Some("node[5-8]"));
    }

    #[test]
    fn test_xmlreader_control() {
        let payload = base64_encode(b"command data");
        let xml = format!(
            "<message type=\"CTL\" msgid=\"0\" srcid=\"1\" action=\"shell\" target=\"node[1-3]\">{}</message>",
            payload
        );
        let mut reader = XmlReader::new();
        reader.feed(&xml).unwrap();

        let msg = reader.pop_msg().unwrap();
        if let Message::Control { action, target, .. } = &msg {
            assert_eq!(action, "shell");
            assert_eq!(target, "node[1-3]");
        } else {
            panic!("expected Control variant");
        }
        assert_eq!(msg.data_decode().unwrap(), b"command data");
    }

    // -- XML roundtrip tests ------------------------------------------------

    #[test]
    fn test_xml_roundtrip_ack() {
        let original = Message::Ack { msgid: 10, ack: 7 };
        let xml = String::from_utf8(original.xml()).unwrap();
        let mut reader = XmlReader::new();
        reader.feed(&xml).unwrap();
        let parsed = reader.pop_msg().unwrap();
        assert_eq!(parsed.ident(), "ACK");
        if let Message::Ack { msgid, ack, .. } = parsed {
            assert_eq!(msgid, 10);
            assert_eq!(ack, 7);
        }
    }

    #[test]
    fn test_xml_roundtrip_cfg_with_data() {
        let mut original = Message::Configuration {
            msgid: 0,
            gateway: "my-gateway".into(),
            data: None,
        };
        original.data_encode(b"topology bytes here");
        let xml = String::from_utf8(original.xml()).unwrap();

        let mut reader = XmlReader::new();
        reader.feed(&xml).unwrap();
        let parsed = reader.pop_msg().unwrap();
        assert_eq!(parsed.ident(), "CFG");
        if let Message::Configuration { gateway, .. } = &parsed {
            assert_eq!(gateway, "my-gateway");
        }
        assert_eq!(parsed.data_decode().unwrap(), b"topology bytes here");
    }

    #[test]
    fn test_xml_roundtrip_error() {
        let original = Message::Error {
            msgid: 3,
            reason: "test <error> & reason".into(),
        };
        let xml = String::from_utf8(original.xml()).unwrap();
        let mut reader = XmlReader::new();
        reader.feed(&xml).unwrap();
        let parsed = reader.pop_msg().unwrap();
        if let Message::Error { reason, .. } = &parsed {
            assert_eq!(reason, "test <error> & reason");
        } else {
            panic!("expected Error variant");
        }
    }

    #[test]
    fn test_xml_roundtrip_retcode() {
        let original = Message::Retcode {
            msgid: 5,
            srcid: 2,
            retcode: 127,
            nodes: "node[1-100]".into(),
        };
        let xml = String::from_utf8(original.xml()).unwrap();
        let mut reader = XmlReader::new();
        reader.feed(&xml).unwrap();
        let parsed = reader.pop_msg().unwrap();
        if let Message::Retcode { retcode, nodes, .. } = &parsed {
            assert_eq!(*retcode, 127);
            assert_eq!(nodes, "node[1-100]");
        } else {
            panic!("expected Retcode variant");
        }
    }

    // -- Channel tests ------------------------------------------------------

    #[test]
    fn test_channel_creation() {
        let ch = Channel::new(true);
        assert!(!ch.opened);
        assert!(!ch.setup);
        assert!(ch.initiator);
    }

    #[test]
    fn test_channel_init_xml() {
        let ch = Channel::new(false);
        let xml = ch.init_xml();
        assert_eq!(
            String::from_utf8(xml).unwrap(),
            "<?xml version='1.0' encoding='UTF-8'?>"
        );
    }

    #[test]
    fn test_channel_open_close_xml() {
        let ch = Channel::new(true);
        let open = String::from_utf8(ch.open_xml("3.0")).unwrap();
        assert_eq!(open, "<channel version=\"3.0\">");
        let close = String::from_utf8(ch.close_xml()).unwrap();
        assert_eq!(close, "</channel>");
    }

    #[test]
    fn test_channel_send() {
        let mut ch = Channel::new(true);
        let msg = Message::ack(42);
        ch.send(&msg);
        let outgoing = ch.take_outgoing().unwrap();
        let xml = String::from_utf8(outgoing).unwrap();
        assert!(xml.contains("type=\"ACK\""));
        assert!(xml.ends_with('\n'));
    }

    #[test]
    fn test_channel_feed_and_receive() {
        let mut ch = Channel::new(false);
        let messages = ch
            .feed(
                "<channel version=\"1.0\"><message type=\"ACK\" msgid=\"0\" ack=\"1\"/></channel>",
            )
            .unwrap();
        assert_eq!(messages.len(), 3); // Start, ACK, End
        assert_eq!(messages[0].ident(), "CHA");
        assert_eq!(messages[1].ident(), "ACK");
        assert_eq!(messages[2].ident(), "END");
    }

    #[test]
    fn test_channel_outbox_ordering() {
        let mut ch = Channel::new(true);
        ch.send(&Message::ack(1));
        ch.send(&Message::ack(2));
        ch.send(&Message::ack(3));

        let m1 = ch.take_outgoing().unwrap();
        let m2 = ch.take_outgoing().unwrap();
        let m3 = ch.take_outgoing().unwrap();
        assert!(ch.take_outgoing().is_none());

        // They should come out in order
        assert!(String::from_utf8(m1).unwrap().contains("ACK"));
        assert!(String::from_utf8(m2).unwrap().contains("ACK"));
        assert!(String::from_utf8(m3).unwrap().contains("ACK"));
    }

    // -- Message Display test -----------------------------------------------

    #[test]
    fn test_message_display() {
        let msg = Message::ack(0);
        let display = format!("{}", msg);
        assert!(display.contains("ACK"));
    }

    // -- Edge case tests ----------------------------------------------------

    #[test]
    fn test_from_attrs_missing_type() {
        let attrs = HashMap::new();
        assert!(Message::from_attrs(&attrs).is_err());
    }

    #[test]
    fn test_from_attrs_missing_srcid() {
        let mut attrs = HashMap::new();
        attrs.insert("type".into(), "CTL".into());
        attrs.insert("msgid".into(), "0".into());
        // missing srcid
        assert!(Message::from_attrs(&attrs).is_err());
    }

    #[test]
    fn test_data_decode_no_data() {
        let msg = Message::configuration("gw");
        let result = msg.data_decode();
        assert!(result.is_err());
    }

    #[test]
    fn test_xml_special_characters_roundtrip() {
        let msg = Message::Error {
            msgid: 0,
            reason: "node<1>&\"2\"".into(),
        };
        let xml = String::from_utf8(msg.xml()).unwrap();
        // Should be escaped in XML
        assert!(xml.contains("&lt;"));
        assert!(xml.contains("&amp;"));

        // Parse it back
        let mut reader = XmlReader::new();
        reader.feed(&xml).unwrap();
        let parsed = reader.pop_msg().unwrap();
        if let Message::Error { reason, .. } = &parsed {
            assert_eq!(reason, "node<1>&\"2\"");
        }
    }
}