First naive implementation of tcp server. Not working right now

This commit is contained in:
Jonas Zeunert
2024-12-30 15:41:58 +01:00
parent 30f374bfc4
commit 5bd7626843
4 changed files with 88 additions and 8 deletions

23
flutr.rs Normal file
View File

@@ -0,0 +1,23 @@
pub struct FlutR {
stream: TcpStream,
msgs: PixelMsgs,
}
impl FlutR {
fn new(host: Host, port: u16, msgs: PixelMsgs) -> Result<Self, std::io::Error> {
let stream = TcpStream::connect(format!("{host}:{port}"))?;
Ok(FlutR { stream, msgs })
}
fn flut(&mut self) {
for msg in self.msgs.clone() {
dbg!(&msg);
let result = self.stream.write(msg.as_str().as_bytes());
match result {
Ok(_) => (),
Err(err) => eprintln!("{err}"),
}
let _ = self.stream.flush();
}
}
}

26
src/flutr.rs Normal file
View File

@@ -0,0 +1,26 @@
use crate::pixelmsgs::PixelMsgs;
use std::{io::Write, net::TcpStream};
pub struct FlutR {
stream: TcpStream,
msgs: PixelMsgs,
}
impl FlutR {
pub fn new(host: Host, port: u16, msgs: PixelMsgs) -> Result<Self, std::io::Error> {
let stream = TcpStream::connect(format!("{host}:{port}"))?;
Ok(FlutR { stream, msgs })
}
pub fn flut(&mut self) {
for msg in self.msgs.clone() {
dbg!(&msg);
let result = self.stream.write(msg.as_str().as_bytes());
match result {
Ok(_) => (),
Err(err) => eprintln!("{err}"),
}
let _ = self.stream.flush();
}
}
}

View File

@@ -1,17 +1,22 @@
use address::Host; use address::Host;
use std::path::PathBuf; use std::path::PathBuf;
use image::{ImageReader, Rgb}; use image::ImageReader;
use clap::Parser; use clap::Parser;
type Pixel<'a> = (u32, u32, &'a Rgb<u8>); mod flutr;
mod pixelmsgs;
use flutr::FlutR;
use pixelmsgs::Add;
use pixelmsgs::PixelMsgs;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
struct Args { struct Args {
image_path: PathBuf, image_path: PathBuf,
addr: Host, host: Host,
#[arg(short, long, default_value_t = 1234)] #[arg(short, long, default_value_t = 1234)]
port: u16, port: u16,
@@ -20,7 +25,16 @@ struct Args {
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
let image_result = parse_image(&args.image_path); let pixel_msgs = parse_image_to_msgs(&args.image_path);
let mut flutr = FlutR::new(args.host, args.port, pixel_msgs).unwrap();
flutr.flut();
}
fn parse_image_to_msgs(path: &PathBuf) -> PixelMsgs {
let mut result = PixelMsgs::default();
let image_result = parse_image(path);
let image = match image_result { let image = match image_result {
Ok(img) => img, Ok(img) => img,
@@ -29,12 +43,11 @@ fn main() {
let rgb_image = image.into_rgb8(); let rgb_image = image.into_rgb8();
for pixel in rgb_image.enumerate_pixels() { for pixel in rgb_image.enumerate_pixels() {
write_pixel(pixel); result.add(pixel);
} }
//println!("{:?}", &image);
}
fn write_pixel(pixel: Pixel) {} result
}
fn parse_image(path: &PathBuf) -> Result<image::DynamicImage, image::error::ImageError> { fn parse_image(path: &PathBuf) -> Result<image::DynamicImage, image::error::ImageError> {
ImageReader::open(path)?.with_guessed_format()?.decode() ImageReader::open(path)?.with_guessed_format()?.decode()

18
src/pixelmsgs.rs Normal file
View File

@@ -0,0 +1,18 @@
use image::Rgb;
pub type PixelMsgs = Vec<String>;
pub trait Add {
fn add(&mut self, item: (u32, u32, &Rgb<u8>));
}
impl Add for PixelMsgs {
fn add(&mut self, item: (u32, u32, &Rgb<u8>)) {
let color: &Rgb<u8> = item.2;
let r = color.0[0];
let g = color.0[1];
let b = color.0[2];
let msg = format!("PX {} {} {r:02x}{g:02x}{b:02x}", item.0, item.1);
self.push(msg);
}
}