]> git.scottworley.com Git - tattlekey/blame - server/src/main.rs
server: Log file name constant
[tattlekey] / server / src / main.rs
CommitLineData
4066e7d1
SW
1// tattlekey: A one-key UDP keyboard
2// Copyright (C) 2023 Scott Worley <scottworley@scottworley.com>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
79d78cde 17use serde::Serialize;
5b8bd049 18use std::collections::HashMap;
dd98493c 19use std::net::UdpSocket;
f9625e91 20use std::time::{Duration, SystemTime};
dd98493c
SW
21
22const MESSAGE_SIZE: usize = 12;
4f2bb592 23const LOGFILENAME: &str = "log.csv";
dd98493c 24
79d78cde 25#[derive(Eq, Debug, Hash, PartialEq, Serialize)]
143d53ed
SW
26struct MessageKey {
27 epoch: u32,
28 device: u16,
29 seq: u16,
30}
31
32#[derive(Debug)]
33struct Message {
34 key: MessageKey,
f9625e91 35 t: SystemTime,
143d53ed
SW
36}
37
38impl From<&[u8; MESSAGE_SIZE]> for Message {
39 fn from(value: &[u8; MESSAGE_SIZE]) -> Self {
f9625e91 40 let ago = u32::from_be_bytes(value[8..=11].try_into().expect("I can't count"));
143d53ed
SW
41 Self {
42 key: MessageKey {
43 epoch: u32::from_be_bytes(value[0..=3].try_into().expect("I can't count")),
44 device: u16::from_be_bytes(value[4..=5].try_into().expect("I can't count")),
45 seq: u16::from_be_bytes(value[6..=7].try_into().expect("I can't count")),
46 },
f9625e91 47 t: SystemTime::now() - Duration::new(ago.into(), 0),
143d53ed
SW
48 }
49 }
50}
51impl TryFrom<&[u8]> for Message {
52 type Error = std::array::TryFromSliceError;
53 fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
54 match <[u8; MESSAGE_SIZE]>::try_from(value) {
55 Ok(correct_size) => Ok(Message::from(&correct_size)),
56 Err(e) => Err(e),
57 }
58 }
59}
60
5b8bd049
SW
61#[derive(Debug)]
62struct Range {
63 start: SystemTime,
64 end: SystemTime,
65}
66impl Range {
67 fn new(t: &SystemTime) -> Self {
68 Self { start: *t, end: *t }
69 }
70 fn contains(&self, t: &SystemTime) -> bool {
71 t > &self.start && t < &self.end
72 }
73 fn extend(&mut self, t: &SystemTime) {
74 if t < &self.start {
75 self.start = *t;
76 }
77 if t > &self.end {
78 self.end = *t;
79 }
80 }
81}
82
891f78b7 83fn main() {
3ad88929
SW
84 let logfile = std::fs::OpenOptions::new()
85 .create(true)
86 .append(true)
4f2bb592 87 .open(LOGFILENAME)
3ad88929
SW
88 .expect("Coudln't open log file");
89 let mut log = csv::Writer::from_writer(logfile);
5b8bd049 90 let mut presses = HashMap::<MessageKey, Range>::new();
dd98493c
SW
91 let socket = UdpSocket::bind("0.0.0.0:29803").expect("couldn't bind to address");
92 loop {
93 let mut buf = [0; MESSAGE_SIZE];
94 match socket.recv_from(&mut buf) {
95 Err(e) => eprintln!("Didn't receive data: {e}"),
96 Ok((number_of_bytes, src_addr)) => {
143d53ed 97 let filled_buf = &buf[..number_of_bytes];
dd98493c
SW
98 if number_of_bytes != MESSAGE_SIZE {
99 eprintln!("Ignoring short message ({number_of_bytes}) from {src_addr}");
100 continue;
101 }
143d53ed 102 let message = Message::try_from(filled_buf).expect("I can't count");
79d78cde
SW
103 log.serialize((&message.key, message.t))
104 .expect("Couldn't write log");
105 log.flush().expect("Couldn't flush log");
5b8bd049
SW
106 if let Some(r) = presses.get_mut(&message.key) {
107 if !r.contains(&message.t) {
108 r.extend(&message.t);
5b8bd049
SW
109 }
110 } else {
5b8bd049
SW
111 presses.insert(message.key, Range::new(&message.t));
112 }
dd98493c
SW
113 }
114 }
115 }
891f78b7 116}