Finish happy path of dispatching messages

This commit is contained in:
Bastian Gruber 2023-05-21 08:02:39 +02:00
parent cfdc4e0e53
commit 6e4c12a9b6
No known key found for this signature in database
GPG key ID: BE9F8C772B188CBF
4 changed files with 42 additions and 8 deletions

View file

@ -98,3 +98,20 @@ async fn test_camera_connection(
Ok(())
}
async fn test_dipatcher_connection(
write: &mut WriteHalf<'_>,
) -> Result<(), Box<dyn std::error::Error>> {
// 81 IAmDispatcher{
// 03 roads: [
// 00 42 66,
// 01 70 368,
// 13 88 5000
// ]
// }
let i_am_dispatcher = [0x81, 0x03, 0x00, 0x42, 0x01, 0x70, 0x13, 0x88];
write.write_all(&i_am_dispatcher).await?;
Ok(())
}

View file

@ -7,6 +7,8 @@ use std::{
use tokio::sync::mpsc;
use tracing::debug;
use crate::frame::ServerFrames;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub(crate) struct DispatcherId(pub(crate) SocketAddr);
@ -37,6 +39,20 @@ pub(crate) struct Ticket {
pub(crate) speed: u16,
}
impl From<Ticket> for ServerFrames {
fn from(ticket: Ticket) -> Self {
ServerFrames::Ticket {
plate: ticket.plate,
road: ticket.road,
mile1: ticket.mile1,
timestamp1: ticket.timestamp1,
mile2: ticket.mile2,
timestamp2: ticket.timestamp2,
speed: ticket.speed,
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub(crate) struct Road(pub(crate) u16);
@ -61,7 +77,7 @@ pub(crate) struct Db {
#[derive(Debug)]
struct State {
cameras: HashMap<CameraId, Camera>,
dispatchers: HashMap<Road, Vec<(DispatcherId, mpsc::Sender<Ticket>)>>,
dispatchers: HashMap<Road, Vec<(DispatcherId, mpsc::Sender<ServerFrames>)>>,
plates: HashMap<(Plate, Road), Vec<(Mile, Timestamp)>>,
ticketed_plates_by_day: HashSet<(Timestamp, String)>,
open_tickets: HashMap<Road, Vec<Ticket>>,
@ -105,7 +121,7 @@ impl Db {
&self,
dispatcher_id: DispatcherId,
roads: Vec<u16>,
writer_stream: mpsc::Sender<Ticket>,
writer_stream: mpsc::Sender<ServerFrames>,
) {
let mut state = self.state.lock().unwrap();
@ -120,7 +136,7 @@ impl Db {
debug!(?state);
}
pub(crate) fn get_dispatcher_for_road(&self, road: Road) -> Option<mpsc::Sender<Ticket>> {
pub(crate) fn get_dispatcher_for_road(&self, road: Road) -> Option<mpsc::Sender<ServerFrames>> {
let state = self.state.lock().unwrap();
let senders = state.dispatchers.get(&road);
if senders.is_none() {

View file

@ -181,7 +181,7 @@ impl Handler {
match frame {
ClientFrames::Plate { plate, timestamp } => {
info!("Receive new plate {plate} {timestamp}");
db.insert_plate(
db.add_plate(
CameraId(self.connection.get_address()),
Plate {
plate: plate.clone(),
@ -196,7 +196,8 @@ impl Handler {
timestamp: Timestamp(timestamp),
},
CameraId(self.connection.get_address()),
);
)
.await;
}
ClientFrames::WantHeartbeat { interval } => {
info!("Want heartbeat: {interval}");

View file

@ -1,6 +1,6 @@
use crate::db::{CameraId, Db, Plate, Ticket};
pub(crate) fn issue_possible_ticket(db: &mut Db, plate: Plate, camera_id: CameraId) {
pub(crate) async fn issue_possible_ticket(db: &mut Db, plate: Plate, camera_id: CameraId) {
let camera = db.get_camera(camera_id).unwrap();
let observed_plates = db
.get_plates_by_road(plate.clone(), camera.road.clone())
@ -50,11 +50,11 @@ pub(crate) fn issue_possible_ticket(db: &mut Db, plate: Plate, camera_id: Camera
let dispatcher = db.get_dispatcher_for_road(road.clone());
if dispatcher.is_none() {
db.add_open_ticket(ticket);
db.add_open_ticket(ticket.clone());
continue;
}
dispatcher.unwrap().send(ticket).await;
let _ = dispatcher.unwrap().send(ticket.clone().into()).await;
db.ticket_plate(day, plate_name.clone());
}
}