From 91b99511153d9e5adb1ffba835cb0706a54676f0 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Thu, 14 Nov 2024 16:13:49 +0200 Subject: [PATCH] feat(master): change to protocol Worker type * also hella strange WorkerType for the redb table value --- emerwen-master/src/db.rs | 38 ++++++++++++++++++++++++--- emerwen-master/src/main.rs | 12 +++++---- emerwen-master/src/server/mod.rs | 11 +++++--- emerwen-master/src/worker.rs | 45 -------------------------------- 4 files changed, 48 insertions(+), 58 deletions(-) delete mode 100644 emerwen-master/src/worker.rs diff --git a/emerwen-master/src/db.rs b/emerwen-master/src/db.rs index ad19b7e..5a0f4b3 100644 --- a/emerwen-master/src/db.rs +++ b/emerwen-master/src/db.rs @@ -7,13 +7,12 @@ use std::path::Path; -use redb::{Database, ReadableTable, TableDefinition}; +use emerwen_protocol::emerwen::shared::Worker; +use redb::{Database, ReadableTable, TableDefinition, TypeName, Value}; use tokio::sync::{mpsc, oneshot}; use tracing::warn; -use crate::worker::Worker; - -const WORKER_TABLE: TableDefinition = TableDefinition::new("workers"); +const WORKER_TABLE: TableDefinition = TableDefinition::new("workers"); pub enum DBMessage { ReadWorkers { @@ -119,3 +118,34 @@ impl DatabaseHandle { rx.await.expect("actor task has been killed") // FIXME: handle } } + +#[derive(Debug)] +struct WorkerType; + +impl Value for WorkerType { + type SelfType<'a> = Worker; + type AsBytes<'a> = Vec; + + fn fixed_width() -> Option { + None + } + + fn from_bytes<'a>(data: &'a [u8]) -> Self::SelfType<'a> + where + Self: 'a, + { + bincode::deserialize(data).unwrap() + } + + fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> Self::AsBytes<'a> + where + Self: 'a, + Self: 'b, + { + bincode::serialize(value).unwrap() + } + + fn type_name() -> TypeName { + TypeName::new("emerwen_worker") + } +} diff --git a/emerwen-master/src/main.rs b/emerwen-master/src/main.rs index a007cf9..c912a5d 100644 --- a/emerwen-master/src/main.rs +++ b/emerwen-master/src/main.rs @@ -8,7 +8,7 @@ use clap::Parser; use emerwen_protocol::emerwen::shared::{ target::{MethodGet, MethodPing}, - Target, + Target, Worker, }; use tokio::sync::mpsc; use tracing::{debug, info, level_filters::LevelFilter}; @@ -17,8 +17,6 @@ use tracing_subscriber::{prelude::*, EnvFilter}; mod db; use db::DatabaseHandle; mod server; -mod worker; -use worker::Worker; #[derive(Parser)] #[command(version)] @@ -31,9 +29,13 @@ struct Args { #[arg(long, default_value = "./tmp/db.redb")] database_file: String, - /// Location of the socket + /// Location of the "IPC" socket #[arg(long, default_value = "./tmp/master.sock")] - socket_location: String, + ipc_socket_location: String, + + /// IPC auth token + #[arg(long, default_value = "ipc_avain_prkl")] + ipc_auth_token: String, } #[tokio::main] diff --git a/emerwen-master/src/server/mod.rs b/emerwen-master/src/server/mod.rs index 203ae57..7da502a 100644 --- a/emerwen-master/src/server/mod.rs +++ b/emerwen-master/src/server/mod.rs @@ -7,16 +7,19 @@ use std::sync::Arc; -use emerwen_protocol::emerwen::workertomaster::{ - worker_to_master_server::{WorkerToMaster, WorkerToMasterServer}, - SetTargetStateRequest, TargetsResponse, +use emerwen_protocol::emerwen::{ + shared::Worker, + workertomaster::{ + worker_to_master_server::{WorkerToMaster, WorkerToMasterServer}, + SetTargetStateRequest, TargetsResponse, + }, }; use tokio::sync::{mpsc, RwLock}; use tonic::{transport::Server, Request, Response, Status}; use tonic_async_interceptor::async_interceptor; use tracing::info; -use crate::{db::DatabaseHandle, worker::Worker}; +use crate::db::DatabaseHandle; mod auth; use auth::AuthInterceptor; diff --git a/emerwen-master/src/worker.rs b/emerwen-master/src/worker.rs deleted file mode 100644 index c56e607..0000000 --- a/emerwen-master/src/worker.rs +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2024 Jonni Liljamo - * - * This file is licensed under GPL-3.0-or-later, see NOTICE and LICENSE for - * more information. - */ - -use emerwen_protocol::emerwen::shared::Target; -use redb::{TypeName, Value}; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Deserialize, Serialize)] -pub struct Worker { - pub id: u32, - pub auth_token: String, - pub targets: Vec, -} - -impl Value for Worker { - type SelfType<'a> = Worker; - type AsBytes<'a> = Vec; - - fn fixed_width() -> Option { - None - } - - fn from_bytes<'a>(data: &'a [u8]) -> Self::SelfType<'a> - where - Self: 'a, - { - bincode::deserialize(data).unwrap() - } - - fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> Self::AsBytes<'a> - where - Self: 'a, - Self: 'b, - { - bincode::serialize(value).unwrap() - } - - fn type_name() -> TypeName { - TypeName::new("emerwen_master_worker") - } -} -- 2.44.1