diff --git a/problem_06/src/connection.rs b/problem_06/src/connection.rs index b2ad7ed..6b26c3a 100644 --- a/problem_06/src/connection.rs +++ b/problem_06/src/connection.rs @@ -1,4 +1,4 @@ -use crate::frame::{self, Frame}; +use crate::frame::{self, ClientFrames, ServerFrames}; use bytes::{Buf, BytesMut}; use std::io::Cursor; @@ -20,7 +20,7 @@ impl Connection { } } - pub async fn read_frame(&mut self) -> crate::Result> { + pub async fn read_frame(&mut self) -> crate::Result> { loop { info!("Loop read_frame"); if let Some(frame) = self.parse_frame()? { @@ -38,20 +38,20 @@ impl Connection { } } - fn parse_frame(&mut self) -> crate::Result> { + fn parse_frame(&mut self) -> crate::Result> { use frame::Error::Incomplete; let mut buf = Cursor::new(&self.buffer[..]); debug!(?buf); - match Frame::check(&mut buf) { + match ClientFrames::check(&mut buf) { Ok(_) => { info!("Frame::check succesful"); let len = buf.position() as usize; debug!(?len); buf.set_position(0); - let frame = Frame::parse(&mut buf)?; + let frame = ClientFrames::parse(&mut buf)?; self.buffer.advance(len); Ok(Some(frame)) @@ -61,7 +61,7 @@ impl Connection { } } - pub async fn write_frame(&mut self, frame: &Frame) -> tokio::io::Result<()> { + pub async fn write_frame(&mut self, frame: &ServerFrames) -> tokio::io::Result<()> { unimplemented!() } } diff --git a/problem_06/src/frame.rs b/problem_06/src/frame.rs index 21597c9..5654471 100644 --- a/problem_06/src/frame.rs +++ b/problem_06/src/frame.rs @@ -6,28 +6,44 @@ use std::string::FromUtf8Error; use tracing::{debug, error}; #[derive(Clone, Debug)] -pub enum Frame { - Error { msg: String }, +pub enum ClientFrames { Plate { plate: String, timestamp: u32 }, WantHeartbeat { interval: u32 }, IAmCamera { road: u16, mile: u16, limit: u16 }, IAmDispatcher { roads: Vec }, } +#[derive(Clone, Debug)] +pub enum ServerFrames { + Error { + msg: String, + }, + Ticket { + plate: String, + road: u16, + mile1: u16, + timestamp1: u16, + mile2: u16, + timestamp2: u16, + speed: u16, + }, + Heartbeat, +} + #[derive(Debug)] pub enum Error { Incomplete, Other(crate::Error), } -impl Frame { +impl ClientFrames { pub fn check(src: &mut Cursor<&[u8]>) -> Result<(), Error> { match get_u8(src)? { - // Error: msg: str - 0x10 => { - let n = get_length(src)?; - skip(src, n as usize) - } + // Error: msg: str (Server -> Client) + // 0x10 => { + // let n = get_length(src)?; + // skip(src, n as usize) + // } // Plate: plate: str, timestamp: u32 0x20 => { // Read length character of the plate string @@ -73,14 +89,14 @@ impl Frame { } } - pub fn parse(src: &mut Cursor<&[u8]>) -> Result { + pub fn parse(src: &mut Cursor<&[u8]>) -> Result { match get_u8(src)? { - // Error: msg: str - 0x10 => { - let n = get_length(src)?; - let msg = get_str(src, n)?.to_string(); - Ok(Frame::Error { msg }) - } + // Error: msg: str (Server -> Client) + // 0x10 => { + // let n = get_length(src)?; + // let msg = get_str(src, n)?.to_string(); + // Ok(Frame::Error { msg }) + // } // Plate: plate: str, timestamp: u32 0x20 => { // Read length character of the plate string @@ -89,7 +105,7 @@ impl Frame { let plate = get_str(src, n)?.to_string(); // check if valid timestamp let timestamp = get_u32(src)?; - Ok(Frame::Plate { plate, timestamp }) + Ok(ClientFrames::Plate { plate, timestamp }) } // Ticket (just Server -> Client) // 0x21 => { @@ -98,7 +114,7 @@ impl Frame { // Want Heartbeat: interval: u32 0x40 => { let interval = get_u32(src)?; - Ok(Frame::WantHeartbeat { interval }) + Ok(ClientFrames::WantHeartbeat { interval }) } // Heartbeat (just Server -> Client) // 0x41 => { @@ -112,7 +128,7 @@ impl Frame { let mile = get_u16(src)?; // limit let limit = get_u16(src)?; - Ok(Frame::IAmCamera { road, mile, limit }) + Ok(ClientFrames::IAmCamera { road, mile, limit }) } // IAmDispatcher: numroads: u8, roads: [u16] 0x81 => { @@ -121,7 +137,7 @@ impl Frame { // roads let roads = get_u16_vec(src, numroads as usize)?; - Ok(Frame::IAmDispatcher { roads }) + Ok(ClientFrames::IAmDispatcher { roads }) } actual => Err(format!("protocol error; invalid frame type byte `{}`", actual).into()), } diff --git a/problem_06/src/lib.rs b/problem_06/src/lib.rs index bdc2cc7..4ba6a3a 100644 --- a/problem_06/src/lib.rs +++ b/problem_06/src/lib.rs @@ -2,7 +2,7 @@ mod connection; pub use connection::Connection; pub mod frame; -pub use frame::Frame; +pub use frame::ClientFrames; pub mod server; diff --git a/problem_06/src/server.rs b/problem_06/src/server.rs index 2cfd3df..835174b 100644 --- a/problem_06/src/server.rs +++ b/problem_06/src/server.rs @@ -1,4 +1,4 @@ -use crate::{frame::Frame, Connection, Shutdown}; +use crate::{frame::ClientFrames, Connection, Shutdown}; use std::future::Future; use std::sync::Arc; @@ -8,6 +8,7 @@ use tokio::time::{self, Duration}; use tracing::{debug, error, info}; struct Listener { + db_holder: DbDropGuard, listener: TcpListener, limit_connections: Arc, notify_shutdown: broadcast::Sender<()>, @@ -129,19 +130,16 @@ impl Handler { }; match frame { - Frame::Error { msg } => { - info!("Error message: {msg}") - } - Frame::Plate { plate, timestamp } => { + ClientFrames::Plate { plate, timestamp } => { info!("Plate: {plate}, timestamp: {timestamp}"); } - Frame::WantHeartbeat { interval } => { + ClientFrames::WantHeartbeat { interval } => { info!("Want heartbeat: {interval}"); } - Frame::IAmCamera { road, mile, limit } => { + ClientFrames::IAmCamera { road, mile, limit } => { info!("Road: {road}, mile: {mile}, limit: {limit}"); } - Frame::IAmDispatcher { roads } => { + ClientFrames::IAmDispatcher { roads } => { info!("roads: {roads:?}"); } }