From 8325ffaa1d5c19fb4e76ee82902c1eddfb5a7462 Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Tue, 10 Oct 2023 18:41:36 -0700 Subject: [PATCH 1/1] server: Replay log --- server/src/main.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index 9406811..ddf5217 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use serde::Serialize; +use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::net::UdpSocket; use std::time::{Duration, SystemTime, UNIX_EPOCH}; @@ -29,7 +29,7 @@ struct MessageKey { seq: u16, } -#[derive(Debug, Serialize)] +#[derive(Debug, Deserialize, Serialize)] struct Message { epoch: u32, device: u16, @@ -104,6 +104,20 @@ fn merge_message(presses: &mut HashMap, message: Message) { } } +fn replay_log() -> HashMap { + let mut presses = HashMap::new(); + if std::path::Path::new(LOGFILENAME).exists() { + let mut log = csv::Reader::from_path(LOGFILENAME).expect("Couldn't open log for replay"); + for message in log.deserialize() { + merge_message( + &mut presses, + message.expect("Error reading log during replay"), + ); + } + } + presses +} + fn open_log_for_writing() -> csv::Writer { let log_file_exists = std::path::Path::new(LOGFILENAME).exists(); let logfile = std::fs::OpenOptions::new() @@ -118,7 +132,7 @@ fn open_log_for_writing() -> csv::Writer { fn main() { let socket = UdpSocket::bind("0.0.0.0:29803").expect("couldn't bind to address"); - let mut presses = HashMap::::new(); + let mut presses = replay_log(); let mut log = open_log_for_writing(); loop { let mut buf = [0; MESSAGE_SIZE]; -- 2.44.1