From: Scott Worley Date: Wed, 11 Oct 2023 01:41:36 +0000 (-0700) Subject: server: Replay log X-Git-Tag: v0.1.0~2 X-Git-Url: http://git.scottworley.com/tattlekey/commitdiff_plain/8325ffaa1d5c19fb4e76ee82902c1eddfb5a7462?ds=inline;hp=e8a97c557b79be528de66f9c8e39f21f7d41527d server: Replay log --- 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];