confium_coordinator/coordinator/
transport.rs1use std::io::{Read, Write};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum TransportMode {
11 Client,
12 Server,
13}
14
15pub trait Transport: Read + Write + Send {
17 fn name(&self) -> &str;
19 fn is_encrypted(&self) -> bool;
21 fn peer_identity(&self) -> Option<String>;
23}
24
25pub struct PlaintextTransport {
27 inner: std::net::TcpStream,
28 peer: Option<String>,
29}
30
31impl PlaintextTransport {
32 pub fn new(stream: std::net::TcpStream) -> Self {
33 let peer = stream.peer_addr().ok().map(|a| a.to_string());
34 Self {
35 inner: stream,
36 peer,
37 }
38 }
39}
40
41impl Read for PlaintextTransport {
42 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
43 self.inner.read(buf)
44 }
45}
46
47impl Write for PlaintextTransport {
48 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
49 self.inner.write(buf)
50 }
51 fn flush(&mut self) -> std::io::Result<()> {
52 self.inner.flush()
53 }
54}
55
56impl Transport for PlaintextTransport {
57 fn name(&self) -> &str {
58 "plaintext"
59 }
60 fn is_encrypted(&self) -> bool {
61 false
62 }
63 fn peer_identity(&self) -> Option<String> {
64 self.peer.clone()
65 }
66}
67
68pub trait TransportFactory: Send + Sync {
70 fn wrap(&self, stream: std::net::TcpStream, mode: TransportMode) -> Box<dyn Transport>;
72 fn name(&self) -> &str;
74}
75
76pub struct PlaintextTransportFactory;
78
79impl TransportFactory for PlaintextTransportFactory {
80 fn wrap(&self, stream: std::net::TcpStream, _mode: TransportMode) -> Box<dyn Transport> {
81 Box::new(PlaintextTransport::new(stream))
82 }
83 fn name(&self) -> &str {
84 "plaintext"
85 }
86}
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn plaintext_not_encrypted() {
94 let factory = PlaintextTransportFactory;
95 assert_eq!(factory.name(), "plaintext");
97 }
98
99 #[test]
100 fn transport_mode_eq() {
101 assert_eq!(TransportMode::Client, TransportMode::Client);
102 assert_ne!(TransportMode::Client, TransportMode::Server);
103 }
104
105 #[test]
106 fn factory_implements_trait() {
107 let factory: Box<dyn TransportFactory> = Box::new(PlaintextTransportFactory);
108 assert_eq!(factory.name(), "plaintext");
109 }
110}