Change from Frame to ClientFrames and ServerFrames

This commit is contained in:
Bastian Gruber 2023-05-08 12:25:09 +02:00
parent 2902d9d19c
commit 0233e3196d
No known key found for this signature in database
GPG key ID: BE9F8C772B188CBF
4 changed files with 48 additions and 34 deletions

View file

@ -1,4 +1,4 @@
use crate::frame::{self, Frame}; use crate::frame::{self, ClientFrames, ServerFrames};
use bytes::{Buf, BytesMut}; use bytes::{Buf, BytesMut};
use std::io::Cursor; use std::io::Cursor;
@ -20,7 +20,7 @@ impl Connection {
} }
} }
pub async fn read_frame(&mut self) -> crate::Result<Option<Frame>> { pub async fn read_frame(&mut self) -> crate::Result<Option<ClientFrames>> {
loop { loop {
info!("Loop read_frame"); info!("Loop read_frame");
if let Some(frame) = self.parse_frame()? { if let Some(frame) = self.parse_frame()? {
@ -38,20 +38,20 @@ impl Connection {
} }
} }
fn parse_frame(&mut self) -> crate::Result<Option<Frame>> { fn parse_frame(&mut self) -> crate::Result<Option<ClientFrames>> {
use frame::Error::Incomplete; use frame::Error::Incomplete;
let mut buf = Cursor::new(&self.buffer[..]); let mut buf = Cursor::new(&self.buffer[..]);
debug!(?buf); debug!(?buf);
match Frame::check(&mut buf) { match ClientFrames::check(&mut buf) {
Ok(_) => { Ok(_) => {
info!("Frame::check succesful"); info!("Frame::check succesful");
let len = buf.position() as usize; let len = buf.position() as usize;
debug!(?len); debug!(?len);
buf.set_position(0); buf.set_position(0);
let frame = Frame::parse(&mut buf)?; let frame = ClientFrames::parse(&mut buf)?;
self.buffer.advance(len); self.buffer.advance(len);
Ok(Some(frame)) 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!() unimplemented!()
} }
} }

View file

@ -6,28 +6,44 @@ use std::string::FromUtf8Error;
use tracing::{debug, error}; use tracing::{debug, error};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Frame { pub enum ClientFrames {
Error { msg: String },
Plate { plate: String, timestamp: u32 }, Plate { plate: String, timestamp: u32 },
WantHeartbeat { interval: u32 }, WantHeartbeat { interval: u32 },
IAmCamera { road: u16, mile: u16, limit: u16 }, IAmCamera { road: u16, mile: u16, limit: u16 },
IAmDispatcher { roads: Vec<u16> }, IAmDispatcher { roads: Vec<u16> },
} }
#[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)] #[derive(Debug)]
pub enum Error { pub enum Error {
Incomplete, Incomplete,
Other(crate::Error), Other(crate::Error),
} }
impl Frame { impl ClientFrames {
pub fn check(src: &mut Cursor<&[u8]>) -> Result<(), Error> { pub fn check(src: &mut Cursor<&[u8]>) -> Result<(), Error> {
match get_u8(src)? { match get_u8(src)? {
// Error: msg: str // Error: msg: str (Server -> Client)
0x10 => { // 0x10 => {
let n = get_length(src)?; // let n = get_length(src)?;
skip(src, n as usize) // skip(src, n as usize)
} // }
// Plate: plate: str, timestamp: u32 // Plate: plate: str, timestamp: u32
0x20 => { 0x20 => {
// Read length character of the plate string // Read length character of the plate string
@ -73,14 +89,14 @@ impl Frame {
} }
} }
pub fn parse(src: &mut Cursor<&[u8]>) -> Result<Frame, Error> { pub fn parse(src: &mut Cursor<&[u8]>) -> Result<ClientFrames, Error> {
match get_u8(src)? { match get_u8(src)? {
// Error: msg: str // Error: msg: str (Server -> Client)
0x10 => { // 0x10 => {
let n = get_length(src)?; // let n = get_length(src)?;
let msg = get_str(src, n)?.to_string(); // let msg = get_str(src, n)?.to_string();
Ok(Frame::Error { msg }) // Ok(Frame::Error { msg })
} // }
// Plate: plate: str, timestamp: u32 // Plate: plate: str, timestamp: u32
0x20 => { 0x20 => {
// Read length character of the plate string // Read length character of the plate string
@ -89,7 +105,7 @@ impl Frame {
let plate = get_str(src, n)?.to_string(); let plate = get_str(src, n)?.to_string();
// check if valid timestamp // check if valid timestamp
let timestamp = get_u32(src)?; let timestamp = get_u32(src)?;
Ok(Frame::Plate { plate, timestamp }) Ok(ClientFrames::Plate { plate, timestamp })
} }
// Ticket (just Server -> Client) // Ticket (just Server -> Client)
// 0x21 => { // 0x21 => {
@ -98,7 +114,7 @@ impl Frame {
// Want Heartbeat: interval: u32 // Want Heartbeat: interval: u32
0x40 => { 0x40 => {
let interval = get_u32(src)?; let interval = get_u32(src)?;
Ok(Frame::WantHeartbeat { interval }) Ok(ClientFrames::WantHeartbeat { interval })
} }
// Heartbeat (just Server -> Client) // Heartbeat (just Server -> Client)
// 0x41 => { // 0x41 => {
@ -112,7 +128,7 @@ impl Frame {
let mile = get_u16(src)?; let mile = get_u16(src)?;
// limit // limit
let limit = get_u16(src)?; let limit = get_u16(src)?;
Ok(Frame::IAmCamera { road, mile, limit }) Ok(ClientFrames::IAmCamera { road, mile, limit })
} }
// IAmDispatcher: numroads: u8, roads: [u16] // IAmDispatcher: numroads: u8, roads: [u16]
0x81 => { 0x81 => {
@ -121,7 +137,7 @@ impl Frame {
// roads // roads
let roads = get_u16_vec(src, numroads as usize)?; 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()), actual => Err(format!("protocol error; invalid frame type byte `{}`", actual).into()),
} }

View file

@ -2,7 +2,7 @@ mod connection;
pub use connection::Connection; pub use connection::Connection;
pub mod frame; pub mod frame;
pub use frame::Frame; pub use frame::ClientFrames;
pub mod server; pub mod server;

View file

@ -1,4 +1,4 @@
use crate::{frame::Frame, Connection, Shutdown}; use crate::{frame::ClientFrames, Connection, Shutdown};
use std::future::Future; use std::future::Future;
use std::sync::Arc; use std::sync::Arc;
@ -8,6 +8,7 @@ use tokio::time::{self, Duration};
use tracing::{debug, error, info}; use tracing::{debug, error, info};
struct Listener { struct Listener {
db_holder: DbDropGuard,
listener: TcpListener, listener: TcpListener,
limit_connections: Arc<Semaphore>, limit_connections: Arc<Semaphore>,
notify_shutdown: broadcast::Sender<()>, notify_shutdown: broadcast::Sender<()>,
@ -129,19 +130,16 @@ impl Handler {
}; };
match frame { match frame {
Frame::Error { msg } => { ClientFrames::Plate { plate, timestamp } => {
info!("Error message: {msg}")
}
Frame::Plate { plate, timestamp } => {
info!("Plate: {plate}, timestamp: {timestamp}"); info!("Plate: {plate}, timestamp: {timestamp}");
} }
Frame::WantHeartbeat { interval } => { ClientFrames::WantHeartbeat { interval } => {
info!("Want heartbeat: {interval}"); info!("Want heartbeat: {interval}");
} }
Frame::IAmCamera { road, mile, limit } => { ClientFrames::IAmCamera { road, mile, limit } => {
info!("Road: {road}, mile: {mile}, limit: {limit}"); info!("Road: {road}, mile: {mile}, limit: {limit}");
} }
Frame::IAmDispatcher { roads } => { ClientFrames::IAmDispatcher { roads } => {
info!("roads: {roads:?}"); info!("roads: {roads:?}");
} }
} }