2021-12-09 01:28:29 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2021-12-09 18:22:23 +00:00
|
|
|
pub const LOOKAROUND_VERSION: &str = env! ("CARGO_PKG_VERSION");
|
2021-12-09 17:01:29 +00:00
|
|
|
|
2021-12-09 18:15:03 +00:00
|
|
|
pub fn find_project_dirs () -> Option <ProjectDirs> {
|
|
|
|
ProjectDirs::from ("", "ReactorScram", "LookAround")
|
|
|
|
}
|
|
|
|
|
2021-12-09 01:28:29 +00:00
|
|
|
#[derive (Debug, thiserror::Error)]
|
|
|
|
pub enum AppError {
|
|
|
|
#[error (transparent)]
|
|
|
|
AddrParse (#[from] std::net::AddrParseError),
|
|
|
|
#[error (transparent)]
|
|
|
|
CliArgs (#[from] CliArgError),
|
2021-12-09 16:46:55 +00:00
|
|
|
#[error ("Operation timed out")]
|
|
|
|
Elapsed (#[from] tokio::time::error::Elapsed),
|
2021-12-09 01:28:29 +00:00
|
|
|
#[error (transparent)]
|
|
|
|
Io (#[from] std::io::Error),
|
|
|
|
#[error (transparent)]
|
|
|
|
Ip (#[from] crate::ip::IpError),
|
|
|
|
#[error (transparent)]
|
2021-12-09 03:11:28 +00:00
|
|
|
Join (#[from] tokio::task::JoinError),
|
|
|
|
#[error (transparent)]
|
2021-12-09 01:28:29 +00:00
|
|
|
MacAddr (#[from] mac_address::MacAddressError),
|
|
|
|
#[error (transparent)]
|
|
|
|
Message (#[from] crate::message::MessageError),
|
|
|
|
#[error (transparent)]
|
2021-12-09 15:34:42 +00:00
|
|
|
ParseInt (#[from] std::num::ParseIntError),
|
|
|
|
#[error (transparent)]
|
2021-12-09 01:28:29 +00:00
|
|
|
Tlv (#[from] crate::tlv::TlvError),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Debug, thiserror::Error)]
|
|
|
|
pub enum CliArgError {
|
|
|
|
#[error ("Missing value for argument `{0}`")]
|
|
|
|
MissingArgumentValue (String),
|
2021-12-09 16:46:55 +00:00
|
|
|
#[error ("Missing required argument <{0}>")]
|
|
|
|
MissingRequiredArg (String),
|
2021-12-09 01:28:29 +00:00
|
|
|
#[error ("First argument should be a subcommand")]
|
|
|
|
MissingSubcommand,
|
|
|
|
#[error ("Unknown subcommand `{0}`")]
|
|
|
|
UnknownSubcommand (String),
|
|
|
|
#[error ("Unrecognized argument `{0}`")]
|
|
|
|
UnrecognizedArgument (String),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn recv_msg_from (socket: &UdpSocket) -> Result <(Vec <Message>, SocketAddr), AppError>
|
|
|
|
{
|
|
|
|
let mut buf = vec! [0u8; PACKET_SIZE];
|
|
|
|
let (bytes_recved, remote_addr) = socket.recv_from (&mut buf).await?;
|
|
|
|
buf.truncate (bytes_recved);
|
|
|
|
let msgs = Message::from_slice2 (&buf)?;
|
|
|
|
|
|
|
|
Ok ((msgs, remote_addr))
|
|
|
|
}
|
|
|
|
|
2021-12-09 03:11:28 +00:00
|
|
|
#[derive (Clone)]
|
2021-12-09 01:28:29 +00:00
|
|
|
pub struct Params {
|
|
|
|
// Servers bind on this port, clients must send to the port
|
|
|
|
pub server_port: u16,
|
|
|
|
|
|
|
|
// Clients and servers will all join the same multicast addr
|
|
|
|
pub multicast_addr: Ipv4Addr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Params {
|
|
|
|
fn default () -> Self {
|
|
|
|
Self {
|
|
|
|
server_port: 9040,
|
|
|
|
multicast_addr: Ipv4Addr::new (225, 100, 99, 98),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|