cargo fmt, restructure db for dispatchers
This commit is contained in:
parent
4b30354214
commit
0fdc85a630
8 changed files with 620 additions and 608 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
use problem_06::{DEFAULT_IP, DEFAULT_PORT};
|
use problem_06::{DEFAULT_IP, DEFAULT_PORT};
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
use tokio::{
|
||||||
use tokio::net::{tcp::WriteHalf, TcpStream};
|
io::{AsyncReadExt, AsyncWriteExt},
|
||||||
|
net::{tcp::WriteHalf, TcpStream},
|
||||||
|
};
|
||||||
use tracing::{debug, error, info};
|
use tracing::{debug, error, info};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
use problem_06::{server, DEFAULT_IP, DEFAULT_PORT};
|
use problem_06::{server, DEFAULT_IP, DEFAULT_PORT};
|
||||||
|
use tokio::{net::TcpListener, signal};
|
||||||
use tokio::net::TcpListener;
|
|
||||||
use tokio::signal;
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
pub async fn main() -> problem_06::Result<()> {
|
pub async fn main() -> problem_06::Result<()> {
|
||||||
|
|
|
||||||
6
problem_06/rustfmt.toml
Normal file
6
problem_06/rustfmt.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
hard_tabs = true
|
||||||
|
imports_granularity = "Crate"
|
||||||
|
reorder_impl_items = true
|
||||||
|
reorder_imports = true
|
||||||
|
group_imports = "StdExternalCrate"
|
||||||
|
reorder_modules = true
|
||||||
|
|
@ -1,12 +1,14 @@
|
||||||
use crate::frame::{self, ClientFrames, ServerFrames};
|
use std::{io::Cursor, net::SocketAddr};
|
||||||
|
|
||||||
use bytes::{Buf, BytesMut};
|
use bytes::{Buf, BytesMut};
|
||||||
use std::io::Cursor;
|
use tokio::{
|
||||||
use std::net::SocketAddr;
|
io::{AsyncReadExt, AsyncWriteExt, BufWriter},
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufWriter};
|
net::TcpStream,
|
||||||
use tokio::net::TcpStream;
|
};
|
||||||
use tracing::{debug, info};
|
use tracing::{debug, info};
|
||||||
|
|
||||||
|
use crate::frame::{self, ClientFrames, ServerFrames};
|
||||||
|
|
||||||
pub(crate) enum ConnectionType {
|
pub(crate) enum ConnectionType {
|
||||||
Camera,
|
Camera,
|
||||||
Dispatcher,
|
Dispatcher,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
use std::collections::HashMap;
|
use std::{
|
||||||
use std::net::SocketAddr;
|
collections::HashMap,
|
||||||
use std::sync::{Arc, Mutex};
|
net::SocketAddr,
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
|
|
@ -25,9 +28,10 @@ pub(crate) struct Camera {
|
||||||
pub(crate) limit: u16,
|
pub(crate) limit: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
|
||||||
|
pub(crate) struct Road(u16);
|
||||||
|
|
||||||
pub(crate) struct DbHolder {
|
pub(crate) struct DbHolder {
|
||||||
/// The `Db` instance that will be shut down when this `DbHolder` struct
|
|
||||||
/// is dropped.
|
|
||||||
db: Db,
|
db: Db,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,19 +43,15 @@ pub(crate) struct Db {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct State {
|
struct State {
|
||||||
cameras: HashMap<CameraId, Camera>,
|
cameras: HashMap<CameraId, Camera>,
|
||||||
dispatchers: HashMap<DispatcherId, (Vec<u16>, mpsc::Sender<ServerFrames>)>,
|
dispatchers: HashMap<Road, (DispatcherId, mpsc::Sender<ServerFrames>)>,
|
||||||
plates: HashMap<CameraId, Plate>,
|
plates: HashMap<CameraId, Plate>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DbHolder {
|
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 {
|
pub(crate) fn new() -> DbHolder {
|
||||||
DbHolder { db: Db::new() }
|
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 {
|
pub(crate) fn db(&self) -> Db {
|
||||||
self.db.clone()
|
self.db.clone()
|
||||||
}
|
}
|
||||||
|
|
@ -81,9 +81,13 @@ impl Db {
|
||||||
writer_stream: mpsc::Sender<ServerFrames>,
|
writer_stream: mpsc::Sender<ServerFrames>,
|
||||||
) {
|
) {
|
||||||
let mut state = self.state.lock().unwrap();
|
let mut state = self.state.lock().unwrap();
|
||||||
|
|
||||||
|
for r in roads.iter() {
|
||||||
state
|
state
|
||||||
.dispatchers
|
.dispatchers
|
||||||
.insert(dispatcher_id, (roads, writer_stream));
|
.insert(Road(*r), (dispatcher_id.clone(), writer_stream.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
debug!(?state);
|
debug!(?state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
|
use std::{fmt, io::Cursor, num::TryFromIntError, string::FromUtf8Error};
|
||||||
|
|
||||||
use bytes::{Buf, BufMut, BytesMut};
|
use bytes::{Buf, BufMut, BytesMut};
|
||||||
use std::fmt;
|
|
||||||
use std::io::Cursor;
|
|
||||||
use std::num::TryFromIntError;
|
|
||||||
use std::string::FromUtf8Error;
|
|
||||||
use tracing::{debug, error};
|
use tracing::{debug, error};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
|
|
||||||
|
|
@ -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::{
|
use crate::{
|
||||||
connection::ConnectionType,
|
connection::ConnectionType,
|
||||||
db::{Camera, CameraId, Db, DbHolder, DispatcherId, Plate},
|
db::{Camera, CameraId, Db, DbHolder, DispatcherId, Plate},
|
||||||
|
|
@ -5,13 +14,6 @@ use crate::{
|
||||||
Connection, Shutdown,
|
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 {
|
struct Listener {
|
||||||
listener: TcpListener,
|
listener: TcpListener,
|
||||||
db_holder: DbHolder,
|
db_holder: DbHolder,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue