diff --git a/cli/src/parser.rs b/cli/src/parser.rs index fbf03fa..2e7c40a 100644 --- a/cli/src/parser.rs +++ b/cli/src/parser.rs @@ -75,7 +75,7 @@ pub struct CliClientArgs { pub struct CliServerArgs { /// The hostname of the server. /// Default: "localhost" - #[clap( long = "hostname", default_value = "localhost")] + #[clap(long = "hostname", default_value = "localhost")] pub hostname: String, /// The port number the server listens on. diff --git a/client/src/fractal_generation.rs b/client/src/fractal_generation.rs index 7486a99..c4cd7c8 100644 --- a/client/src/fractal_generation.rs +++ b/client/src/fractal_generation.rs @@ -52,7 +52,7 @@ pub fn generate_fractal_set( let pixel_intensity = descriptor.compute_pixel_intensity(&complex_point, fragment_task.max_iteration); - + *pixel = Rgb(color(pixel_intensity)); pixel_matrice_intensity.push(pixel_intensity); @@ -226,5 +226,4 @@ mod julia_descriptor_tests { assert_eq!(img.dimensions(), (800, 600)); } } - } diff --git a/client/src/networking.rs b/client/src/networking.rs index 44c79c4..ab145b0 100644 --- a/client/src/networking.rs +++ b/client/src/networking.rs @@ -132,8 +132,9 @@ pub fn process_fragment_task( cli_args: &CliClientArgs, ) -> Result { let dir_path_buf = get_dir_path_buf()?; - - let img_path: String = get_file_path("julia", dir_path_buf, get_extension_str(FileExtension::PNG))?; + + let img_path: String = + get_file_path("julia", dir_path_buf, get_extension_str(FileExtension::PNG))?; let (img, pixel_data_bytes, pixel_intensity_matrice) = generate_fractal_set(task.clone())?; debug!("Pixel data bytes: {:?}", pixel_data_bytes); diff --git a/server/src/main.rs b/server/src/main.rs index 751fc2a..6f1f05d 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -10,7 +10,7 @@ fn main() -> Result<(), FractalError> { shared::logger::init_logger(cli_args.verbose, cli_args.debug)?; env::set_var("RESOLUTION_WIDTH", cli_args.width.to_string()); env::set_var("RESOLUTION_HEIGHT", cli_args.height.to_string()); - + let address = format!("{}:{}", cli_args.hostname, cli_args.port); match run_server(address.as_str()) { Ok(_) => info!("Server stopped successfully!"), diff --git a/server/src/messages/fragment_maker.rs b/server/src/messages/fragment_maker.rs index 6dec022..063fa2d 100644 --- a/server/src/messages/fragment_maker.rs +++ b/server/src/messages/fragment_maker.rs @@ -16,7 +16,10 @@ pub fn create_tasks() -> Result, Box> { let width = get_env_var_as_u16("RESOLUTION_WIDTH")?; let height = get_env_var_as_u16("RESOLUTION_HEIGHT")?; - let range = generate_range(Range::new(Point::new(-3.0, -3.0), Point::new(3.0, 3.0)), 2.0); + let range = generate_range( + Range::new(Point::new(-3.0, -3.0), Point::new(3.0, 3.0)), + 2.0, + ); let mut tasks = vec![]; for r in range { @@ -31,7 +34,10 @@ pub fn create_tasks() -> Result, Box> { }), }, max_iteration: 64, - resolution: Resolution { nx: width, ny: height }, + resolution: Resolution { + nx: width, + ny: height, + }, range: r, }; @@ -39,28 +45,24 @@ pub fn create_tasks() -> Result, Box> { } Ok(tasks) - -} - -pub fn process_result(_result: FragmentResult) { - - - } - +pub fn process_result(_result: FragmentResult) {} pub fn generate_range(full_image: Range, step: f64) -> Vec { let mut ranges = Vec::new(); - let y_step = step; - let x_step = step; + let y_step = step; + let x_step = step; let mut y = full_image.min.y; while y < full_image.max.y { let mut x = full_image.min.x; while x < full_image.max.x { let min = Point::new(x, y); - let max = Point::new((x + x_step).min(full_image.max.x), (y + y_step).min(full_image.max.y)); + let max = Point::new( + (x + x_step).min(full_image.max.x), + (y + y_step).min(full_image.max.y), + ); ranges.push(Range::new(min, max)); x += x_step; } @@ -68,4 +70,3 @@ pub fn generate_range(full_image: Range, step: f64) -> Vec { } ranges } - diff --git a/server/src/messages/handler.rs b/server/src/messages/handler.rs index 1153286..8068cbf 100644 --- a/server/src/messages/handler.rs +++ b/server/src/messages/handler.rs @@ -1,9 +1,19 @@ +use log::{debug, error, info}; +use shared::{ + types::{ + error::FractalError, filesystem::FileExtension, messages::Message, + pixel_intensity::PixelIntensity, + }, + utils::{ + filesystem::{get_dir_path_buf, get_extension_str, get_file_path}, + fragment_task_impl::FragmentTaskOperation, + image::image_from_pixel_intensity, + }, +}; use std::{ io::{self, Read}, net::TcpStream, }; -use log::{debug, error, info}; -use shared::{types::{error::FractalError, filesystem::FileExtension, messages::Message, pixel_intensity::PixelIntensity}, utils::{filesystem::{get_dir_path_buf, get_extension_str, get_file_path}, fragment_task_impl::FragmentTaskOperation, image::image_from_pixel_intensity}}; use super::serialization::deserialize_message; use crate::{messages::fragment_maker::create_tasks, services}; @@ -59,7 +69,6 @@ pub fn handle_client(mut stream: TcpStream) -> io::Result<()> { io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-8") })?; - debug!("Received JSON: {}", json_str); let data_str = match std::str::from_utf8(&buffer) { Ok(str) => str, @@ -74,7 +83,7 @@ pub fn handle_client(mut stream: TcpStream) -> io::Result<()> { let mut buffer = vec![0; total_size - json_size]; stream.read_exact(&mut buffer)?; debug!("Received data: {:?}", buffer); - pixel_intensity = PixelIntensity::vec_data_to_pixel_intensity_matrix(buffer); + pixel_intensity = PixelIntensity::vec_data_to_pixel_intensity_matrix(buffer); } debug!("Received data: {}", data_str); @@ -94,7 +103,6 @@ pub fn handle_client(mut stream: TcpStream) -> io::Result<()> { debug!("Task created: {:?}", task.clone()); - let serialized_task = match task.serialize() { Ok(serialized_task) => serialized_task, Err(e) => { @@ -117,44 +125,46 @@ pub fn handle_client(mut stream: TcpStream) -> io::Result<()> { todo!() } Ok(Message::FragmentResult(_result)) => { - //process_result(result); - - let img = match image_from_pixel_intensity(pixel_intensity) { - Ok(img) => img, - Err(e) => { - error!("Error creating image from pixel intensity: {:?}", e); - return Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-8")); - } - }; - - let dir_path_buf = match get_dir_path_buf() { - Ok(dir_path_buf) => dir_path_buf, - Err(e) => { - error!("Error getting directory path: {:?}", e); - return Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-8")); - } - }; - - let img_path: String = match get_file_path("test-23_02_23", dir_path_buf, get_extension_str(FileExtension::PNG)) { - Ok(img_path) => img_path, - Err(e) => { - error!("Error getting file path: {:?}", e); - return Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-8")); - } - }; - - - match img.save(img_path.clone()).map_err(FractalError::Image) { - Ok(_) => { - info!("Image saved successfully"); - debug!("Image path {}", img_path); - } - Err(e) => { - error!("Error saving image: {:?}", e); - return Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-8")); - } - } - + //process_result(result); + + let img = match image_from_pixel_intensity(pixel_intensity) { + Ok(img) => img, + Err(e) => { + error!("Error creating image from pixel intensity: {:?}", e); + return Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-8")); + } + }; + + let dir_path_buf = match get_dir_path_buf() { + Ok(dir_path_buf) => dir_path_buf, + Err(e) => { + error!("Error getting directory path: {:?}", e); + return Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-8")); + } + }; + + let img_path: String = match get_file_path( + "test-23_02_23", + dir_path_buf, + get_extension_str(FileExtension::PNG), + ) { + Ok(img_path) => img_path, + Err(e) => { + error!("Error getting file path: {:?}", e); + return Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-8")); + } + }; + + match img.save(img_path.clone()).map_err(FractalError::Image) { + Ok(_) => { + info!("Image saved successfully"); + debug!("Image path {}", img_path); + } + Err(e) => { + error!("Error saving image: {:?}", e); + return Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-8")); + } + } } Err(e) => { error!("Error deserializing request: {:?}", e); @@ -162,4 +172,4 @@ pub fn handle_client(mut stream: TcpStream) -> io::Result<()> { } Ok(()) -} \ No newline at end of file +} diff --git a/server/src/messages/serialization.rs b/server/src/messages/serialization.rs index d833fbe..c2a624a 100644 --- a/server/src/messages/serialization.rs +++ b/server/src/messages/serialization.rs @@ -3,7 +3,10 @@ use serde::de::Error as SerdeError; use shared::{ types::messages::{FragmentRequest, FragmentResult, Message}, - utils::{fragment_request_impl::FragmentRequestOperation, fragment_result_impl::FragmentResultOperation}, + utils::{ + fragment_request_impl::FragmentRequestOperation, + fragment_result_impl::FragmentResultOperation, + }, }; /// Deserializes a JSON string into a `Message` enum variant. diff --git a/shared/src/types/pixel_intensity.rs b/shared/src/types/pixel_intensity.rs index 540acd3..e5f4db7 100644 --- a/shared/src/types/pixel_intensity.rs +++ b/shared/src/types/pixel_intensity.rs @@ -9,4 +9,4 @@ pub struct PixelIntensity { pub zn: f32, pub count: f32, -} \ No newline at end of file +} diff --git a/shared/src/types/point.rs b/shared/src/types/point.rs index f247757..33ec759 100644 --- a/shared/src/types/point.rs +++ b/shared/src/types/point.rs @@ -25,5 +25,4 @@ impl Point { pub fn new(x: f64, y: f64) -> Point { Point { x, y } } - } diff --git a/shared/src/types/range.rs b/shared/src/types/range.rs index d62721a..8786ea1 100644 --- a/shared/src/types/range.rs +++ b/shared/src/types/range.rs @@ -28,4 +28,3 @@ impl Range { Range { min, max } } } - diff --git a/shared/src/types/resolution.rs b/shared/src/types/resolution.rs index 58456bd..6de05cb 100644 --- a/shared/src/types/resolution.rs +++ b/shared/src/types/resolution.rs @@ -25,5 +25,4 @@ impl Resolution { pub fn new(nx: u16, ny: u16) -> Resolution { Resolution { nx, ny } } - } diff --git a/shared/src/utils/colors_utils.rs b/shared/src/utils/colors_utils.rs index 0551bdd..80694ed 100644 --- a/shared/src/utils/colors_utils.rs +++ b/shared/src/utils/colors_utils.rs @@ -1,5 +1,7 @@ -use crate::{types::{color::{HSL, RGB}, pixel_intensity::PixelIntensity}} -; +use crate::types::{ + color::{HSL, RGB}, + pixel_intensity::PixelIntensity, +}; ///Generates a color based on the provided pixel intensity. /// # Arguments @@ -74,4 +76,4 @@ fn test_color() { assert!(test2.eq("u8")); assert_eq!(result, [63, 191, 191]); -} \ No newline at end of file +} diff --git a/shared/src/utils/env_utils.rs b/shared/src/utils/env_utils.rs index 1353990..8e09ef6 100644 --- a/shared/src/utils/env_utils.rs +++ b/shared/src/utils/env_utils.rs @@ -4,10 +4,12 @@ use std::env; /// Retourne `Ok` avec la valeur convertie ou `Err` avec un message d'erreur. pub fn get_env_var_as_u16(var_name: &str) -> Result { match env::var(var_name) { - Ok(value) => { - value.parse::() - .map_err(|e| format!("Erreur lors de la conversion de la variable d'environnement {} en u16: {}", var_name, e)) - }, + Ok(value) => value.parse::().map_err(|e| { + format!( + "Erreur lors de la conversion de la variable d'environnement {} en u16: {}", + var_name, e + ) + }), Err(_) => Err(format!("Variable d'environnement {} non définie", var_name)), } } diff --git a/shared/src/utils/image.rs b/shared/src/utils/image.rs index 6b05f50..5182c2d 100644 --- a/shared/src/utils/image.rs +++ b/shared/src/utils/image.rs @@ -4,9 +4,9 @@ use crate::types::{pixel_intensity::PixelIntensity, resolution::Resolution}; use super::{colors_utils::color, env_utils::get_env_var_as_u16}; - - -pub fn image_from_pixel_intensity(pixel_intensity: Vec) -> Result, Vec>, Box> { +pub fn image_from_pixel_intensity( + pixel_intensity: Vec, +) -> Result, Vec>, Box> { let width = get_env_var_as_u16("RESOLUTION_WIDTH")?; let height = get_env_var_as_u16("RESOLUTION_HEIGHT")?; @@ -19,4 +19,4 @@ pub fn image_from_pixel_intensity(pixel_intensity: Vec) -> Resul } Ok(img) -} \ No newline at end of file +} diff --git a/shared/src/utils/mod.rs b/shared/src/utils/mod.rs index da4f755..65f11e3 100644 --- a/shared/src/utils/mod.rs +++ b/shared/src/utils/mod.rs @@ -5,5 +5,5 @@ pub mod fragment_request_impl; pub mod fragment_result_impl; pub mod fragment_task_impl; pub mod image; +pub mod pixel_intensity_impl; pub mod type_of; -pub mod pixel_intensity_impl; \ No newline at end of file diff --git a/shared/src/utils/pixel_data_impl.rs b/shared/src/utils/pixel_data_impl.rs index 31d7141..a4a12ac 100644 --- a/shared/src/utils/pixel_data_impl.rs +++ b/shared/src/utils/pixel_data_impl.rs @@ -12,4 +12,4 @@ impl PixelData { pub fn new(resolution: Resolution, data: Vec) -> PixelData { PixelData { resolution, data } } -} \ No newline at end of file +} diff --git a/shared/src/utils/pixel_intensity_impl.rs b/shared/src/utils/pixel_intensity_impl.rs index f399d1d..d9df72a 100644 --- a/shared/src/utils/pixel_intensity_impl.rs +++ b/shared/src/utils/pixel_intensity_impl.rs @@ -16,23 +16,25 @@ impl PixelIntensity { pub fn vec_data_to_pixel_intensity_matrix(vec_data: Vec) -> Vec { let mut pixel_intensity_matrix = Vec::new(); let mut i = 0; - + // Assurez-vous que i + la taille de zn + la taille de count n'est pas hors limite while i + 8 + 4 <= vec_data.len() { // Extraire zn - let zn_bytes = <[u8; 4]>::try_from(&vec_data[i..i+4]).expect("slice with incorrect length"); + let zn_bytes = + <[u8; 4]>::try_from(&vec_data[i..i + 4]).expect("slice with incorrect length"); let zn = f32::from_be_bytes(zn_bytes); i += 4; // Extraire count - let count_bytes = <[u8; 4]>::try_from(&vec_data[i..i+4]).expect("slice with incorrect length"); + let count_bytes = + <[u8; 4]>::try_from(&vec_data[i..i + 4]).expect("slice with incorrect length"); let count = f32::from_be_bytes(count_bytes); i += 4; // Ajouter à la matrice pixel_intensity_matrix.push(PixelIntensity { zn, count }); } - + pixel_intensity_matrix } } @@ -52,12 +54,9 @@ mod pixel_intensity_tests { #[test] fn should_handle_vector_with_incomplete_zn_bytes() { - let vec_data: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0 - ]; - + let vec_data: Vec = vec![0, 0, 0, 0, 0, 0, 0]; + let result = PixelIntensity::vec_data_to_pixel_intensity_matrix(vec_data); assert_eq!(result.len(), 0); } - -} \ No newline at end of file +}