From a63cbae570974ace23bd568e1e7bd71f696a6b1d Mon Sep 17 00:00:00 2001 From: xqtc Date: Fri, 6 Sep 2024 03:50:45 +0200 Subject: [PATCH] gRPC working \^w^/ --- flake.nix | 1 + uwusched-nodes/src/grpc/mod.rs | 24 +++++++++++++++++ uwusched-nodes/src/head.rs | 48 ++++++++++++---------------------- 3 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 uwusched-nodes/src/grpc/mod.rs diff --git a/flake.nix b/flake.nix index 288a293..cfae12c 100644 --- a/flake.nix +++ b/flake.nix @@ -32,6 +32,7 @@ pkgs.libiconv pkgs.pkg-config pkgs.protobuf + pkgs.grpcurl ]; rust-toolchain = pkgs.symlinkJoin { name = "rust-toolchain"; diff --git a/uwusched-nodes/src/grpc/mod.rs b/uwusched-nodes/src/grpc/mod.rs new file mode 100644 index 0000000..a0a99fa --- /dev/null +++ b/uwusched-nodes/src/grpc/mod.rs @@ -0,0 +1,24 @@ +use crate::sched::{DataResponse, DataRequest, data_server}; +use log::{debug, info}; + +#[derive(Debug, Default)] +pub struct DataService {} + +#[tonic::async_trait] +impl data_server::Data for DataService { + async fn data(&self, request: tonic::Request) -> Result, tonic::Status> { + info!("Got a request: {:?}", request); + let input = request.get_ref(); + let data = vec![]; + + let res = DataResponse { + node_id: input.node_id.clone(), + hashmap_id: input.hashmap_id.clone(), + uuid: input.uuid.clone(), + length: data.len().to_string(), + data, + }; + debug!("{:?}", &res); + Ok(tonic::Response::new(res)) + } +} diff --git a/uwusched-nodes/src/head.rs b/uwusched-nodes/src/head.rs index 27f5e07..19b8cbb 100644 --- a/uwusched-nodes/src/head.rs +++ b/uwusched-nodes/src/head.rs @@ -1,8 +1,10 @@ mod config; +mod grpc; use crate::config::CONFIG; use clap::{Parser, Subcommand}; -use log::debug; +use log::{debug, info}; +use tonic::transport::Server; // Import generated protobuf mod sched { tonic::include_proto!("sched"); @@ -30,37 +32,21 @@ fn get_type_of(_: &T) -> &'static str { std::any::type_name::() } -// impl sched::data_server::Data for sched::DataResponse { -// fn data<'life0, 'async_trait>( -// &'life0 self, -// request: tonic::Request, -// ) -> ::core::pin::Pin< -// Box< -// dyn ::core::future::Future< -// Output = std::result::Result< -// tonic::Response, -// tonic::Status, -// >, -// > + ::core::marker::Send -// + 'async_trait, -// >, -// > -// where -// 'life0: 'async_trait, -// Self: 'async_trait, -// { -// } -// } - -fn main() { +#[tokio::main] +async fn main() -> Result<(), Box> { std::env::set_var("RUST_LOG", CONFIG.log.level.clone()); pretty_env_logger::init(); + let addr = "[::1]:50051".parse()?; + info!("Starting gRPC server on {}", addr); + let data = grpc::DataService::default(); + Server::builder().add_service(sched::data_server::DataServer::new(data)).serve(addr).await?; debug!("{:?}", CONFIG.node); - let cli = Cli::parse(); - match &cli.command { - Some(Commands::Start { role }) => { - debug!("{:#?}", role) - } - None => {} - } + // let cli = Cli::parse(); + // match &cli.command { + // Some(Commands::Start { role }) => { + // debug!("{:#?}", role) + // } + // None => {} + // } + Ok(()) }