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
|
use std::io::{BufRead, Read, Write};
use byteorder::WriteBytesExt;
#[derive(Debug, Clone)]
pub struct Message {
pub event: lexpr::Value,
pub data: lexpr::Value,
}
pub struct Client {
reader: std::io::BufReader<std::net::TcpStream>,
buf: String,
}
impl Client {
pub fn new(addr: &str, subs: &[lexpr::Value]) -> Self {
let mut socket = std::net::TcpStream::connect(addr).expect("failed to connect to message bus");
socket.set_nonblocking(true).expect("failed to set message bus socket nonblocking");
for s in subs {
write!(socket, "(sub {})\n", s).expect("failed to send subscribe message to bus");
}
let reader = std::io::BufReader::new(socket);
Self { reader, buf: String::new(), }
}
pub fn pump(&mut self) -> Option<Message> {
match self.reader.read_line(&mut self.buf) {
Ok(l) => {
// log::info!("read line: {}", self.buf);
let mv = lexpr::from_str(&self.buf);
self.buf.clear();
match mv {
Ok(v) => {
match v.as_cons() {
Some(cs) => {
Some(Message { event: cs.car().clone(), data: cs.cdr().clone() })
},
_ => { log::error!("malformed message bus input s-expression: {}", v); None },
}
},
Err(e) => { log::error!("malformed message bus input line: {}", e); None },
}
},
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
// if self.buf.len() > 0 {
// log::error!("error wouldblock: buf is {}", self.buf);
// }
None
},
Err(e) => panic!("IO error on message bus: {}", e),
}
}
}
#[derive(Debug, Clone)]
pub struct BinaryMessage {
pub event: Vec<u8>,
pub data: Vec<u8>
}
#[derive(Debug, Clone)]
pub enum BinaryClientState {
PartialEventLength { buf_len: usize, buf: [u8; 4] },
PartialEvent { len: usize, buf_len: usize, buf: Vec<u8> },
PartialDataLength { event: Vec<u8>, buf_len: usize, buf: [u8; 4] },
PartialData { event: Vec<u8>, len: usize, buf_len: usize, buf: Vec<u8> },
Message { event: Vec<u8>, data: Vec<u8> },
}
impl Default for BinaryClientState {
fn default() -> Self {
Self::PartialEventLength { buf_len: 0, buf: [0; 4] }
}
}
pub struct BinaryClient {
state: BinaryClientState,
reader: std::io::BufReader<std::net::TcpStream>,
}
impl BinaryClient {
pub fn new(addr: &str, subs: &[&[u8]]) -> Self {
let mut socket = std::net::TcpStream::connect(addr).expect("failed to connect to message bus");
socket.set_nonblocking(true).expect("failed to set message bus socket nonblocking");
for s in subs {
write!(socket, "s").expect("failed to send subscribe message to bus");
socket.write_u32::<byteorder::LE>(s.len() as u32).expect("failed to send subscribe message length to bus");
socket.write_all(s).expect("failed to send subscribe message to bus");
}
socket.flush().expect("failed to flush bus connection");
let reader = std::io::BufReader::new(socket);
Self { state: BinaryClientState::PartialEventLength { buf_len: 0, buf: [0; 4] }, reader }
}
fn read(reader: &mut std::io::BufReader<std::net::TcpStream>, buf: &mut [u8]) -> Option<usize> {
match reader.read(buf) {
Ok(sz) => Some(sz),
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
None
},
Err(e) => panic!("IO error on message bus: {}", e),
}
}
fn update_state(
reader: &mut std::io::BufReader<std::net::TcpStream>,
mut state: BinaryClientState,
) -> BinaryClientState {
loop {
state = match state {
BinaryClientState::PartialEventLength { mut buf_len, mut buf } => {
buf_len += if let Some(x) = Self::read(reader, &mut buf[buf_len..]) {
x
} else { break BinaryClientState::PartialEventLength { buf_len, buf }; };
if buf_len == 4 {
let len = u32::from_le_bytes(buf) as usize;
BinaryClientState::PartialEvent {
len,
buf_len: 0,
buf: vec![0; len],
}
} else { BinaryClientState::PartialEventLength { buf_len, buf } }
},
BinaryClientState::PartialEvent { len, mut buf_len, mut buf } => {
buf_len += if let Some(x) = Self::read(reader, &mut buf[buf_len..]) {
x
} else { break BinaryClientState::PartialEvent { len, buf_len, buf }; };
if buf_len == len {
BinaryClientState::PartialDataLength {
event: buf.clone(),
buf_len: 0,
buf: [0; 4],
}
} else { BinaryClientState::PartialEvent { len, buf_len, buf } }
},
BinaryClientState::PartialDataLength { event, mut buf_len, mut buf } => {
buf_len += if let Some(x) = Self::read(reader, &mut buf[buf_len..]) {
x
} else { break BinaryClientState::PartialDataLength { event, buf_len, buf }; };
if buf_len == 4 {
let len = u32::from_le_bytes(buf) as usize;
BinaryClientState::PartialData {
event,
len,
buf_len: 0,
buf: vec![0; len],
}
} else { BinaryClientState::PartialDataLength { event, buf_len, buf } }
},
BinaryClientState::PartialData { event, len, mut buf_len, mut buf } => {
buf_len += if let Some(x) = Self::read(reader, &mut buf[buf_len..]) {
x
} else { break BinaryClientState::PartialData { event, len, buf_len, buf }; };
if buf_len == len {
BinaryClientState::Message {
event,
data: buf.clone(),
}
} else { BinaryClientState::PartialData { event, len, buf_len, buf } }
},
st@BinaryClientState::Message{..} => break st,
};
}
}
pub fn pump(&mut self) -> Option<BinaryMessage> {
self.state = Self::update_state(&mut self.reader, std::mem::take(&mut self.state));
match std::mem::take(&mut self.state) {
BinaryClientState::Message { event, data } => {
self.state = BinaryClientState::PartialEventLength { buf_len: 0, buf: [0; 4] };
Some(BinaryMessage {
event: event,
data: data,
})
},
st => {
self.state = st;
None
}
}
}
}
|