]> git.scottworley.com Git - tattlekey/blame - server/src/main.rs
server: Drop `ago` field
[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
5b8bd049 17use std::collections::HashMap;
dd98493c 18use std::net::UdpSocket;
f9625e91 19use std::time::{Duration, SystemTime};
dd98493c
SW
20
21const MESSAGE_SIZE: usize = 12;
22
5b8bd049 23#[derive(Eq, Debug, Hash, PartialEq)]
143d53ed
SW
24struct MessageKey {
25 epoch: u32,
26 device: u16,
27 seq: u16,
28}
29
30#[derive(Debug)]
31struct Message {
32 key: MessageKey,
f9625e91 33 t: SystemTime,
143d53ed
SW
34}
35
36impl From<&[u8; MESSAGE_SIZE]> for Message {
37 fn from(value: &[u8; MESSAGE_SIZE]) -> Self {
f9625e91 38 let ago = u32::from_be_bytes(value[8..=11].try_into().expect("I can't count"));
143d53ed
SW
39 Self {
40 key: MessageKey {
41 epoch: u32::from_be_bytes(value[0..=3].try_into().expect("I can't count")),
42 device: u16::from_be_bytes(value[4..=5].try_into().expect("I can't count")),
43 seq: u16::from_be_bytes(value[6..=7].try_into().expect("I can't count")),
44 },
f9625e91 45 t: SystemTime::now() - Duration::new(ago.into(), 0),
143d53ed
SW
46 }
47 }
48}
49impl TryFrom<&[u8]> for Message {
50 type Error = std::array::TryFromSliceError;
51 fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
52 match <[u8; MESSAGE_SIZE]>::try_from(value) {
53 Ok(correct_size) => Ok(Message::from(&correct_size)),
54 Err(e) => Err(e),
55 }
56 }
57}
58
5b8bd049
SW
59#[derive(Debug)]
60struct Range {
61 start: SystemTime,
62 end: SystemTime,
63}
64impl Range {
65 fn new(t: &SystemTime) -> Self {
66 Self { start: *t, end: *t }
67 }
68 fn contains(&self, t: &SystemTime) -> bool {
69 t > &self.start && t < &self.end
70 }
71 fn extend(&mut self, t: &SystemTime) {
72 if t < &self.start {
73 self.start = *t;
74 }
75 if t > &self.end {
76 self.end = *t;
77 }
78 }
79}
80
891f78b7 81fn main() {
5b8bd049 82 let mut presses = HashMap::<MessageKey, Range>::new();
dd98493c
SW
83 let socket = UdpSocket::bind("0.0.0.0:29803").expect("couldn't bind to address");
84 loop {
85 let mut buf = [0; MESSAGE_SIZE];
86 match socket.recv_from(&mut buf) {
87 Err(e) => eprintln!("Didn't receive data: {e}"),
88 Ok((number_of_bytes, src_addr)) => {
143d53ed 89 let filled_buf = &buf[..number_of_bytes];
dd98493c
SW
90 if number_of_bytes != MESSAGE_SIZE {
91 eprintln!("Ignoring short message ({number_of_bytes}) from {src_addr}");
92 continue;
93 }
143d53ed 94 let message = Message::try_from(filled_buf).expect("I can't count");
5b8bd049
SW
95 if let Some(r) = presses.get_mut(&message.key) {
96 if !r.contains(&message.t) {
97 r.extend(&message.t);
98 println!("Updated press: {:?}: {r:?}", message.key);
99 }
100 } else {
101 println!("Got new press: {:?}: {:?}", message.key, message.t);
102 presses.insert(message.key, Range::new(&message.t));
103 }
dd98493c
SW
104 }
105 }
106 }
891f78b7 107}