From 06ffd5a5e2ea1a4fbc46bf91352145e31d379d7e Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Thu, 14 Nov 2024 15:44:44 +0200 Subject: [PATCH] feat: move proto files to root, rename services --- emerwen-master/src/main.rs | 6 ++++- emerwen-master/src/server/mod.rs | 8 +++--- emerwen-master/src/worker.rs | 2 +- emerwen-protocol/Cargo.toml | 2 +- emerwen-protocol/build.rs | 9 ++++++- emerwen-protocol/proto/emerwen.proto | 39 ---------------------------- emerwen-protocol/src/lib.rs | 4 +-- emerwen-worker/src/main.rs | 4 +-- emerwen-worker/src/monitor.rs | 10 +++---- proto/shared/target.proto | 23 ++++++++++++++++ proto/workertomaster.proto | 19 ++++++++++++++ 11 files changed, 70 insertions(+), 56 deletions(-) delete mode 100644 emerwen-protocol/proto/emerwen.proto create mode 100644 proto/shared/target.proto create mode 100644 proto/workertomaster.proto diff --git a/emerwen-master/src/main.rs b/emerwen-master/src/main.rs index 7faea56..a007cf9 100644 --- a/emerwen-master/src/main.rs +++ b/emerwen-master/src/main.rs @@ -6,7 +6,7 @@ */ use clap::Parser; -use emerwen_protocol::{ +use emerwen_protocol::emerwen::shared::{ target::{MethodGet, MethodPing}, Target, }; @@ -30,6 +30,10 @@ struct Args { /// Location of the database file #[arg(long, default_value = "./tmp/db.redb")] database_file: String, + + /// Location of the socket + #[arg(long, default_value = "./tmp/master.sock")] + socket_location: String, } #[tokio::main] diff --git a/emerwen-master/src/server/mod.rs b/emerwen-master/src/server/mod.rs index 41a2122..203ae57 100644 --- a/emerwen-master/src/server/mod.rs +++ b/emerwen-master/src/server/mod.rs @@ -7,8 +7,8 @@ use std::sync::Arc; -use emerwen_protocol::{ - emerwen_protocol_server::{EmerwenProtocol, EmerwenProtocolServer}, +use emerwen_protocol::emerwen::workertomaster::{ + worker_to_master_server::{WorkerToMaster, WorkerToMasterServer}, SetTargetStateRequest, TargetsResponse, }; use tokio::sync::{mpsc, RwLock}; @@ -54,7 +54,7 @@ pub async fn run( Server::builder() .layer(async_interceptor(AuthInterceptor::new(db_handle.clone()))) - .add_service(EmerwenProtocolServer::new( + .add_service(WorkerToMasterServer::new( MasterServer::new(db_handle.clone(), workers_two).await, )) .serve(addr.parse()?) @@ -78,7 +78,7 @@ impl MasterServer { } #[tonic::async_trait] -impl EmerwenProtocol for MasterServer { +impl WorkerToMaster for MasterServer { async fn get_targets(&self, request: Request<()>) -> Result, Status> { let worker_id = match request.extensions().get::() { Some(context) => context.worker_id, diff --git a/emerwen-master/src/worker.rs b/emerwen-master/src/worker.rs index 937489f..c56e607 100644 --- a/emerwen-master/src/worker.rs +++ b/emerwen-master/src/worker.rs @@ -5,7 +5,7 @@ * more information. */ -use emerwen_protocol::Target; +use emerwen_protocol::emerwen::shared::Target; use redb::{TypeName, Value}; use serde::{Deserialize, Serialize}; diff --git a/emerwen-protocol/Cargo.toml b/emerwen-protocol/Cargo.toml index 6d5ca11..63de25f 100644 --- a/emerwen-protocol/Cargo.toml +++ b/emerwen-protocol/Cargo.toml @@ -10,7 +10,7 @@ repository.workspace = true [dependencies] prost = "0.13" -serde = "1" +serde = { version = "1", features = ["derive"] } tonic = "0.12" [build-dependencies] diff --git a/emerwen-protocol/build.rs b/emerwen-protocol/build.rs index 9dc3ad9..8bd2f48 100644 --- a/emerwen-protocol/build.rs +++ b/emerwen-protocol/build.rs @@ -1,5 +1,6 @@ fn main() -> Result<(), Box> { tonic_build::configure() + .include_file("mod.rs") .type_attribute("Target", "#[derive(serde::Deserialize, serde::Serialize)]") .type_attribute( "Target.method", @@ -13,6 +14,12 @@ fn main() -> Result<(), Box> { "Target.MethodGET", "#[derive(serde::Deserialize, serde::Serialize)]", ) - .compile_protos(&["proto/emerwen.proto"], &["proto"])?; + .compile_protos( + &[ + "../proto/workertomaster.proto", + "../proto/shared/target.proto", + ], + &["../proto"], + )?; Ok(()) } diff --git a/emerwen-protocol/proto/emerwen.proto b/emerwen-protocol/proto/emerwen.proto deleted file mode 100644 index 7ccfad1..0000000 --- a/emerwen-protocol/proto/emerwen.proto +++ /dev/null @@ -1,39 +0,0 @@ -syntax = "proto3"; - -import "google/protobuf/empty.proto"; -package emerwen; - -service EmerwenProtocol { - rpc GetTargets (google.protobuf.Empty) returns (TargetsResponse); - rpc SetTargetState (SetTargetStateRequest) returns (google.protobuf.Empty); -} - -message Target { - uint32 id = 1; - string addr = 2; - uint32 interval = 3; - oneof method { - MethodPing ping = 4; - MethodGET get = 5; - } - - message MethodPing {} - message MethodGET { - repeated uint32 ok_codes = 1; - } -} - -message TargetsResponse { - repeated Target targets = 1; -} - -enum TargetState { - DOWN = 0; - UP = 1; - UNKNOWN = 2; -} - -message SetTargetStateRequest { - uint32 id = 1; - TargetState state = 2; -} diff --git a/emerwen-protocol/src/lib.rs b/emerwen-protocol/src/lib.rs index d6fb415..0dee097 100644 --- a/emerwen-protocol/src/lib.rs +++ b/emerwen-protocol/src/lib.rs @@ -5,9 +5,9 @@ * more information. */ -use target::{Method, MethodGet, MethodPing}; +tonic::include_proto!("mod"); -tonic::include_proto!("emerwen"); +use emerwen::shared::target::{Method, MethodGet, MethodPing}; impl From for Method { fn from(value: MethodPing) -> Self { diff --git a/emerwen-worker/src/main.rs b/emerwen-worker/src/main.rs index 2bfa655..5e0e052 100644 --- a/emerwen-worker/src/main.rs +++ b/emerwen-worker/src/main.rs @@ -6,7 +6,7 @@ */ use clap::Parser; -use emerwen_protocol::emerwen_protocol_client::EmerwenProtocolClient; +use emerwen_protocol::emerwen::workertomaster::worker_to_master_client::WorkerToMasterClient; use tonic::{metadata::MetadataValue, service::Interceptor, transport::Channel}; use tracing::{debug, info}; use tracing_subscriber::{prelude::*, EnvFilter}; @@ -70,7 +70,7 @@ async fn main() -> Result<(), Box> { .await?; // Client is cheap to clone. - let mut client = EmerwenProtocolClient::with_interceptor( + let mut client = WorkerToMasterClient::with_interceptor( channel, AuthInterceptor { token: format!("Bearer {}", args.auth_token).parse()?, diff --git a/emerwen-worker/src/monitor.rs b/emerwen-worker/src/monitor.rs index 849645c..7df9464 100644 --- a/emerwen-worker/src/monitor.rs +++ b/emerwen-worker/src/monitor.rs @@ -7,9 +7,9 @@ use std::time::Duration; -use emerwen_protocol::{ - emerwen_protocol_client::EmerwenProtocolClient, target::Method, SetTargetStateRequest, Target, - TargetState, +use emerwen_protocol::emerwen::{ + shared::{target::Method, Target, TargetState}, + workertomaster::{worker_to_master_client::WorkerToMasterClient, SetTargetStateRequest}, }; use tonic::{service::interceptor::InterceptedService, transport::Channel}; @@ -17,14 +17,14 @@ use crate::AuthInterceptor; pub struct TargetMonitor { target: Target, - client: EmerwenProtocolClient>, + client: WorkerToMasterClient>, last_state: TargetState, } impl TargetMonitor { pub fn new( target: Target, - client: EmerwenProtocolClient>, + client: WorkerToMasterClient>, ) -> TargetMonitor { TargetMonitor { target, diff --git a/proto/shared/target.proto b/proto/shared/target.proto new file mode 100644 index 0000000..be7ff03 --- /dev/null +++ b/proto/shared/target.proto @@ -0,0 +1,23 @@ +syntax = "proto3"; +package emerwen.shared; + +message Target { + uint32 id = 1; + string addr = 2; + uint32 interval = 3; + oneof method { + MethodPing ping = 4; + MethodGET get = 5; + } + + message MethodPing {} + message MethodGET { + repeated uint32 ok_codes = 1; + } +} + +enum TargetState { + DOWN = 0; + UP = 1; + UNKNOWN = 2; +} diff --git a/proto/workertomaster.proto b/proto/workertomaster.proto new file mode 100644 index 0000000..ac6aa65 --- /dev/null +++ b/proto/workertomaster.proto @@ -0,0 +1,19 @@ +syntax = "proto3"; +package emerwen.workertomaster; + +import "google/protobuf/empty.proto"; +import "shared/target.proto"; + +service WorkerToMaster { + rpc GetTargets (google.protobuf.Empty) returns (TargetsResponse); + rpc SetTargetState (SetTargetStateRequest) returns (google.protobuf.Empty); +} + +message TargetsResponse { + repeated shared.Target targets = 1; +} + +message SetTargetStateRequest { + uint32 id = 1; + shared.TargetState state = 2; +} -- 2.44.1