Skip to content

Commit

Permalink
Merge pull request #81 from jabibamman/restyled/feat/server-fragment_…
Browse files Browse the repository at this point in the history
…result

Restyle Feat - server fragment result
  • Loading branch information
jabibamman authored Feb 23, 2024
2 parents 7a11c61 + 1de0369 commit 22f3d7f
Show file tree
Hide file tree
Showing 18 changed files with 106 additions and 93 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ To generate documentation for all packages including dependencies:
cargo doc --open
```


## CLI Arguments

Represents command line arguments the CLI application
Expand Down
2 changes: 1 addition & 1 deletion cli/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions client/src/fractal_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -226,5 +226,4 @@ mod julia_descriptor_tests {
assert_eq!(img.dimensions(), (800, 600));
}
}

}
5 changes: 3 additions & 2 deletions client/src/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ pub fn process_fragment_task(
cli_args: &CliClientArgs,
) -> Result<TcpStream, FractalError> {
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);
Expand Down
2 changes: 1 addition & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!"),
Expand Down
29 changes: 15 additions & 14 deletions server/src/messages/fragment_maker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ pub fn create_tasks() -> Result<Vec<FragmentTask>, Box<dyn Error>> {
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 {
Expand All @@ -31,41 +34,39 @@ pub fn create_tasks() -> Result<Vec<FragmentTask>, Box<dyn Error>> {
}),
},
max_iteration: 64,
resolution: Resolution { nx: width, ny: height },
resolution: Resolution {
nx: width,
ny: height,
},
range: r,
};

tasks.push(task);
}

Ok(tasks)

}

pub fn process_result(_result: FragmentResult) {



}

pub fn process_result(_result: FragmentResult) {}

pub fn generate_range(full_image: Range, step: f64) -> Vec<Range> {
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;
}
y += y_step;
}
ranges
}

98 changes: 54 additions & 44 deletions server/src/messages/handler.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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) => {
Expand All @@ -117,49 +125,51 @@ 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);
}
}

Ok(())
}
}
5 changes: 4 additions & 1 deletion server/src/messages/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion shared/src/types/pixel_intensity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
pub struct PixelIntensity {
pub zn: f32,
pub count: f32,
}
}
1 change: 0 additions & 1 deletion shared/src/types/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ impl Point {
pub fn new(x: f64, y: f64) -> Point {
Point { x, y }
}

}
1 change: 0 additions & 1 deletion shared/src/types/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ impl Range {
Range { min, max }
}
}

1 change: 0 additions & 1 deletion shared/src/types/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ impl Resolution {
pub fn new(nx: u16, ny: u16) -> Resolution {
Resolution { nx, ny }
}

}
8 changes: 5 additions & 3 deletions shared/src/utils/colors_utils.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -74,4 +76,4 @@ fn test_color() {
assert!(test2.eq("u8"));

assert_eq!(result, [63, 191, 191]);
}
}
10 changes: 6 additions & 4 deletions shared/src/utils/env_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16, String> {
match env::var(var_name) {
Ok(value) => {
value.parse::<u16>()
.map_err(|e| format!("Erreur lors de la conversion de la variable d'environnement {} en u16: {}", var_name, e))
},
Ok(value) => value.parse::<u16>().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)),
}
}
8 changes: 4 additions & 4 deletions shared/src/utils/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PixelIntensity>) -> Result<ImageBuffer<Rgb<u8>, Vec<u8>>, Box<dyn std::error::Error>> {
pub fn image_from_pixel_intensity(
pixel_intensity: Vec<PixelIntensity>,
) -> Result<ImageBuffer<Rgb<u8>, Vec<u8>>, Box<dyn std::error::Error>> {
let width = get_env_var_as_u16("RESOLUTION_WIDTH")?;
let height = get_env_var_as_u16("RESOLUTION_HEIGHT")?;

Expand All @@ -19,4 +19,4 @@ pub fn image_from_pixel_intensity(pixel_intensity: Vec<PixelIntensity>) -> Resul
}

Ok(img)
}
}
2 changes: 1 addition & 1 deletion shared/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion shared/src/utils/pixel_data_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ impl PixelData {
pub fn new(resolution: Resolution, data: Vec<u8>) -> PixelData {
PixelData { resolution, data }
}
}
}
Loading

0 comments on commit 22f3d7f

Please sign in to comment.