First naive implementation of tcp server. Not working right now
This commit is contained in:
23
flutr.rs
Normal file
23
flutr.rs
Normal 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
26
src/flutr.rs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
29
src/main.rs
29
src/main.rs
@@ -1,17 +1,22 @@
|
||||
use address::Host;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use image::{ImageReader, Rgb};
|
||||
use image::ImageReader;
|
||||
|
||||
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)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
image_path: PathBuf,
|
||||
addr: Host,
|
||||
host: Host,
|
||||
|
||||
#[arg(short, long, default_value_t = 1234)]
|
||||
port: u16,
|
||||
@@ -20,7 +25,16 @@ struct Args {
|
||||
fn main() {
|
||||
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 {
|
||||
Ok(img) => img,
|
||||
@@ -29,12 +43,11 @@ fn main() {
|
||||
|
||||
let rgb_image = image.into_rgb8();
|
||||
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> {
|
||||
ImageReader::open(path)?.with_guessed_format()?.decode()
|
||||
|
||||
18
src/pixelmsgs.rs
Normal file
18
src/pixelmsgs.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user