Skip to content

Commit

Permalink
finish implementing list
Browse files Browse the repository at this point in the history
  • Loading branch information
sullyj3 committed Aug 7, 2024
1 parent f513ed8 commit c5d61b5
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 13 deletions.
2 changes: 2 additions & 0 deletions dev_daemon.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python3

import socket
import sys
import os
Expand Down
5 changes: 2 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

use std::io::{self, BufRead, BufReader, BufWriter, LineWriter, Write};
use std::net::TcpStream;
use std::path::{Path, PathBuf};
use std::io::{self, BufRead, BufReader, LineWriter, Write};
use std::path::PathBuf;
use std::os::unix::net::UnixStream;

use dirs;
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/handle_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl CmdHandlerCtx {
}

fn list(&self) -> ListResponse {
ListResponse::ok(self.state.get_timerinfo_for_client())
ListResponse::ok(self.state.get_timerinfo_for_client(self.now))
}


Expand Down
4 changes: 2 additions & 2 deletions src/daemon/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl DaemonCtx {
id
}

pub fn get_timerinfo_for_client(&self) -> Vec<TimerInfoForClient> {
self.timers.get_timerinfo_for_client()
pub fn get_timerinfo_for_client(&self, now: Instant) -> Vec<TimerInfoForClient> {
self.timers.get_timerinfo_for_client(now)
}

pub fn add_timer(&self, now: Instant, duration: Duration) -> TimerId {
Expand Down
25 changes: 21 additions & 4 deletions src/sand/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,27 @@ pub enum Timer {
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct TimerInfoForClient;
pub enum TimerStateForClient {
Paused,
Running,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct TimerInfoForClient {
id: TimerId,
state: TimerStateForClient,
remaining_millis: u64,
}

impl TimerInfoForClient {
pub fn new(_id: TimerId, _timer: &Timer) -> Self {
Self
impl TimerInfoForClient {

pub fn new(id: TimerId, timer: &Timer, now: Instant) -> Self {
let (state, remaining_millis) = match timer {
Timer::Paused { remaining } =>
(TimerStateForClient::Paused, remaining.as_millis() as u64),
Timer::Running { due, .. } =>
(TimerStateForClient::Running, (*due - now).as_millis() as u64),
};
Self { id, state, remaining_millis }
}
}
9 changes: 6 additions & 3 deletions src/sand/timers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

use std::time::Instant;

use dashmap::{DashMap, Entry};

use crate::sand::timer::*;
Expand All @@ -13,9 +15,10 @@ impl Timers{
}
}

pub fn get_timerinfo_for_client(&self) -> Vec<TimerInfoForClient> {
self.0.iter().map(|rm| {
TimerInfoForClient::new(*rm.key(), rm.value())
pub fn get_timerinfo_for_client(&self, now: Instant) -> Vec<TimerInfoForClient> {
self.0.iter().map(|ref_multi| {
let (id, timer) = ref_multi.pair();
TimerInfoForClient::new(*id, timer, now)
}).collect()
}

Expand Down
3 changes: 3 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def daemon_socket():
log(f"Removing socket file {SOCKET_PATH}")
ensure_deleted(SOCKET_PATH)

# TODO refactor daemon tests to use fake client, client tests to use fake daemon
# they should be testable independently

@pytest.fixture
def daemon(daemon_socket):

Expand Down

0 comments on commit c5d61b5

Please sign in to comment.