diff --git a/configs/config.txt b/configs/config.txt deleted file mode 100644 index 32c2d8b..0000000 --- a/configs/config.txt +++ /dev/null @@ -1,2 +0,0 @@ -server_address:127.0.0.1 -port:2444 \ No newline at end of file diff --git a/src/client.rs b/src/client.rs index 58fb579..ea3c0f2 100644 --- a/src/client.rs +++ b/src/client.rs @@ -28,26 +28,22 @@ pub async fn start(config: Config, debug: bool) { } } -pub async fn connect(config: &Config, debug: bool) -> Option<(WebSocketSender, WebSocketReceiver)> { - let ws_connection = - match connect_async(format!("ws://{}:{}", config.server_address, config.port)).await { - Ok(ws_connection) => ws_connection, - Err(err_val) => { - if debug { - eprintln!("Error: WebSocket Connection | {}", err_val); - } - return None; +async fn connect(config: &Config, debug: bool) -> Option<(WebSocketSender, WebSocketReceiver)> { + let ws_connection = match connect_async(format!("ws://{}:{}", config.ip, config.port)).await { + Ok(ws_connection) => ws_connection, + Err(err_val) => { + if debug { + eprintln!("Error: WebSocket Connection | {}", err_val); } - }; + return None; + } + }; let (ws_sender, ws_receiver) = ws_connection.0.split(); Some((ws_sender, ws_receiver)) } -pub async fn serve( - (ws_sender, mut ws_receiver): (WebSocketSender, WebSocketReceiver), - debug: bool, -) { +async fn serve((ws_sender, mut ws_receiver): (WebSocketSender, WebSocketReceiver), debug: bool) { let ws_sender = Arc::new(Mutex::new(ws_sender)); while let Some(message) = receive(&mut ws_receiver, debug).await { match serde_json::from_str::(&message[..]) { @@ -68,7 +64,7 @@ pub async fn serve( } } -pub async fn execute(payload: Payload, debug: bool) -> Option { +async fn execute(payload: Payload, debug: bool) -> Option { if debug { println!("{:#?}", payload); } @@ -87,7 +83,7 @@ pub async fn execute(payload: Payload, debug: bool) -> Option { } } -pub async fn receive(ws_receiver: &mut WebSocketReceiver, debug: bool) -> Option { +async fn receive(ws_receiver: &mut WebSocketReceiver, debug: bool) -> Option { match ws_receiver.next().await { Some(message) => match message { Ok(message) => { @@ -116,7 +112,7 @@ pub async fn receive(ws_receiver: &mut WebSocketReceiver, debug: bool) -> Option } } -pub async fn send( +async fn send( output: Option, payload: Payload, ws_sender: Arc>, diff --git a/src/lib.rs b/src/lib.rs index 03f03e0..90fb3f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,18 +6,19 @@ pub mod client; pub mod server; pub mod utils; +#[derive(Debug)] pub enum Runner { Server, Client, } - +#[derive(Debug)] pub enum RunnerMode { State(Runner, bool), } #[derive(Debug, Clone)] pub struct Config { - pub server_address: IpAddr, + pub ip: IpAddr, pub port: u16, } diff --git a/src/main.rs b/src/main.rs index 2825486..0626937 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,14 @@ -use rust_remote::{ - utils::{read_config, take_args}, - Runner, RunnerMode, -}; +use rust_remote::{utils::take_args, Runner, RunnerMode}; #[tokio::main] async fn main() { println!("Hello, world!"); - let config = match read_config() { - Some(config) => config, - None => { - eprintln!("Error: Read Config"); - return; - } - }; + let args = take_args(); + println!("{:#?}", args); - match take_args() { - Some(runner_mode) => match runner_mode { + match args { + Some((runner_mode, config)) => match runner_mode { RunnerMode::State(Runner::Server, false) => { rust_remote::server::start(config, false).await } diff --git a/src/server.rs b/src/server.rs index 6b457fe..ab6aa4d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -46,8 +46,8 @@ pub async fn start(config: Config, debug: bool) { } } } -pub async fn setup(config: Config, debug: bool) -> Option { - match TcpListener::bind(format!("{}:{}", config.server_address, config.port)).await { +async fn setup(config: Config, debug: bool) -> Option { + match TcpListener::bind(format!("{}:{}", config.ip, config.port)).await { Ok(listener) => Some(listener), Err(err_val) => { if debug { @@ -58,7 +58,7 @@ pub async fn setup(config: Config, debug: bool) -> Option { } } -pub async fn establish_connection( +async fn establish_connection( listener: &TcpListener, debug: bool, ) -> Option<(WebSocketSender, WebSocketReceiver)> { @@ -81,7 +81,7 @@ pub async fn establish_connection( } } -pub async fn payload_from_input(debug: bool) -> Option { +async fn payload_from_input(debug: bool) -> Option { println!("User"); // let user = match get_input() { // Some(input) => input, @@ -116,7 +116,7 @@ pub async fn payload_from_input(debug: bool) -> Option { } } -pub fn get_input(debug: bool) -> Option { +fn get_input(debug: bool) -> Option { let mut payload_input: String = String::new(); match stdin().read_line(&mut payload_input) { Ok(_) => Some(payload_input.trim().to_string()), @@ -129,7 +129,7 @@ pub fn get_input(debug: bool) -> Option { } } -pub async fn receive(ws_receiver: Arc>, debug: bool) -> Option { +async fn receive(ws_receiver: Arc>, debug: bool) -> Option { match ws_receiver.lock().await.next().await { Some(message) => match message { Ok(message) => { @@ -158,7 +158,7 @@ pub async fn receive(ws_receiver: Arc>, debug: bool) -> } } -pub async fn send(payload: Payload, ws_sender: Arc>, debug: bool) -> bool { +async fn send(payload: Payload, ws_sender: Arc>, debug: bool) -> bool { let payload = serde_json::json!(payload); let result = ws_sender .lock() diff --git a/src/utils.rs b/src/utils.rs index 04653b3..2989196 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,57 +1,58 @@ -use std::{env, fs::File, io::Read}; +use std::{env, net::IpAddr}; use crate::{Config, Runner, RunnerMode}; -pub fn take_args() -> Option { +pub fn take_args() -> Option<(RunnerMode, Config)> { let args: Vec = env::args().collect(); - if args.len() > 1 { - let runner = match &args[1][..] { - "--server" => Runner::Server, - "--client" => Runner::Client, - _ => return None, - }; - let debug = if args.len() > 2 { - match &args[2][..] { - "--debug" => true, - _ => return None, + let mut runner = Runner::Server; + let mut debug = false; + let mut ip = "127.0.0.1".to_string(); + let mut port = "3444".to_string(); + for (i, arg) in args.iter().enumerate() { + match arg.as_str() { + "--server" | "-sv" => runner = Runner::Server, + "--client" | "-cl" => runner = Runner::Client, + "--debug" | "-d" => debug = true, + "--ip" | "i" => ip = args[i + 1].clone(), + "--port" | "p" => port = args[i + 1].clone(), + "--help" | "-h" => { + show_help(); + std::process::exit(0); } - } else { - false - }; - Some(RunnerMode::State(runner, debug)) - } else { - None + _ => continue, + } } + let ip = match ip.parse::() { + Ok(ip) => ip, + Err(err_val) => { + eprintln!("Error: IP Parse | {}", err_val); + return None; + } + }; + + let port = match port.parse::() { + Ok(port) => port, + Err(err_val) => { + eprintln!("Error: Port Parse | {}", err_val); + return None; + } + }; + + let config = Config { ip, port }; + + let runner_mode = RunnerMode::State(runner, debug); + Some((runner_mode, config)) } -pub fn read_config() -> Option { - let mut config_file = match File::open("configs/config.txt") { - Ok(config_file) => config_file, - Err(_) => return None, - }; - let mut configs = String::new(); - match config_file.read_to_string(&mut configs) { - Ok(_) => { - let configs: Vec = configs.split('\n').map(|x| x.to_string()).collect(); - let server_address = match configs[0].split(':').last() { - Some(server_address_unchecked) => match server_address_unchecked.parse() { - Ok(server_address) => server_address, - Err(_) => return None, - }, - None => return None, - }; - let port = match configs[1].split(':').last() { - Some(port_unchecked) => match port_unchecked.parse() { - Ok(port) => port, - Err(_) => return None, - }, - None => return None, - }; - Some(Config { - server_address, - port, - }) - } - Err(_) => None, - } +fn show_help() { + println!("\n\n\n"); + println!(" Arguments | Details | Defaults"); + println!("----------------------------------------------------------------------"); + println!(" -i -> --ip | Specifies IP Address | 127.0.0.1"); + println!(" -p -> --port | Specifies Port Address | 3444"); + println!(" -sv -> --server | Starts as a Server | True"); + println!(" -cl -> --client | Starts as a Client | False"); + println!(" -d -> --debug | Starts in Debug Mode | False"); + println!(" -h -> --help | Shows Help | False"); + println!("\n\n\n"); }