-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from jabibamman/develop
Feat - server minimal and tests
- Loading branch information
Showing
19 changed files
with
430 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod operation; | ||
pub mod parser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::parser::CliArgs; | ||
|
||
/// Parses `CliArgs` to create a formatted address string. | ||
/// | ||
/// This function takes an instance of `CliArgs` and returns a string representing an address. | ||
/// It supports both client and server arguments. The address format is "hostname:port". | ||
/// | ||
/// ## Parameters | ||
/// - `cli_args`: An instance of `CliArgs`, which can be either `CliClientArgs` or `CliServerArgs`. | ||
/// | ||
/// ## Returns | ||
/// A `String` that represents the address in the format "hostname:port". | ||
/// | ||
/// ## Examples | ||
/// Assuming `CliArgs::Client` variant is used with hostname "192.168.1.0" and port 8787: | ||
/// ```rust | ||
/// use cli::{parser::{CliClientArgs, Parser, CliArgs}, operation::parse_to_address}; | ||
/// | ||
/// let client_args = CliArgs::Client(CliClientArgs { | ||
/// hostname: "192.168.1.0".to_string(), | ||
/// port: 8787, | ||
/// worker_name: "worker".to_string(), | ||
/// }); | ||
/// | ||
/// let address = parse_to_address(client_args); | ||
/// assert_eq!(address, "192.168.1.0:8787"); | ||
/// ``` | ||
/// Similarly, you can create an instance of `CliArgs::Server` and pass it to this function. | ||
pub fn parse_to_address(cli_args: CliArgs) -> String { | ||
match cli_args { | ||
CliArgs::Client(args) => format!("{}:{}", args.hostname, args.port), | ||
CliArgs::Server(args) => format!("{}:{}", args.hostname, args.port), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod operation_tests { | ||
use super::*; | ||
use crate::parser::{CliArgs, CliClientArgs, CliServerArgs}; | ||
|
||
pub fn initialize() -> CliServerArgs { | ||
CliServerArgs { | ||
hostname: "127.0.0.1".to_string(), | ||
port: 8787, | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_parse_client_to_address() { | ||
let args = initialize(); | ||
let client_args = CliArgs::Client(CliClientArgs { | ||
hostname: args.hostname, | ||
port: args.port, | ||
worker_name: "worker".to_string(), | ||
}); | ||
|
||
let address = parse_to_address(client_args); | ||
assert_eq!(address, "127.0.0.1:8787"); | ||
} | ||
|
||
#[test] | ||
fn test_parse_server_to_address() { | ||
let args = initialize(); | ||
let server_args = CliArgs::Server(args); | ||
|
||
let address = parse_to_address(server_args); | ||
assert_eq!(address, "127.0.0.1:8787"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,74 @@ | ||
pub use clap::Parser; | ||
|
||
/// # Command line arguments for the CLI | ||
/// Represents command line arguments for a client in a CLI application. | ||
/// | ||
/// > This struct is used to parse the command line arguments | ||
/// This struct is used to parse and store command line arguments specific to the client part of an application. | ||
/// It leverages the `clap` crate for argument parsing. | ||
/// | ||
/// ## Example | ||
/// ## Fields | ||
/// - `hostname`: A string specifying the hostname. Defaults to "localhost". | ||
/// - `port`: A 16-bit unsigned integer specifying the port number. Defaults to 8787. | ||
/// - `worker_name`: A string specifying the name of the worker. Defaults to "worker". | ||
/// | ||
/// ## Example | ||
/// Command line usage might look like this: | ||
/// ```sh | ||
/// worker -H 192.168.1.0 -P 3000 -N my_group_name | ||
/// worker -H 192.168.1.0 -P 8787 -N my_group_name | ||
/// ``` | ||
#[derive(Parser, Debug)] | ||
pub struct CliArgs { | ||
pub struct CliClientArgs { | ||
/// The hostname of the client. | ||
/// Default: "localhost" | ||
#[clap(short = 'H', long = "hostname", default_value = "localhost")] | ||
pub hostname: String, | ||
|
||
/// The port number to connect on. | ||
/// Default: 8787 | ||
#[clap(short = 'P', long = "port", default_value = "8787")] | ||
pub port: u16, | ||
|
||
/// The name of the worker. | ||
/// Default: "worker" | ||
#[clap(short = 'N', long = "name", default_value = "worker")] | ||
pub worker_name: String, | ||
} | ||
|
||
/// Represents command line arguments for a server in a CLI application. | ||
/// | ||
/// Similar to `CliClientArgs`, this struct is for parsing server-specific command line arguments. | ||
/// It uses the `clap` crate for parsing. | ||
/// | ||
/// ## Fields | ||
/// - `hostname`: A string specifying the hostname. Defaults to "localhost". | ||
/// - `port`: A 16-bit unsigned integer specifying the port number. Defaults to 8787. | ||
/// | ||
/// ## Example | ||
/// Command line usage for the server might be: | ||
/// ```sh | ||
/// server -H 192.168.1.0 -P 8787 | ||
/// ``` | ||
#[derive(Parser, Debug)] | ||
pub struct CliServerArgs { | ||
/// The hostname of the server. | ||
/// Default: "localhost" | ||
#[clap(short = 'H', long = "hostname", default_value = "localhost")] | ||
pub hostname: String, | ||
|
||
/// The port number the server listens on. | ||
/// Default: 8787 | ||
#[clap(short = 'P', long = "port", default_value = "8787")] | ||
pub port: u16, | ||
} | ||
|
||
/// An enumeration representing the possible types of command line arguments. | ||
/// | ||
/// This enum helps in differentiating between client and server command line arguments. | ||
/// It is a common practice in CLI applications to have different sets of arguments for different modes (client/server). | ||
/// | ||
/// ## Variants | ||
/// - `Client(CliClientArgs)`: Command line arguments specific to the client. | ||
/// - `Server(CliServerArgs)`: Command line arguments specific to the server. | ||
pub enum CliArgs { | ||
Client(CliClientArgs), | ||
Server(CliServerArgs), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,43 @@ | ||
use std::{io::Read, net::TcpStream}; | ||
|
||
/// Handles a client TCP stream. | ||
/// | ||
/// This function is responsible for managing a single client TCP stream. It performs the following actions: | ||
/// - Retrieves and prints the local address of the stream. | ||
/// - Reads data from the stream into a buffer and prints the received message. | ||
/// - Handles potential errors in both steps and prints relevant error messages. | ||
/// | ||
/// ## Parameters | ||
/// - `stream`: A mutable reference to a `TcpStream`. This stream represents the connection with the client. | ||
/// | ||
/// ## Panics | ||
/// This function doesn't explicitly panic, but operations on `stream` (like `read`) might panic if they encounter irrecoverable errors. | ||
/// | ||
/// ## Example Usage | ||
/// Note: This example is a conceptual representation and may not work as-is. | ||
/// ```no_run | ||
/// use std::net::{TcpListener, TcpStream}; | ||
/// use server::handler::handle_client; | ||
/// | ||
/// let listener = TcpListener::bind("127.0.0.1:8080").unwrap(); | ||
/// for stream in listener.incoming() { | ||
/// if let Ok(stream) = stream { | ||
/// handle_client(stream); | ||
/// } | ||
/// } | ||
/// ``` | ||
pub fn handle_client(mut stream: TcpStream) { | ||
match stream.local_addr() { | ||
Ok(addr) => println!("[SERVER] Connection established {}", addr), | ||
Err(e) => println!("[SERVER] Failed to get local address: {}", e), | ||
} | ||
|
||
let mut buffer = [0; 1024]; | ||
match stream.read(&mut buffer) { | ||
Ok(_) => println!( | ||
"[SERVER] Message received: {}", | ||
String::from_utf8_lossy(&buffer[..]) | ||
), | ||
Err(e) => println!("[SERVER] Error reading from stream: {}", e), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use cli::{ | ||
operation::parse_to_address, | ||
parser::{CliArgs, CliServerArgs, Parser}, | ||
}; | ||
use server::services::server_runner::run_server; | ||
|
||
fn main() -> std::io::Result<()> { | ||
let cli_args: CliArgs = CliArgs::Server(CliServerArgs::parse()); | ||
let address = parse_to_address(cli_args); | ||
match run_server(address.as_str()) { | ||
Ok(_) => println!("[SERVER] Server stopped."), | ||
Err(e) => println!("[SERVER] Server stopped with error: {}", e), | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod connect; | ||
pub mod reader; | ||
pub mod server_runner; | ||
pub mod write; |
Oops, something went wrong.