cargo fmt, restructure db for dispatchers

This commit is contained in:
Bastian Gruber 2023-05-19 15:01:12 +02:00
parent 4b30354214
commit 0fdc85a630
No known key found for this signature in database
GPG key ID: BE9F8C772B188CBF
8 changed files with 620 additions and 608 deletions

View file

@ -1,6 +1,8 @@
use problem_06::{DEFAULT_IP, DEFAULT_PORT};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{tcp::WriteHalf, TcpStream};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{tcp::WriteHalf, TcpStream},
};
use tracing::{debug, error, info};
#[tokio::main]

View file

@ -1,7 +1,5 @@
use problem_06::{server, DEFAULT_IP, DEFAULT_PORT};
use tokio::net::TcpListener;
use tokio::signal;
use tokio::{net::TcpListener, signal};
#[tokio::main]
pub async fn main() -> problem_06::Result<()> {

6
problem_06/rustfmt.toml Normal file
View file

@ -0,0 +1,6 @@
hard_tabs = true
imports_granularity = "Crate"
reorder_impl_items = true
reorder_imports = true
group_imports = "StdExternalCrate"
reorder_modules = true

View file

@ -1,12 +1,14 @@
use crate::frame::{self, ClientFrames, ServerFrames};
use std::{io::Cursor, net::SocketAddr};
use bytes::{Buf, BytesMut};
use std::io::Cursor;
use std::net::SocketAddr;
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufWriter};
use tokio::net::TcpStream;
use tokio::{
io::{AsyncReadExt, AsyncWriteExt, BufWriter},
net::TcpStream,
};
use tracing::{debug, info};
use crate::frame::{self, ClientFrames, ServerFrames};
pub(crate) enum ConnectionType {
Camera,
Dispatcher,

View file

@ -1,6 +1,9 @@
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use std::{
collections::HashMap,
net::SocketAddr,
sync::{Arc, Mutex},
};
use tokio::sync::mpsc;
use tracing::debug;
@ -25,9 +28,10 @@ pub(crate) struct Camera {
pub(crate) limit: u16,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub(crate) struct Road(u16);
pub(crate) struct DbHolder {
/// The `Db` instance that will be shut down when this `DbHolder` struct
/// is dropped.
db: Db,
}
@ -39,19 +43,15 @@ pub(crate) struct Db {
#[derive(Debug)]
struct State {
cameras: HashMap<CameraId, Camera>,
dispatchers: HashMap<DispatcherId, (Vec<u16>, mpsc::Sender<ServerFrames>)>,
dispatchers: HashMap<Road, (DispatcherId, mpsc::Sender<ServerFrames>)>,
plates: HashMap<CameraId, Plate>,
}
impl DbHolder {
/// Create a new `DbHolder`, wrapping a `Db` instance. When this is dropped
/// the `Db`'s purge task will be shut down.
pub(crate) fn new() -> DbHolder {
DbHolder { db: Db::new() }
}
/// Get the shared database. Internally, this is an
/// `Arc`, so a clone only increments the ref count.
pub(crate) fn db(&self) -> Db {
self.db.clone()
}
@ -81,9 +81,13 @@ impl Db {
writer_stream: mpsc::Sender<ServerFrames>,
) {
let mut state = self.state.lock().unwrap();
for r in roads.iter() {
state
.dispatchers
.insert(dispatcher_id, (roads, writer_stream));
.insert(Road(*r), (dispatcher_id.clone(), writer_stream.clone()));
}
debug!(?state);
}

View file

@ -1,8 +1,6 @@
use std::{fmt, io::Cursor, num::TryFromIntError, string::FromUtf8Error};
use bytes::{Buf, BufMut, BytesMut};
use std::fmt;
use std::io::Cursor;
use std::num::TryFromIntError;
use std::string::FromUtf8Error;
use tracing::{debug, error};
#[derive(Clone, Debug)]

View file

@ -1,3 +1,12 @@
use std::{future::Future, sync::Arc};
use tokio::{
net::{TcpListener, TcpStream},
sync::{broadcast, mpsc, Semaphore},
time::{self, Duration},
};
use tracing::{debug, error, info};
use crate::{
connection::ConnectionType,
db::{Camera, CameraId, Db, DbHolder, DispatcherId, Plate},
@ -5,13 +14,6 @@ use crate::{
Connection, Shutdown,
};
use std::future::Future;
use std::sync::Arc;
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{broadcast, mpsc, Semaphore};
use tokio::time::{self, Duration};
use tracing::{debug, error, info};
struct Listener {
listener: TcpListener,
db_holder: DbHolder,