diff options
| author | LLLL Colonq <llll@colonq> | 2025-01-28 12:03:11 -0500 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2025-01-28 12:03:11 -0500 |
| commit | 23d651bbad510f14484cf1c0a8081ffff890565e (patch) | |
| tree | 3645723cb9383bcf8ea3733d50d76ba1fe86e131 /src/common/overlay/fig.rs | |
| parent | f448de77d84b985047a332150c0382adc1836899 (diff) | |
Working overlay
Diffstat (limited to 'src/common/overlay/fig.rs')
| -rw-r--r-- | src/common/overlay/fig.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/common/overlay/fig.rs b/src/common/overlay/fig.rs new file mode 100644 index 0000000..b60f8e9 --- /dev/null +++ b/src/common/overlay/fig.rs @@ -0,0 +1,40 @@ +use std::io::{BufRead, Write}; + +#[derive(Debug, Clone)] +pub struct Message { + pub event: lexpr::Value, + pub data: lexpr::Value, +} + +pub struct Client { + reader: std::io::BufReader<std::net::TcpStream>, +} +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, } + } + pub fn pump(&mut self) -> Option<Message> { + let mut buf = String::new(); + match self.reader.read_line(&mut buf) { + Ok(l) => match lexpr::from_str(&buf) { + 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 => None, + Err(e) => panic!("IO error on message bus: {}", e), + } + } +} |
