use crate::header::{parse_block, Error, Headers, StatusLine};
use crate::negotiate::{BIG, LITTLE};
use labstream_wire::{ByteOrder, Codec, Format, Sample, TEST_PATTERN_OFFSETS};
#[derive(Debug, Clone, PartialEq)]
pub struct FeedParams {
pub version: i32,
pub uid: String,
pub native_byte_order: i32,
pub endian_performance: f64,
pub has_ieee754_floats: bool,
pub supports_subnormals: bool,
pub value_size: i32,
pub max_buffer_length: i32,
pub max_chunk_length: i32,
pub hostname: String,
pub source_id: String,
pub session_id: String,
}
impl FeedParams {
pub fn new(uid: &str, format: Format) -> Self {
FeedParams {
version: crate::negotiate::LSL_PROTOCOL_VERSION,
uid: uid.to_string(),
native_byte_order: if cfg!(target_endian = "little") {
LITTLE
} else {
BIG
},
endian_performance: 0.0,
has_ieee754_floats: true,
supports_subnormals: true,
value_size: format.width().unwrap_or(0) as i32,
max_buffer_length: 360,
max_chunk_length: 0,
hostname: String::new(),
source_id: String::new(),
session_id: "default".to_string(),
}
}
pub fn to_wire(&self) -> Vec<u8> {
let mut s = format!("LSL:streamfeed/{} {}\r\n", self.version, self.uid);
s.push_str(&format!(
"Native-Byte-Order: {}\r\n",
self.native_byte_order
));
s.push_str(&format!(
"Endian-Performance: {}\r\n",
self.endian_performance.floor()
));
s.push_str(&format!(
"Has-IEEE754-Floats: {}\r\n",
self.has_ieee754_floats as i32
));
s.push_str(&format!(
"Supports-Subnormals: {}\r\n",
self.supports_subnormals as i32
));
s.push_str(&format!("Value-Size: {}\r\n", self.value_size));
s.push_str(&format!("Data-Protocol-Version: {}\r\n", self.version));
s.push_str(&format!(
"Max-Buffer-Length: {}\r\n",
self.max_buffer_length
));
s.push_str(&format!("Max-Chunk-Length: {}\r\n", self.max_chunk_length));
s.push_str(&format!("Hostname: {}\r\n", self.hostname));
s.push_str(&format!("Source-Id: {}\r\n", self.source_id));
s.push_str(&format!("Session-Id: {}\r\n", self.session_id));
s.push_str("\r\n");
s.into_bytes()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct FeedAgreement {
pub data_protocol_version: i32,
pub order: ByteOrder,
pub suppress_subnormals: bool,
pub uid: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum State {
AwaitingHeaders,
AwaitingTestPattern,
Streaming,
Failed,
}
pub struct FeedClient {
params: FeedParams,
format: Format,
channels: usize,
state: State,
agreement: Option<FeedAgreement>,
codec: Option<Codec>,
}
impl FeedClient {
pub fn start(params: FeedParams, format: Format, channels: usize) -> (Self, Vec<u8>) {
let bytes = params.to_wire();
(
FeedClient {
params,
format,
channels,
state: State::AwaitingHeaders,
agreement: None,
codec: None,
},
bytes,
)
}
pub fn state(&self) -> State {
self.state
}
pub fn agreement(&self) -> Option<&FeedAgreement> {
self.agreement.as_ref()
}
pub fn codec(&self) -> Option<&Codec> {
self.codec.as_ref()
}
pub fn on_headers(&mut self, buf: &[u8]) -> Result<usize, Error> {
let nl = match buf.iter().position(|&b| b == b'\n') {
Some(n) => n,
None => return Err(Error::Incomplete),
};
let status = StatusLine::parse(&String::from_utf8_lossy(&buf[..nl]))?;
if status.version / 100 > self.params.version / 100 {
self.state = State::Failed;
return Err(Error::VersionTooNew {
theirs: status.version,
ours: self.params.version,
});
}
if status.action() != crate::header::StatusAction::Proceed {
self.state = State::Failed;
return Err(Error::Status(status));
}
let (headers, used) = match parse_block(&buf[nl + 1..]) {
Some(x) => x,
None => return Err(Error::Incomplete),
};
let agreement = self.read_answer(&headers)?;
let order = agreement.order;
self.codec = Some(
Codec::new(self.format, self.channels, order)
.with_suppress_subnormals(agreement.suppress_subnormals),
);
self.agreement = Some(agreement);
self.state = State::AwaitingTestPattern;
Ok(nl + 1 + used)
}
fn read_answer(&mut self, h: &Headers) -> Result<FeedAgreement, Error> {
let uid = h.get("uid").unwrap_or("").to_string();
if !uid.is_empty() && uid != self.params.uid {
self.state = State::Failed;
return Err(Error::UidMismatch {
expected: self.params.uid.clone(),
actual: uid,
});
}
let raw = h.get_int("byte-order").unwrap_or(LITTLE as i64) as i32;
let order_value = if raw == 0 {
self.params.native_byte_order
} else {
raw
};
let order = match ByteOrder::from_wire(order_value) {
Some(o) => o,
None => {
self.state = State::Failed;
return Err(Error::ByteOrderNotSupported(order_value));
}
};
let version = h
.get_int("data-protocol-version")
.unwrap_or(self.params.version as i64) as i32;
if version > self.params.version {
self.state = State::Failed;
return Err(Error::VersionTooNew {
theirs: version,
ours: self.params.version,
});
}
if version < 110 {
self.state = State::Failed;
return Err(Error::VersionTooOld { theirs: version });
}
Ok(FeedAgreement {
data_protocol_version: version,
order,
suppress_subnormals: h.get_bool("suppress-subnormals").unwrap_or(false),
uid,
})
}
pub fn on_test_pattern(&mut self, buf: &[u8]) -> Result<usize, TestPatternError> {
let codec = self.codec.as_ref().ok_or(TestPatternError::NotReady)?;
let mut off = 0usize;
for (i, pattern) in TEST_PATTERN_OFFSETS.iter().enumerate() {
let (got, n) = match codec.decode(&buf[off..]) {
Ok(x) => x,
Err(labstream_wire::Error::Truncated) => return Err(TestPatternError::Incomplete),
Err(e) => {
self.state = State::Failed;
return Err(TestPatternError::Decode(e));
}
};
let want = labstream_wire::test_pattern(self.format, self.channels, *pattern);
if !same_sample(&got, &want) {
self.state = State::Failed;
return Err(TestPatternError::Mismatch {
index: i,
expected: Box::new(want),
actual: Box::new(got),
});
}
off += n;
}
self.state = State::Streaming;
Ok(off)
}
pub fn on_samples(&self, buf: &[u8]) -> Result<(Vec<Sample>, usize), labstream_wire::Error> {
let codec = self.codec.as_ref().expect("the handshake set the codec");
codec.decode_all(buf)
}
}
fn same_sample(a: &Sample, b: &Sample) -> bool {
use labstream_wire::Value;
a.timestamp.to_bits() == b.timestamp.to_bits()
&& a.values.len() == b.values.len()
&& a.values.iter().zip(&b.values).all(|(x, y)| match (x, y) {
(Value::F32(p), Value::F32(q)) => p.to_bits() == q.to_bits(),
(Value::F64(p), Value::F64(q)) => p.to_bits() == q.to_bits(),
_ => x == y,
})
}
#[derive(Debug, Clone, PartialEq)]
pub enum TestPatternError {
Incomplete,
NotReady,
Decode(labstream_wire::Error),
Mismatch {
index: usize,
expected: Box<Sample>,
actual: Box<Sample>,
},
}
#[cfg(test)]
mod tests {
use super::*;
use crate::header::parse_block;
use crate::negotiate::{negotiate, FeedRequest, Outcome, ServerStream};
fn handshake(
format: Format,
channels: usize,
client_order: i32,
client_perf: f64,
) -> FeedClient {
let mut p = FeedParams::new("uid-1", format);
p.native_byte_order = client_order;
p.endian_performance = client_perf;
let (mut client, request) = FeedClient::start(p, format, channels);
let text = String::from_utf8_lossy(&request);
let head = text.split("\r\n").next().unwrap().to_string();
let uid = head.split(' ').nth(1).unwrap_or("").to_string();
let block_start = request.iter().position(|&b| b == b'\n').unwrap() + 1;
let (headers, _) = parse_block(&request[block_start..]).expect("a block");
let srv = ServerStream::new("uid-1", format, LITTLE, 1000.0);
let req = FeedRequest::from_headers(110, &uid, &headers, srv.channel_bytes);
let accepted = match negotiate(&req, &srv) {
Outcome::Accept(a) => a,
Outcome::Refuse(s) => panic!("refused: {} {}", s.code, s.message),
};
let answer = accepted.to_wire(110, "uid-1");
client.on_headers(answer.as_bytes()).expect("headers");
let codec = *client.codec().expect("a codec");
let mut bytes = Vec::new();
for off in TEST_PATTERN_OFFSETS {
let s = labstream_wire::test_pattern(format, channels, off);
codec.encode(&s, &mut bytes).expect("encode");
}
let used = client.on_test_pattern(&bytes).expect("the test pattern");
assert_eq!(used, bytes.len());
client
}
#[test]
fn the_request_carries_every_header_that_liblsl_writes() {
let p = FeedParams::new("abc", Format::Float32);
let text = String::from_utf8(p.to_wire()).unwrap();
for name in [
"Native-Byte-Order",
"Endian-Performance",
"Has-IEEE754-Floats",
"Supports-Subnormals",
"Value-Size",
"Data-Protocol-Version",
"Max-Buffer-Length",
"Max-Chunk-Length",
"Hostname",
"Source-Id",
"Session-Id",
] {
assert!(text.contains(name), "the request is missing {name}");
}
assert!(text.starts_with("LSL:streamfeed/110 abc\r\n"));
assert!(text.ends_with("\r\n\r\n"));
}
#[test]
fn a_full_handshake_reaches_the_streaming_state() {
let c = handshake(Format::Float32, 8, LITTLE, 0.0);
assert_eq!(c.state(), State::Streaming);
let a = c.agreement().expect("an agreement");
assert_eq!(a.data_protocol_version, 110);
assert_eq!(a.order, ByteOrder::Little);
}
#[test]
fn a_slow_client_makes_the_server_swap() {
let c = handshake(Format::Double64, 4, BIG, 0.0);
assert_eq!(c.agreement().unwrap().order, ByteOrder::Big);
}
#[test]
fn a_wrong_uid_in_the_answer_stops_the_feed() {
let p = FeedParams::new("uid-1", Format::Float32);
let (mut c, _) = FeedClient::start(p, Format::Float32, 1);
let answer = "LSL/110 200 OK\r\nUID: another\r\nByte-Order: 1234\r\n\
Suppress-Subnormals: 0\r\nData-Protocol-Version: 110\r\n\r\n";
match c.on_headers(answer.as_bytes()) {
Err(Error::UidMismatch { actual, .. }) => assert_eq!(actual, "another"),
other => panic!("expected a UID mismatch, got {other:?}"),
}
assert_eq!(c.state(), State::Failed);
}
#[test]
fn a_404_answer_stops_the_feed() {
let p = FeedParams::new("uid-1", Format::Float32);
let (mut c, _) = FeedClient::start(p, Format::Float32, 1);
match c.on_headers(b"LSL/110 404 Not found\r\n\r\n") {
Err(Error::Status(s)) => assert_eq!(s.code, 404),
other => panic!("expected a status error, got {other:?}"),
}
assert_eq!(c.state(), State::Failed);
}
#[test]
fn a_byte_order_of_zero_means_the_native_order() {
let p = FeedParams::new("uid-1", Format::Float32);
let (mut c, _) = FeedClient::start(p, Format::Float32, 1);
let answer = "LSL/110 200 OK\r\nUID: uid-1\r\nByte-Order: 0\r\n\
Suppress-Subnormals: 0\r\nData-Protocol-Version: 110\r\n\r\n";
c.on_headers(answer.as_bytes()).expect("headers");
assert_eq!(c.agreement().unwrap().order, ByteOrder::native());
}
#[test]
fn a_wrong_test_pattern_stops_the_feed() {
let p = FeedParams::new("uid-1", Format::Float32);
let (mut c, _) = FeedClient::start(p, Format::Float32, 2);
let answer = "LSL/110 200 OK\r\nUID: uid-1\r\nByte-Order: 1234\r\n\
Suppress-Subnormals: 0\r\nData-Protocol-Version: 110\r\n\r\n";
c.on_headers(answer.as_bytes()).expect("headers");
let codec = *c.codec().unwrap();
let mut bytes = Vec::new();
for off in [2, 4] {
codec
.encode(
&labstream_wire::test_pattern(Format::Float32, 2, off),
&mut bytes,
)
.unwrap();
}
match c.on_test_pattern(&bytes) {
Err(TestPatternError::Mismatch { index, .. }) => assert_eq!(index, 0),
other => panic!("expected a mismatch, got {other:?}"),
}
assert_eq!(c.state(), State::Failed);
}
#[test]
fn an_incomplete_buffer_asks_for_more() {
let p = FeedParams::new("uid-1", Format::Float32);
let (mut c, _) = FeedClient::start(p, Format::Float32, 1);
assert_eq!(
c.on_headers(b"LSL/110 200 OK\r\nUID: uid-1\r\n"),
Err(Error::Incomplete)
);
assert_eq!(c.state(), State::AwaitingHeaders);
}
}