X-Git-Url: http://git.scottworley.com/tattlekey/blobdiff_plain/e8a97c557b79be528de66f9c8e39f21f7d41527d..3c8c23bba013dd7aad787692bc86d58e713977d2:/server/src/main.rs
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];