DEVELOPMENT ENVIRONMENT

~liljamo/emerwen

06ffd5a5e2ea1a4fbc46bf91352145e31d379d7e — Jonni Liljamo 9 days ago 0bedb35
feat: move proto files to root, rename services
M emerwen-master/src/main.rs => emerwen-master/src/main.rs +5 -1
@@ 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]

M emerwen-master/src/server/mod.rs => emerwen-master/src/server/mod.rs +4 -4
@@ 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<Response<TargetsResponse>, Status> {
        let worker_id = match request.extensions().get::<WorkerContext>() {
            Some(context) => context.worker_id,

M emerwen-master/src/worker.rs => emerwen-master/src/worker.rs +1 -1
@@ 5,7 5,7 @@
 * more information.
 */

use emerwen_protocol::Target;
use emerwen_protocol::emerwen::shared::Target;
use redb::{TypeName, Value};
use serde::{Deserialize, Serialize};


M emerwen-protocol/Cargo.toml => emerwen-protocol/Cargo.toml +1 -1
@@ 10,7 10,7 @@ repository.workspace = true

[dependencies]
prost = "0.13"
serde = "1"
serde = { version = "1", features = ["derive"] }
tonic = "0.12"

[build-dependencies]

M emerwen-protocol/build.rs => emerwen-protocol/build.rs +8 -1
@@ 1,5 1,6 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
    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<dyn std::error::Error>> {
            "Target.MethodGET",
            "#[derive(serde::Deserialize, serde::Serialize)]",
        )
        .compile_protos(&["proto/emerwen.proto"], &["proto"])?;
        .compile_protos(
            &[
                "../proto/workertomaster.proto",
                "../proto/shared/target.proto",
            ],
            &["../proto"],
        )?;
    Ok(())
}

M emerwen-protocol/src/lib.rs => emerwen-protocol/src/lib.rs +2 -2
@@ 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<MethodPing> for Method {
    fn from(value: MethodPing) -> Self {

M emerwen-worker/src/main.rs => emerwen-worker/src/main.rs +2 -2
@@ 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<dyn std::error::Error>> {
        .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()?,

M emerwen-worker/src/monitor.rs => emerwen-worker/src/monitor.rs +5 -5
@@ 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<InterceptedService<Channel, AuthInterceptor>>,
    client: WorkerToMasterClient<InterceptedService<Channel, AuthInterceptor>>,
    last_state: TargetState,
}

impl TargetMonitor {
    pub fn new(
        target: Target,
        client: EmerwenProtocolClient<InterceptedService<Channel, AuthInterceptor>>,
        client: WorkerToMasterClient<InterceptedService<Channel, AuthInterceptor>>,
    ) -> TargetMonitor {
        TargetMonitor {
            target,

R emerwen-protocol/proto/emerwen.proto => proto/shared/target.proto +1 -17
@@ 1,12 1,5 @@
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);
}
package emerwen.shared;

message Target {
    uint32 id = 1;


@@ 23,17 16,8 @@ message Target {
    }
}

message TargetsResponse {
    repeated Target targets = 1;
}

enum TargetState {
    DOWN = 0;
    UP = 1;
    UNKNOWN = 2;
}

message SetTargetStateRequest {
    uint32 id = 1;
    TargetState state = 2;
}

A proto/workertomaster.proto => proto/workertomaster.proto +19 -0
@@ 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;
}