Skip to content

Commit

Permalink
Merge pull request #34 from jabibamman/feat/cli
Browse files Browse the repository at this point in the history
Feat - first cli basic implementation
  • Loading branch information
jabibamman authored Nov 16, 2023
2 parents e1832e4 + 19d1100 commit 0a967a6
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 36 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ members = [
"shared",
"server",
"complex",
"cli"
]
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ To build and the complex library (used by other components):
cargo build -p complex
```

## Running the worker with CLI

To run the worker with CLI, use the following command:

You can read the CLI rustdoc documentation for more information on the CLI arguments.

```bash
cargo run -p client -- -h
```

## Documentation

To generate documentation for all packages without including dependencies (recommended):
Expand Down
10 changes: 10 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "cli"
version = "0.1.0"
edition = "2021"
authors = ["James <jabib@myges.fr>", "Ronan <rkielt@myges.fr>", "Charles <ccretois@myges.fr>", "Samira <sseddar@myges.fr>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.4.6", features = ["derive"] }
1 change: 1 addition & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod parser;
22 changes: 22 additions & 0 deletions cli/src/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub use clap::Parser;

/// # Command line arguments for the CLI
///
/// > This struct is used to parse the command line arguments
///
/// ## Example
///
/// ```sh
/// worker -H 192.168.1.0 -P 3000 -N my_group_name
/// ```
#[derive(Parser, Debug)]
pub struct CliArgs {
#[clap(short = 'H', long = "hostname", default_value = "localhost")]
pub hostname: String,

#[clap(short = 'P', long = "port", default_value = "8787")]
pub port: u16,

#[clap(short = 'N', long = "name", default_value = "worker")]
pub worker_name: String,
}
1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ image = { version = "0.24.7", features = [] }
complex = { path = "../complex" }
shared = { path = "../shared" }
server = { path = "../server" }
cli = { path = "../cli" }
32 changes: 0 additions & 32 deletions client/src/client.rs

This file was deleted.

12 changes: 11 additions & 1 deletion client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
mod image;
mod julia;

use std::io;

use crate::image::open_image;
use crate::julia::generate_julia_set;

use cli::parser::{CliArgs, Parser};
use server::services::{connect::connect, reader::read_message};
use shared::types::filesystem::FileExtension;
use shared::types::fractal_descriptor::FractalType::Julia;
use shared::types::fractal_descriptor::{FractalDescriptor, JuliaDescriptor};
Expand All @@ -14,7 +18,11 @@ use shared::types::u8data::U8Data;
use shared::types::{complex::Complex, resolution::Resolution};
use shared::utils::filesystem::{get_dir_path_buf, get_extension_str, get_file_path};

fn main() {
fn main() -> io::Result<()> {
let args: CliArgs = CliArgs::parse();
let stream = connect(format!("{}:{}", args.hostname, args.port).as_str())?;
let message = read_message(stream);
println!("{}", message);
let img_path = get_file_path(
"julia",
get_dir_path_buf(),
Expand Down Expand Up @@ -54,4 +62,6 @@ fn main() {
}

open_image(img_path.as_str());

Ok(())
}
3 changes: 2 additions & 1 deletion client/src/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod client;
pub mod image;
pub mod julia;
pub mod networking;
1 change: 1 addition & 0 deletions client/src/networking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 2 additions & 2 deletions server/src/services/connect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::Result;
use std::io;
use std::net::TcpStream;

/// Connect to a server
Expand All @@ -10,7 +10,7 @@ use std::net::TcpStream;
/// # Return
///
/// * `Result<TcpStream, std::io::Error>` - TcpStream
pub fn connect(address: &str) -> Result<TcpStream> {
pub fn connect(address: &str) -> io::Result<TcpStream> {
let stream = TcpStream::connect(address)?;

Ok(stream)
Expand Down
1 change: 1 addition & 0 deletions server/src/services/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod connect;
pub mod reader;
pub mod write;
45 changes: 45 additions & 0 deletions server/src/services/reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::{io::Read, net::TcpStream};

/// Read a message from a TCP stream.
///
/// This function reads data from the given TCP stream up to 1024 bytes.
/// It assumes that the message is UTF-8 encoded. If the message contains
/// non-UTF-8 bytes, they will be replaced by U+FFFD REPLACEMENT CHARACTER.
/// The function also trims null characters from the message.
///
/// # Arguments
///
/// * `stream` - A mutable reference to the TCP stream from which to read the message.
///
/// # Returns
///
/// Returns a `String` containing the message read from the stream.
///
/// # Examples
///
/// ```no_run
/// use std::net::{TcpListener, TcpStream};
/// use server::services::reader::read_message;
///
/// let listener = TcpListener::bind("127.0.0.1:0").unwrap();
/// let address = listener.local_addr().unwrap();
/// let mut stream = TcpStream::connect(address).unwrap();
///
/// let message = read_message(stream);
/// println!("Received message: {}", message);
/// ```
///
/// # Panics
///
/// Panics if the reading from the stream fails or if the buffer cannot be
/// converted to a UTF-8 string.
///
pub fn read_message(mut stream: TcpStream) -> String {
let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap();

let message = String::from_utf8_lossy(&buffer[..]);
let message = message.trim_matches(char::from(0)).to_string();

message
}
11 changes: 11 additions & 0 deletions server/src/services/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ use std::net::TcpStream;
/// # Return
///
/// * `Result<TcpStream, std::io::Error>` - TcpStream
///
/// # Example
///
/// ```no_run
/// use std::net::TcpStream;
/// use server::services::write::write;
///
/// let stream = TcpStream::connect("localhost:8787").unwrap();
/// let stream = write(stream, "Hello world!").unwrap();
/// ```
///
pub fn write(mut stream: TcpStream, message: &str) -> Result<TcpStream> {
stream.write(format!("${}", message).as_bytes())?;

Expand Down

0 comments on commit 0a967a6

Please sign in to comment.