From 641b48b367481e9bda7b6492806932b823b94c4c Mon Sep 17 00:00:00 2001 From: Bastian Gruber Date: Mon, 22 May 2023 15:00:59 +0200 Subject: [PATCH] Send error --- problem_06/src/connection.rs | 1 + problem_06/src/server.rs | 35 +++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/problem_06/src/connection.rs b/problem_06/src/connection.rs index 4267a6f..1fde011 100644 --- a/problem_06/src/connection.rs +++ b/problem_06/src/connection.rs @@ -8,6 +8,7 @@ use tokio::{ use crate::frame::{self, ClientFrames, ServerFrames}; +#[derive(PartialEq)] pub(crate) enum ConnectionType { Camera, Dispatcher, diff --git a/problem_06/src/server.rs b/problem_06/src/server.rs index a16979a..ce7462d 100644 --- a/problem_06/src/server.rs +++ b/problem_06/src/server.rs @@ -180,16 +180,27 @@ impl Handler { ) -> crate::Result<()> { match frame { ClientFrames::Plate { plate, timestamp } => { - info!("Receive new plate: {plate} at {timestamp}"); - issue_possible_ticket( - db, - Plate { - plate: PlateName(plate.clone()), - timestamp: Timestamp(timestamp), - }, - CameraId(self.connection.get_address()), - ) - .await; + if self.connection_type.is_some() + && self.connection_type == Some(ConnectionType::Camera) + { + info!("Receive new plate: {plate} at {timestamp}"); + issue_possible_ticket( + db, + Plate { + plate: PlateName(plate.clone()), + timestamp: Timestamp(timestamp), + }, + CameraId(self.connection.get_address()), + ) + .await; + } else { + let _ = send_message + .send(ServerFrames::Error { + msg: "Not connected as camera".to_string(), + }) + .await; + return Err("Already connected".into()); + } } ClientFrames::WantHeartbeat { interval } => { if interval > 0 { @@ -204,7 +215,7 @@ impl Handler { if self.connection_type.is_some() { let _ = send_message .send(ServerFrames::Error { - msg: "Already connected as camera".to_string(), + msg: "Already connected as a connection type".to_string(), }) .await; return Err("Already connected".into()); @@ -224,7 +235,7 @@ impl Handler { if self.connection_type.is_some() { let _ = send_message .send(ServerFrames::Error { - msg: "Already connected as dispatcher".to_string(), + msg: "Already connected as a connection type".to_string(), }) .await; return Err("Already connected".into());