Skip to content

Commit

Permalink
feat: somewhat working UI channel with Rust
Browse files Browse the repository at this point in the history
Signed-off-by: Martichou <m@rtin.fyi>
  • Loading branch information
Martichou committed Feb 25, 2024
1 parent c2072b5 commit 6452132
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 85 deletions.
3 changes: 3 additions & 0 deletions core_lib/bindings/DeviceType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type DeviceType = "Unknown" | "Phone" | "Tablet" | "Laptop";
4 changes: 4 additions & 0 deletions core_lib/bindings/RemoteDeviceInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { DeviceType } from "./DeviceType";

export interface RemoteDeviceInfo { name: string, device_type: DeviceType, }
3 changes: 2 additions & 1 deletion core_lib/bindings/TransferMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { RemoteDeviceInfo } from "./RemoteDeviceInfo";

export interface TransferMetadata { id: string, files: Array<string>, pin_code: string | null, text_description: string | null, }
export interface TransferMetadata { id: string, source: RemoteDeviceInfo | null, files: Array<string>, pin_code: string | null, text_description: string | null, }
2 changes: 2 additions & 0 deletions core_lib/bindings/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from "./ChannelDirection"
export * from "./DeviceType"
export * from "./ChannelMessage"
export * from "./TransferMetadata"
export * from "./ChannelAction"
export * from "./RemoteDeviceInfo"
export * from "./State"
3 changes: 3 additions & 0 deletions core_lib/src/hdl/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ impl InboundRequest {
if !dest.exists() {
break;
}
dest.pop();
counter += 1;
}

Expand All @@ -731,6 +732,7 @@ impl InboundRequest {

let metadata = TransferMetadata {
id: self.state.id.clone(),
source: self.state.remote_device_info.clone(),
files: files_name,
pin_code: self.state.pin_code.clone(),
text_description: None,
Expand All @@ -757,6 +759,7 @@ impl InboundRequest {
if meta.r#type() == text_metadata::Type::Url {
let metadata = TransferMetadata {
id: self.state.id.clone(),
source: self.state.remote_device_info.clone(),
files: vec![],
pin_code: self.state.pin_code.clone(),
text_description: meta.text_title.clone(),
Expand Down
3 changes: 2 additions & 1 deletion core_lib/src/hdl/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use ts_rs::TS;

use crate::sharing_nearby::FileMetadata;
use crate::{sharing_nearby::FileMetadata, utils::RemoteDeviceInfo};

#[derive(Debug)]
pub struct InternalFileInfo {
Expand All @@ -19,6 +19,7 @@ pub struct InternalFileInfo {
#[ts(export)]
pub struct TransferMetadata {
pub id: String,
pub source: Option<RemoteDeviceInfo>,
pub files: Vec<String>,
pub pin_code: Option<String>,
pub text_description: Option<String>,
Expand Down
12 changes: 10 additions & 2 deletions core_lib/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use tokio::net::TcpListener;
use tokio::sync::broadcast::Sender;
use tokio_util::sync::CancellationToken;

use crate::channel::ChannelMessage;
use crate::channel::{ChannelDirection, ChannelMessage};
use crate::errors::AppError;
use crate::hdl::InboundRequest;
use crate::hdl::{InboundRequest, State};

const INNER_NAME: &str = "TcpServer";

Expand Down Expand Up @@ -39,6 +39,7 @@ impl TcpServer {
match r {
Ok((socket, remote_addr)) => {
trace!("{INNER_NAME}: new client: {remote_addr}");
let esender = self.sender.clone();
let csender = self.sender.clone();

tokio::spawn(async move {
Expand All @@ -50,6 +51,13 @@ impl TcpServer {
Err(e) => match e.downcast_ref() {
Some(AppError::NotAnError) => break,
None => {
let _ = esender.send(ChannelMessage {
id: remote_addr.to_string(),
direction: ChannelDirection::LibToFront,
action: None,
state: Some(State::Disconnected),
meta: None,
});
error!("{INNER_NAME}: error while handling client: {e}");
break;
}
Expand Down
8 changes: 6 additions & 2 deletions core_lib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ use base64::Engine;
use hkdf::Hkdf;
use p256::{PublicKey, SecretKey};
use rand::{Rng, RngCore};
use serde::{Deserialize, Serialize};
use sha2::digest::generic_array::GenericArray;
use sha2::Sha256;
use tokio::io::AsyncReadExt;
use tokio::net::TcpStream;
use ts_rs::TS;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize, Serialize, TS)]
#[ts(export)]
#[allow(dead_code)]
pub enum DeviceType {
Unknown = 0,
Expand All @@ -33,7 +36,8 @@ impl DeviceType {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize, Serialize, TS)]
#[ts(export)]
pub struct RemoteDeviceInfo {
pub name: String,
pub device_type: DeviceType,
Expand Down
5 changes: 3 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "cross-env RUST_BACKTRACE=1 concurrently -k \"tauri dev\" \"pnpm devtools\"",
"dev": "cross-env RUST_BACKTRACE=1 concurrently -k \"tauri dev\"",
"build": "tauri build",
"vite:dev": "vite dev",
"vite:build": "vite build",
Expand All @@ -17,6 +17,7 @@
},
"dependencies": {
"@tauri-apps/api": "1.5.1",
"@martichou/core_lib": "../core_lib",
"vue": "3.3.11"
},
"devDependencies": {
Expand Down Expand Up @@ -45,4 +46,4 @@
"vitest": "1.1.0",
"vue-tsc": "1.8.25"
}
}
}
3 changes: 3 additions & 0 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 11 additions & 12 deletions frontend/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#[macro_use]
extern crate log;

use rquickshare::channel::ChannelMessage;
use rquickshare::channel::{ChannelDirection, ChannelMessage};
use rquickshare::RQS;
use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu};
use tokio::sync::broadcast::Sender;
Expand Down Expand Up @@ -83,21 +83,20 @@ async fn main() -> Result<(), anyhow::Error> {
}

fn rs2js<R: tauri::Runtime>(message: ChannelMessage, manager: &impl Manager<R>) {
info!("rs2js: {:?}", &message);
if message.direction == ChannelDirection::FrontToLib {
return;
}

info!("rs2js: {:?}", &message);
manager.emit_all("rs2js", &message).unwrap();
}

#[tauri::command]
async fn js2rs(message: String, state: tauri::State<'_, AppState>) -> Result<(), String> {
fn js2rs(message: ChannelMessage, state: tauri::State<'_, AppState>) -> Result<(), String> {
info!("js2rs: {:?}", &message);

let _ = match serde_json::from_str::<ChannelMessage>(&message) {
Ok(m) => state.sender.send(m),
Err(e) => {
error!("Cannot serialize message: {} due to: {}", message, e);
return Err(String::from("Cannot serialize message"));
}
};

Ok(())
match state.sender.send(message) {
Ok(_) => Ok(()),
Err(e) => return Err(format!("Coudln't perform: {}", e))
}
}
Loading

0 comments on commit 6452132

Please sign in to comment.