Skip to content

Commit

Permalink
fix: misc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
imranbarbhuiya authored and root committed Aug 13, 2023
1 parent 520cb5f commit 5757db1
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 41 deletions.
9 changes: 8 additions & 1 deletion client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import { Timer } from './Timer.js';

const name = process.argv[2];

const Job = /**@type {const}*/ ({
Ping: 0,
Pong: 1,
GenerateImage: 2,
Unknown: 3,
});

const client = new Client(name, {
maximumRetries: Infinity,
retryTime: 2_000,
Expand Down Expand Up @@ -38,7 +45,7 @@ await sleep(5000);
const timer = new Timer();
for (let i = 0; i < 100; i++) {
// Send a message to the Rust server
const message = { payload: `ping` };
const message = { payload: `ping`, job: Job.Ping };

let thing = timer.time();
const msg = await client.sendTo('Sofi', message, { receptive: true });
Expand Down
3 changes: 1 addition & 2 deletions src/canvas/functions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::{cell::RefCell, fs::File, io::Read, rc::Rc};
use std::{fs::File, io::Read};

use skia_safe::{
font_style::{Slant, Weight, Width},
Font, FontStyle, Typeface,
};
use tokio;

use super::Canvas; // 0.2.21, features = ["macros"]

Expand Down
16 changes: 5 additions & 11 deletions src/canvas/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::mem;
pub mod functions;
pub mod request;
use image::EncodableLayout;
use skia_safe::{
image::{BitDepth, CachingHint},
AlphaType, Color, ColorSpace, ColorType, Data, EncodedImageFormat, Font, ISize, Image,
ImageGenerator, ImageInfo, Paint, PaintStyle, Path, Picture, Point, Rect, Surface,
Color, Data, EncodedImageFormat, Font, Image, ImageGenerator, Paint, PaintStyle, Path, Point,
Rect, Surface,
};
use webp::Encoder;
pub struct Canvas {
surface: Surface,
path: Path,
Expand Down Expand Up @@ -107,12 +104,9 @@ impl Canvas {
#[inline]
pub fn webp(&mut self) -> Vec<u8> {
let image = self.surface.image_snapshot();
let png_data = image.encode_to_data(EncodedImageFormat::WEBP).unwrap();
// let image_bytes = image::load_from_memory(png_data.as_bytes()).unwrap();

// let encoder = Encoder::from_image(&image_bytes).unwrap();

// encoder.encode(100.).to_vec()
let png_data = image
.encode_to_data_with_quality(EncodedImageFormat::WEBP, 80)
.unwrap();
png_data.as_bytes().to_vec()
}

Expand Down
6 changes: 2 additions & 4 deletions src/canvas/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ impl Request {

pub async fn fetch_buffer(&self, url: &str) -> Vec<u8> {
let res = self.client.get(url).send().await.unwrap();
let buffer = res.bytes().await.unwrap().to_vec();
buffer
res.bytes().await.unwrap().to_vec()
}
}

pub async fn fetch_buffer(url: &str) -> Vec<u8> {
let res = reqwest::get(url).await.unwrap();
let buffer = res.bytes().await.expect("issue").to_vec();
buffer
res.bytes().await.expect("issue").to_vec()
}
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
let _three_uid = _three_cards.2.get("unique_id").unwrap().as_str().unwrap();

// let one = format!("{_one_uid}:character_cards:buffer");
let one = "e0d140e3-a33f-45cd-a156-661ddc921ab3:character_cards:buffer".to_string();
let one = "8a89afd4-36c2-476b-929c-d050e83b0578:character_cards:buffer".to_string();
// let two = format!("{_two_uid}:character_cards:buffer");
let two = "c09404d4-f4e2-4c9c-ba13-3c2eacf486b1:character_cards:buffer".to_string();
let two = "3286f1af-e030-474b-9d34-cb4249196952:character_cards:buffer".to_string();
// let three = format!("{_three_uid}:character_cards:buffer");
let three = "ec153239-a238-4483-81b1-f8c38cc29eb1:character_cards:buffer".to_string();
let three = "3343cf53-bacd-49d0-8240-6dea019be172:character_cards:buffer".to_string();
let images = connection.mget(vec![one, two, three]).unwrap();

let image_one = deserialize_buffer(&images[0]).buffer;
Expand Down Expand Up @@ -146,11 +146,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
465.0,
);

let drop_image = canvas.webp();
let drop = canvas.webp();

let name = format!("./out/{}.{}.webp", 1, Utc::now().timestamp_millis());

let mut file = File::create(name).unwrap();
file.write_all(&drop_image).unwrap();

file.write_all(&drop).unwrap();
println!("Time taken: {:?}", Instant::now() - start);
});

Expand Down
2 changes: 0 additions & 2 deletions src/mongo/functions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::time::{Duration, Instant};

use mongodb::{
bson::{doc, Document},
options::FindOneOptions,
Expand Down
31 changes: 16 additions & 15 deletions src/tcp/message_handler.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
use serde::{Deserialize, Serialize};

// #[derive(Serialize, Deserialize, Debug)]
// pub enum Job {
// Ping,
// Pong,
// GenerateImage,
// }
#[derive(Serialize, Deserialize, Debug)]
pub enum Job {
Ping,
Pong,
GenerateImage,
Unknown,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Payload {
// pub job: Job,
pub payload: String,
pub job: Job,
pub data: String,
}

pub struct IncomingMessage {
pub id: u64,
pub receptive: bool,
pub data: Payload,
pub payload: Payload,
}

pub async fn handle_message(message: IncomingMessage) -> Option<Payload> {
match message.data.payload.as_str() {
"ping" => Some(Payload {
// job: Job::Pong,
payload: "pong".to_string(),
match message.payload.job {
Job::Ping => Some(Payload {
job: Job::Pong,
data: "pong".to_string(),
}),
_ => Some(Payload {
// job: Job::Pong,
payload: "unknown".to_string(),
job: Job::Unknown,
data: "unknown".to_string(),
}),
}
}
2 changes: 1 addition & 1 deletion src/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub async fn handle_connection(
let message = IncomingMessage {
id: header.id,
receptive: header.receptive,
data: message,
payload: message,
};

let data = handle_message(message).await;
Expand Down

0 comments on commit 5757db1

Please sign in to comment.