Add heartbeat
This commit is contained in:
parent
6e4c12a9b6
commit
15f64eb8c8
6 changed files with 53 additions and 13 deletions
|
|
@ -75,6 +75,7 @@ async fn test_all_different_messages(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn test_camera_connection(
|
||||
write: &mut WriteHalf<'_>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
|
@ -99,6 +100,7 @@ async fn test_camera_connection(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn test_dipatcher_connection(
|
||||
write: &mut WriteHalf<'_>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ use tokio::{net::TcpListener, signal};
|
|||
pub async fn main() -> problem_06::Result<()> {
|
||||
tracing_subscriber::fmt::try_init().expect("Couldn't setup logging");
|
||||
|
||||
// Bind a TCP listener
|
||||
let listener = TcpListener::bind(&format!("{DEFAULT_IP}:{DEFAULT_PORT}")).await?;
|
||||
|
||||
let _ = server::run(listener, signal::ctrl_c()).await;
|
||||
|
|
|
|||
|
|
@ -200,6 +200,5 @@ impl Db {
|
|||
state
|
||||
.ticketed_plates_by_day
|
||||
.contains(&(Timestamp(day), plate_name))
|
||||
// debug!(?state);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
39
problem_06/src/heartbeat.rs
Normal file
39
problem_06/src/heartbeat.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
use crate::frame::ServerFrames;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
pub(crate) struct Heartbeat {
|
||||
is_running: bool,
|
||||
interval: Duration,
|
||||
message: mpsc::Sender<ServerFrames>,
|
||||
}
|
||||
|
||||
impl Heartbeat {
|
||||
pub(crate) fn new(interval: u32, message: mpsc::Sender<ServerFrames>) -> Self {
|
||||
Self {
|
||||
is_running: false,
|
||||
interval: Duration::from_millis((interval * 100) as u64),
|
||||
message,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn start(&mut self) {
|
||||
if self.is_running {
|
||||
let _ = self.message.send(ServerFrames::Error {
|
||||
msg: "Heartbeat alreadt exists".to_string(),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
self.is_running = true;
|
||||
|
||||
let mut interval = tokio::time::interval(self.interval);
|
||||
|
||||
interval.tick().await;
|
||||
|
||||
loop {
|
||||
interval.tick().await;
|
||||
let _ = self.message.send(ServerFrames::Heartbeat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,16 +1,13 @@
|
|||
mod connection;
|
||||
pub use connection::Connection;
|
||||
|
||||
pub mod frame;
|
||||
pub use frame::ClientFrames;
|
||||
|
||||
pub mod db;
|
||||
|
||||
mod db;
|
||||
mod frame;
|
||||
mod heartbeat;
|
||||
pub mod server;
|
||||
|
||||
pub mod ticketing;
|
||||
|
||||
mod shutdown;
|
||||
mod ticketing;
|
||||
|
||||
pub use connection::Connection;
|
||||
pub use frame::ClientFrames;
|
||||
use shutdown::Shutdown;
|
||||
|
||||
pub const DEFAULT_IP: &'static str = "0.0.0.0";
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ use crate::{
|
|||
connection::ConnectionType,
|
||||
db::{Camera, CameraId, Db, DbHolder, DispatcherId, Limit, Mile, Plate, Road, Timestamp},
|
||||
frame::{ClientFrames, ServerFrames},
|
||||
heartbeat::Heartbeat,
|
||||
ticketing::issue_possible_ticket,
|
||||
Connection, Shutdown,
|
||||
};
|
||||
|
|
@ -200,7 +201,10 @@ impl Handler {
|
|||
.await;
|
||||
}
|
||||
ClientFrames::WantHeartbeat { interval } => {
|
||||
info!("Want heartbeat: {interval}");
|
||||
tokio::spawn(async move {
|
||||
let mut heartbeat = Heartbeat::new(interval, send_message.clone());
|
||||
heartbeat.start().await;
|
||||
});
|
||||
}
|
||||
ClientFrames::IAmCamera { road, mile, limit } => {
|
||||
if self.connection_type.is_some() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue