diff options
| author | Scott Lawrence <scott+git@ineffectivetheory.com> | 2024-06-29 11:02:33 -0700 |
|---|---|---|
| committer | Scott Lawrence <scott+git@ineffectivetheory.com> | 2024-06-29 11:02:33 -0700 |
| commit | 272e2d62e044a785db64becf2d8006fdda985d6a (patch) | |
| tree | 442ab7219242ef173932b8cc268a9bab34e62553 | |
| parent | a594313b5f47531138b79795fdf72ae74776f1cf (diff) | |
| download | varanus-272e2d62e044a785db64becf2d8006fdda985d6a.tar.gz varanus-272e2d62e044a785db64becf2d8006fdda985d6a.tar.bz2 varanus-272e2d62e044a785db64becf2d8006fdda985d6a.zip | |
Fetching state over socket
| -rw-r--r-- | src/main.rs | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 0282ec7..e9e5e0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ -use std::io::Write; +use std::error; +use std::io::{Read,Write}; use std::os::unix::net::{UnixStream,UnixListener}; +use std::str; use std::sync::{Arc,Mutex}; use std::thread::{sleep,spawn}; use std::time::{Duration,Instant,SystemTime}; @@ -37,6 +39,8 @@ struct State { asof: SystemTime, } +type Result<T> = std::result::Result<T, Box<dyn error::Error>>; + fn init_state() -> State { let asof = SystemTime::now(); return State { @@ -49,9 +53,11 @@ fn update_state(state: &mut State) { state.asof = asof; } -fn get_state(sockfile: String) -> std::io::Result<State> { - let mut socket = UnixStream::connect(sockfile); - Ok(init_state()) +fn get_state(sockfile: String) -> Result<State> { + let mut socket = UnixStream::connect(sockfile)?; + let mut buf = vec![]; + socket.read_to_end(&mut buf)?; + Ok(serde_json::from_str::<State>(str::from_utf8(&buf)?)?) } fn main() { |
