pub const BASE_PORT: u16 = 16572;
pub const PORT_RANGE: u16 = 32;
pub const LINK_ADDRESSES: [&str; 3] = ["255.255.255.255", "224.0.0.1", "224.0.0.183"];
pub const SITE_ADDRESSES: [&str; 1] = ["239.255.172.215"];
pub fn build_query(query: &str, return_port: u16, query_id: &str) -> Vec<u8> {
format!("LSL:shortinfo\r\n{query}\r\n{return_port} {query_id}\r\n").into_bytes()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Query {
pub query: String,
pub return_port: u16,
pub query_id: String,
}
pub fn parse_query(buf: &[u8]) -> Option<Query> {
let text = String::from_utf8_lossy(buf);
let mut lines = text.split('\n');
let method = lines.next()?.trim_end_matches('\r').trim();
if method != "LSL:shortinfo" {
return None;
}
let query = lines.next()?.trim_end_matches('\r').trim().to_string();
let tail = lines.next()?;
let mut parts = tail.split_whitespace();
let return_port = parts.next()?.parse().ok()?;
let query_id = parts.next().unwrap_or("").to_string();
Some(Query {
query,
return_port,
query_id,
})
}
pub fn build_answer(query_id: &str, shortinfo_xml: &str) -> Vec<u8> {
let mut v = Vec::with_capacity(query_id.len() + 2 + shortinfo_xml.len());
v.extend_from_slice(query_id.as_bytes());
v.extend_from_slice(b"\r\n");
v.extend_from_slice(shortinfo_xml.as_bytes());
v
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Answer {
pub query_id: String,
pub xml: String,
}
pub fn parse_answer(buf: &[u8]) -> Option<Answer> {
let nl = buf.iter().position(|&b| b == b'\n')?;
let id = String::from_utf8_lossy(&buf[..nl]).trim().to_string();
let xml = String::from_utf8_lossy(&buf[nl + 1..]).to_string();
Some(Answer { query_id: id, xml })
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ShortInfo {
pub name: String,
pub stream_type: String,
pub channel_count: u32,
pub channel_format: String,
pub uid: String,
pub source_id: String,
pub nominal_srate: f64,
pub hostname: String,
pub v4data_port: u16,
pub v4service_port: u16,
}
fn tag<'a>(xml: &'a str, name: &str) -> Option<&'a str> {
let open = format!("<{name}>");
let close = format!("</{name}>");
let a = xml.find(&open)? + open.len();
let b = xml[a..].find(&close)? + a;
Some(&xml[a..b])
}
impl ShortInfo {
pub fn parse(xml: &str) -> Option<Self> {
Some(ShortInfo {
name: tag(xml, "name")?.to_string(),
stream_type: tag(xml, "type").unwrap_or("").to_string(),
channel_count: tag(xml, "channel_count")?.trim().parse().ok()?,
channel_format: tag(xml, "channel_format")?.trim().to_string(),
uid: tag(xml, "uid")?.to_string(),
source_id: tag(xml, "source_id").unwrap_or("").to_string(),
nominal_srate: tag(xml, "nominal_srate")
.and_then(|s| s.trim().parse().ok())
.unwrap_or(0.0),
hostname: tag(xml, "hostname").unwrap_or("").to_string(),
v4data_port: tag(xml, "v4data_port")?.trim().parse().ok()?,
v4service_port: tag(xml, "v4service_port")?.trim().parse().ok()?,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &str = "<?xml version=\"1.0\"?><info><name>Probe</name><type>EEG</type>\
<channel_count>8</channel_count><channel_format>float32</channel_format>\
<source_id>src</source_id><nominal_srate>100.0000000000000</nominal_srate>\
<uid>25cfa701-e55b-4e42-8fc2-103bbd2f86b6</uid><session_id>default</session_id>\
<hostname>box</hostname><v4data_port>16572</v4data_port>\
<v4service_port>16572</v4service_port></info>";
#[test]
fn a_query_round_trips() {
let bytes = build_query("name='Probe'", 40000, "token-1");
assert_eq!(
String::from_utf8_lossy(&bytes),
"LSL:shortinfo\r\nname='Probe'\r\n40000 token-1\r\n"
);
let q = parse_query(&bytes).expect("a query");
assert_eq!(q.query, "name='Probe'");
assert_eq!(q.return_port, 40000);
assert_eq!(q.query_id, "token-1");
}
#[test]
fn an_empty_query_survives_the_round_trip() {
let bytes = build_query("", 1, "t");
let q = parse_query(&bytes).expect("a query");
assert_eq!(q.query, "");
}
#[test]
fn another_method_is_not_a_query() {
assert_eq!(parse_query(b"LSL:timedata\r\n1 2.0\r\n"), None);
}
#[test]
fn an_answer_round_trips() {
let bytes = build_answer("token-1", SAMPLE);
let a = parse_answer(&bytes).expect("an answer");
assert_eq!(a.query_id, "token-1");
assert_eq!(a.xml, SAMPLE);
}
#[test]
fn the_reader_finds_every_field() {
let s = ShortInfo::parse(SAMPLE).expect("a description");
assert_eq!(s.name, "Probe");
assert_eq!(s.channel_count, 8);
assert_eq!(s.channel_format, "float32");
assert_eq!(s.uid, "25cfa701-e55b-4e42-8fc2-103bbd2f86b6");
assert_eq!(s.v4data_port, 16572);
assert_eq!(s.v4service_port, 16572);
assert!((s.nominal_srate - 100.0).abs() < 1e-9);
}
}