mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-03-24 21:50:57 +03:00
cleanup
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashSet,
|
collections::HashSet,
|
||||||
error::Error,
|
|
||||||
fmt::Display,
|
fmt::Display,
|
||||||
net::{IpAddr, SocketAddr},
|
net::{IpAddr, SocketAddr},
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
@@ -8,24 +7,20 @@ use std::{
|
|||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use slab::Slab;
|
use slab::Slab;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::config::DEFAULT_PORT;
|
use crate::config::DEFAULT_PORT;
|
||||||
use input_capture;
|
use input_capture;
|
||||||
|
|
||||||
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy, Serialize, Deserialize)]
|
#[derive(Debug, Default, Eq, Hash, PartialEq, Clone, Copy, Serialize, Deserialize)]
|
||||||
pub enum Position {
|
pub enum Position {
|
||||||
|
#[default]
|
||||||
Left,
|
Left,
|
||||||
Right,
|
Right,
|
||||||
Top,
|
Top,
|
||||||
Bottom,
|
Bottom,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Position {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::Left
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Position> for input_capture::Position {
|
impl From<Position> for input_capture::Position {
|
||||||
fn from(position: Position) -> input_capture::Position {
|
fn from(position: Position) -> input_capture::Position {
|
||||||
match position {
|
match position {
|
||||||
@@ -37,19 +32,12 @@ impl From<Position> for input_capture::Position {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Error)]
|
||||||
|
#[error("not a valid position: {pos}")]
|
||||||
pub struct PositionParseError {
|
pub struct PositionParseError {
|
||||||
string: String,
|
pos: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for PositionParseError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "not a valid position: {}", self.string)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Error for PositionParseError {}
|
|
||||||
|
|
||||||
impl FromStr for Position {
|
impl FromStr for Position {
|
||||||
type Err = PositionParseError;
|
type Err = PositionParseError;
|
||||||
|
|
||||||
@@ -59,7 +47,7 @@ impl FromStr for Position {
|
|||||||
"right" => Ok(Self::Right),
|
"right" => Ok(Self::Right),
|
||||||
"top" => Ok(Self::Top),
|
"top" => Ok(Self::Top),
|
||||||
"bottom" => Ok(Self::Bottom),
|
"bottom" => Ok(Self::Bottom),
|
||||||
_ => Err(PositionParseError { string: s.into() }),
|
_ => Err(PositionParseError { pos: s.into() }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -141,27 +129,15 @@ pub struct ClientState {
|
|||||||
pub resolving: bool,
|
pub resolving: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
pub struct ClientManager {
|
pub struct ClientManager {
|
||||||
clients: Slab<(ClientConfig, ClientState)>,
|
clients: Slab<(ClientConfig, ClientState)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ClientManager {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ClientManager {
|
impl ClientManager {
|
||||||
pub fn new() -> Self {
|
|
||||||
let clients = Slab::new();
|
|
||||||
Self { clients }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// add a new client to this manager
|
/// add a new client to this manager
|
||||||
pub fn add_client(&mut self) -> ClientHandle {
|
pub fn add_client(&mut self) -> ClientHandle {
|
||||||
let client_config = Default::default();
|
self.clients.insert(Default::default()) as ClientHandle
|
||||||
let client_state = Default::default();
|
|
||||||
self.clients.insert((client_config, client_state)) as ClientHandle
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// find a client by its address
|
/// find a client by its address
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ pub struct Server {
|
|||||||
impl Server {
|
impl Server {
|
||||||
pub fn new(config: &Config) -> Self {
|
pub fn new(config: &Config) -> Self {
|
||||||
let active_client = Rc::new(Cell::new(None));
|
let active_client = Rc::new(Cell::new(None));
|
||||||
let client_manager = Rc::new(RefCell::new(ClientManager::new()));
|
let client_manager = Rc::new(RefCell::new(ClientManager::default()));
|
||||||
let state = Rc::new(Cell::new(State::Receiving));
|
let state = Rc::new(Cell::new(State::Receiving));
|
||||||
let port = Rc::new(Cell::new(config.port));
|
let port = Rc::new(Cell::new(config.port));
|
||||||
for config_client in config.get_clients() {
|
for config_client in config.get_clients() {
|
||||||
@@ -104,7 +104,7 @@ impl Server {
|
|||||||
let notify_emulation = Arc::new(Notify::new()); /* notify emultation restart */
|
let notify_emulation = Arc::new(Notify::new()); /* notify emultation restart */
|
||||||
|
|
||||||
// udp task
|
// udp task
|
||||||
let (mut network, udp_send, udp_recv, port_tx) = network_task::new(
|
let (network, udp_send, udp_recv, port_tx) = network_task::new(
|
||||||
self.clone(),
|
self.clone(),
|
||||||
frontend_tx.clone(),
|
frontend_tx.clone(),
|
||||||
cancellation_token.clone(),
|
cancellation_token.clone(),
|
||||||
@@ -112,7 +112,7 @@ impl Server {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// input capture
|
// input capture
|
||||||
let (mut capture, capture_channel) = capture_task::new(
|
let (capture, capture_channel) = capture_task::new(
|
||||||
capture_backend,
|
capture_backend,
|
||||||
self.clone(),
|
self.clone(),
|
||||||
udp_send.clone(),
|
udp_send.clone(),
|
||||||
@@ -124,7 +124,7 @@ impl Server {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// input emulation
|
// input emulation
|
||||||
let (mut emulation, emulate_channel) = emulation_task::new(
|
let (emulation, emulate_channel) = emulation_task::new(
|
||||||
emulation_backend,
|
emulation_backend,
|
||||||
self.clone(),
|
self.clone(),
|
||||||
udp_recv,
|
udp_recv,
|
||||||
@@ -138,7 +138,7 @@ impl Server {
|
|||||||
|
|
||||||
// create dns resolver
|
// create dns resolver
|
||||||
let resolver = dns::DnsResolver::new().await?;
|
let resolver = dns::DnsResolver::new().await?;
|
||||||
let (mut resolver, dns_req) = resolver_task::new(
|
let (resolver, dns_req) = resolver_task::new(
|
||||||
resolver,
|
resolver,
|
||||||
self.clone(),
|
self.clone(),
|
||||||
frontend_tx,
|
frontend_tx,
|
||||||
@@ -146,7 +146,7 @@ impl Server {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// frontend listener
|
// frontend listener
|
||||||
let (mut frontend, frontend_tx) = frontend_task::new(
|
let (frontend, frontend_tx) = frontend_task::new(
|
||||||
frontend,
|
frontend,
|
||||||
frontend_rx,
|
frontend_rx,
|
||||||
self.clone(),
|
self.clone(),
|
||||||
@@ -160,7 +160,7 @@ impl Server {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// task that pings clients to see if they are responding
|
// task that pings clients to see if they are responding
|
||||||
let mut ping = ping_task::new(
|
let ping = ping_task::new(
|
||||||
self.clone(),
|
self.clone(),
|
||||||
udp_send.clone(),
|
udp_send.clone(),
|
||||||
emulate_channel.clone(),
|
emulate_channel.clone(),
|
||||||
@@ -189,17 +189,10 @@ impl Server {
|
|||||||
let _ = dns_req.send(DnsRequest { hostname, handle }).await;
|
let _ = dns_req.send(DnsRequest { hostname, handle }).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log::info!("running service");
|
|
||||||
|
|
||||||
tokio::select! {
|
log::info!("running service");
|
||||||
_ = signal::ctrl_c() => log::info!("terminating service"),
|
signal::ctrl_c().await.expect("failed to listen for CTRL+C");
|
||||||
_ = &mut capture => { }
|
log::info!("terminating service");
|
||||||
_ = &mut emulation => { }
|
|
||||||
_ = &mut frontend => { }
|
|
||||||
_ = &mut resolver => { }
|
|
||||||
_ = &mut network => { }
|
|
||||||
_ = &mut ping => { }
|
|
||||||
}
|
|
||||||
|
|
||||||
cancellation_token.cancel();
|
cancellation_token.cancel();
|
||||||
let _ = join!(capture, emulation, frontend, network, resolver, ping);
|
let _ = join!(capture, emulation, frontend, network, resolver, ping);
|
||||||
|
|||||||
Reference in New Issue
Block a user