diff options
| author | Scott Lawrence <scott+git@ineffectivetheory.com> | 2024-06-29 00:58:28 -0700 |
|---|---|---|
| committer | Scott Lawrence <scott+git@ineffectivetheory.com> | 2024-06-29 00:58:28 -0700 |
| commit | 41a1f3ed336d3a03b9ce4091ee4dde0b724511e3 (patch) | |
| tree | b3cca435ff14acc45acbb8a4bc55f1ced9564972 | |
| parent | 11091289776ef6941f8c6558991484c7fcfa1069 (diff) | |
| download | varanus-41a1f3ed336d3a03b9ce4091ee4dde0b724511e3.tar.gz varanus-41a1f3ed336d3a03b9ce4091ee4dde0b724511e3.tar.bz2 varanus-41a1f3ed336d3a03b9ce4091ee4dde0b724511e3.zip | |
Spawning a thread and opening a socket file
| -rw-r--r-- | src/main.rs | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index d25f128..fb1165d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ -use std::thread::sleep; -use std::time::{Duration,Instant,SystemTime}; use std::os::unix::net::{UnixStream,UnixListener}; +use std::sync::Mutex; +use std::thread::{sleep,spawn}; +use std::time::{Duration,Instant,SystemTime}; use clap::Parser; use serde::{Serialize,Deserialize}; @@ -13,6 +14,8 @@ struct Cli { verbose: bool, #[arg(short='D', long, default_value="10")] delay: u64, + #[arg(short='s', long, default_value="varanus.sock")] + socket: String, } #[derive(Serialize, Deserialize)] @@ -45,22 +48,33 @@ 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 main() { let args = Cli::parse(); if args.daemon { let start = Instant::now(); let delay_ms: u64 = args.delay * 1000; let mut cycle: u64 = 0; - let mut state = init_state(); + let state_mutex = Mutex::new(init_state()); + let listener = spawn(move || { + }); loop { cycle += 1; sleep(Duration::from_millis(cycle*delay_ms) - start.elapsed()); if args.verbose { println!("{:?} elapsed; updating state...", start.elapsed()); } - update_state(&mut state); + { + let mut state = state_mutex.lock().unwrap(); + update_state(&mut state); + } } } else { + let state = get_state(args.socket).unwrap(); } } |
