From 9aea6ee55fed64a86dc72785566867670e0acff6 Mon Sep 17 00:00:00 2001 From: Ramil_Algayev Date: Thu, 26 Dec 2024 23:18:47 +0400 Subject: [PATCH] I need help --- cli-mock/brain.proto | 22 +++++++++++---------- daemon-mock/brain.proto | 23 ++++++++++++---------- daemon-mock/src/main.rs | 42 +++++++++++++++++++++++++++++------------ 3 files changed, 55 insertions(+), 32 deletions(-) diff --git a/cli-mock/brain.proto b/cli-mock/brain.proto index fd8662f..94c6e40 100644 --- a/cli-mock/brain.proto +++ b/cli-mock/brain.proto @@ -12,15 +12,17 @@ message RegisterNodeReq { string node_pubkey = 1; string owner_pubkey = 2; string ip = 3; - string country = 4; - string city = 5; - uint32 avail_ports = 6; - uint32 avail_ipv4 = 7; - uint32 avail_ipv6 = 8; - uint32 avail_vcpus = 9; - uint32 avail_memory_mb = 10; - uint32 avail_storage_gb = 11; - uint32 max_ports_per_vm = 12; +} + +message NodeRes { + string node_pubkey = 1; + uint32 avail_ports = 2; + uint32 avail_ipv4 = 3; + uint32 avail_ipv6 = 4; + uint32 avail_vcpus = 5; + uint32 avail_memory_mb = 6; + uint32 avail_storage_gb = 7; + uint32 max_ports_per_vm = 8; } message NewVMReq { @@ -93,6 +95,7 @@ message DeleteVMReq { service BrainDaemonService { rpc RegisterNode (RegisterNodeReq) returns (Empty); + rpc NodeResUpdate (stream NodeRes) returns (Empty); rpc GetNewVMReqs (NodePubkey) returns (stream NewVMReq); rpc SendNewVMResp (stream NewVMResp) returns (Empty); rpc GetDeleteVMReq (NodePubkey) returns (stream DeleteVMReq); @@ -101,7 +104,6 @@ service BrainDaemonService { rpc SendUpdateVMResp (stream UpdateVMResp) returns (Empty); } - message NodeFilters { uint32 free_ports = 1; bool offers_ipv4 = 2; diff --git a/daemon-mock/brain.proto b/daemon-mock/brain.proto index fd8662f..f11a278 100644 --- a/daemon-mock/brain.proto +++ b/daemon-mock/brain.proto @@ -12,15 +12,17 @@ message RegisterNodeReq { string node_pubkey = 1; string owner_pubkey = 2; string ip = 3; - string country = 4; - string city = 5; - uint32 avail_ports = 6; - uint32 avail_ipv4 = 7; - uint32 avail_ipv6 = 8; - uint32 avail_vcpus = 9; - uint32 avail_memory_mb = 10; - uint32 avail_storage_gb = 11; - uint32 max_ports_per_vm = 12; +} + +message NodeRes { + string node_pubkey = 1; + uint32 avail_ports = 2; + uint32 avail_ipv4 = 3; + uint32 avail_ipv6 = 4; + uint32 avail_vcpus = 5; + uint32 avail_memory_mb = 6; + uint32 avail_storage_gb = 7; + uint32 max_ports_per_vm = 8; } message NewVMReq { @@ -93,6 +95,7 @@ message DeleteVMReq { service BrainDaemonService { rpc RegisterNode (RegisterNodeReq) returns (Empty); + rpc NodeResUpdate (stream NodeRes) returns (Empty); rpc GetNewVMReqs (NodePubkey) returns (stream NewVMReq); rpc SendNewVMResp (stream NewVMResp) returns (Empty); rpc GetDeleteVMReq (NodePubkey) returns (stream DeleteVMReq); @@ -101,7 +104,6 @@ service BrainDaemonService { rpc SendUpdateVMResp (stream UpdateVMResp) returns (Empty); } - message NodeFilters { uint32 free_ports = 1; bool offers_ipv4 = 2; @@ -128,3 +130,4 @@ service BrainCliService { rpc DeleteVM (DeleteVMReq) returns (Empty); rpc UpdateVM (UpdateVMReq) returns (UpdateVMResp); } + diff --git a/daemon-mock/src/main.rs b/daemon-mock/src/main.rs index 783f4a9..4c767aa 100644 --- a/daemon-mock/src/main.rs +++ b/daemon-mock/src/main.rs @@ -6,7 +6,7 @@ pub mod brain { use anyhow::Result; use brain::{ brain_daemon_service_client::BrainDaemonServiceClient, DeleteVmReq, NewVmResp, - NewVmReq, NodePubkey, RegisterNodeReq, UpdateVmReq, UpdateVmResp, + NewVmReq, NodePubkey, RegisterNodeReq, UpdateVmReq, UpdateVmResp, NodeRes, }; use lazy_static::lazy_static; use log::{debug, error, info, warn}; @@ -71,8 +71,23 @@ async fn register_node(mut client: BrainDaemonServiceClient) { node_pubkey: SECURE_PUBLIC_KEY.clone(), owner_pubkey: "IamTheOwnerOf".to_string() + &SECURE_PUBLIC_KEY, ip: "10.0.10.1".to_string(), - country: "Cyrodiil".to_string(), - city: "Bruma".to_string(), + }; + match client.register_node(req).await { + Ok(_) => info!( + "Registered as 10.0.10.1 {}", + SECURE_PUBLIC_KEY.clone() + ), + Err(e) => error!("Could not register node data: {e:?}"), + }; +} + +async fn update_node_res( + mut _client: BrainDaemonServiceClient, + tx: Sender, + ) -> Result<()> { + debug!("Starting node registration..."); + let req = NodeRes { + node_pubkey: SECURE_PUBLIC_KEY.clone(), avail_ports: 10000, avail_ipv4: 10, avail_ipv6: 100_000, @@ -81,13 +96,13 @@ async fn register_node(mut client: BrainDaemonServiceClient) { avail_storage_gb: 700, max_ports_per_vm: 5, }; - match client.register_node(req).await { - Ok(_) => info!( - "Registered as 10.0.10.1 from Bruma/Cyrodiil with ID {}", - SECURE_PUBLIC_KEY.clone() - ), - Err(e) => error!("Could not register node data: {e:?}"), - }; + + if let Err(e) = tx.send(req).await { + error!("Failed to send NodeRes: {e}"); + } else { + info!("NodeRes sent successfully"); + } + Ok(()) } async fn listen_for_deleted_vms( @@ -217,6 +232,10 @@ async fn connect_and_run() -> Result<()> { let mut streaming_tasks = JoinSet::new(); register_node(client.clone()).await; + + let update_node_res_client = client.clone(); + let (node_tx, _node_rx) = tokio::sync::mpsc::channel(6); + streaming_tasks.spawn(update_node_res(update_node_res_client, node_tx)); let newvm_client = client.clone(); let (tx, newvm_rx) = tokio::sync::mpsc::channel(6); @@ -242,13 +261,12 @@ async fn connect_and_run() -> Result<()> { handle_update_vm_requests(updatevm_rx, resp_tx).await; }); - let deletevms_client = client.clone(); let (tx, _deletevm_rx) = tokio::sync::mpsc::channel(6); streaming_tasks.spawn(listen_for_deleted_vms(deletevms_client, tx)); let task_output = streaming_tasks.join_next().await; - warn!("One stream exited: {task_output:?}"); + warn!("One stream exited: {task_output:?} {streaming_tasks:?}"); Ok(()) }