Skip to content

Commit

Permalink
switch messages to tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
Donald Ball committed Jan 29, 2023
1 parent 9adbefb commit 3c48538
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 32 deletions.
14 changes: 7 additions & 7 deletions rust/speed/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io;
use std::{collections::BTreeSet, io};

use crate::domain::{Camera, Message};
use crate::domain::{Camera, Dispatcher, Message, Road};

use tokio::{
io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter},
Expand All @@ -25,7 +25,7 @@ impl<'a> Connection<'a> {

pub async fn write_message(&mut self, message: &Message) -> io::Result<()> {
match message {
Message::Error { msg } => {
Message::Error(msg) => {
self.writer.write_u8(0x10).await?;
// write_string
self.writer.write_u8(msg.len().try_into().unwrap()).await?;
Expand Down Expand Up @@ -73,7 +73,7 @@ impl<'a> Connection<'a> {
}
0x40 => {
let interval = self.reader.read_u32().await?;
Ok(Message::WantHeartbeat { interval })
Ok(Message::WantHeartbeat(interval))
}
0x80 => {
let road = self.reader.read_u16().await?;
Expand All @@ -83,11 +83,11 @@ impl<'a> Connection<'a> {
}
0x81 => {
let numroads = self.reader.read_u8().await?;
let mut roads: Vec<u16> = Vec::with_capacity(numroads.into());
let mut roads: BTreeSet<Road> = BTreeSet::new();
for _ in 0..numroads {
roads.push(self.reader.read_u16().await?);
roads.insert(self.reader.read_u16().await?);
}
Ok(Message::IAmDispatcher { numroads, roads })
Ok(Message::IAmDispatcher(Dispatcher { roads }))
}
_ => Err(io::Error::from(io::ErrorKind::Unsupported)),
}
Expand Down
15 changes: 3 additions & 12 deletions rust/speed/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,19 @@ use std::collections::{BTreeMap, BTreeSet, VecDeque};
// TODO how can we construct de/serialization code for these declaratively?
pub enum Message {
// server -> client
Error {
msg: String,
},
Error(String),
// camera -> server
Plate(Plate, Timestamp),
// server -> dispatcher
Ticket(Ticket),
// client -> server
WantHeartbeat {
interval: Deciseconds,
},
WantHeartbeat(Deciseconds),
// server -> client
Heartbeat,
// (client->camera) -> server
IAmCamera(Camera),
// (client->dispatcher) -> server
IAmDispatcher {
// Note this is redundant with roads.len(), just a serialization artifact.
// Do we keep it in the struct or compute it at serialization?
numroads: u8,
roads: Vec<Road>,
},
IAmDispatcher(Dispatcher),
}

pub type Timestamp = u32;
Expand Down
16 changes: 3 additions & 13 deletions rust/speed/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ enum ConnKind {
}

async fn send_error(mut conn: Connection<'_>, msg: &str) -> Result<(), io::Error> {
let msg = Message::Error {
msg: msg.to_string(),
};
conn.write_message(&msg).await?;
conn.write_message(&Message::Error(msg.to_string())).await?;
Ok(())
}

Expand All @@ -94,7 +91,7 @@ async fn handle(mut socket: TcpStream, tx: mpsc::Sender<ServerCommand>) -> Resul
msg = conn.read_message() => {
// TODO do we send_error on error?
let msg = msg?;
if let Message::WantHeartbeat { interval } = msg {
if let Message::WantHeartbeat(interval) = msg {
if heartbeat.is_some() {
return send_error(conn, "already beating").await;
}
Expand All @@ -107,15 +104,8 @@ async fn handle(mut socket: TcpStream, tx: mpsc::Sender<ServerCommand>) -> Resul
Message::IAmCamera(camera) => {
kind = ConnKind::Camera(camera);
}
Message::IAmDispatcher { numroads: _, roads } => {
// TODO should this be a from/into relation between the msg and the struct?
let mut droads = BTreeSet::new();
roads.iter().for_each(|road| {
droads.insert(*road);
});
let dispatcher = Dispatcher { roads: droads };
Message::IAmDispatcher(dispatcher) => {
kind = ConnKind::Dispatcher(dispatcher);
todo!("register the dispatcher locally");
}
_ => {
return send_error(conn, "unidentified").await;
Expand Down

0 comments on commit 3c48538

Please sign in to comment.