Replace gRPC Backend (#10)

**Rationale:**

Having two separate servers and communication methods resulted in additional maintenance & the need to convert often between backend & frontend data types.
By moving the backend communication off of gRPC and to just use websockets it both gives more control & allows for simplification of the implementation.

#8

**Changes:**

- Replaces gRPC backend.
  - New implementation automatically handles reconnect logic
- Implements an api layer
- Migrates examples to the api layer
- Implements a proc macro to make command handling easier
- Implements unit tests for the api layer (90+% coverage)
- Implements integration tests for the proc macro (90+% coverage)

Reviewed-on: #10
Co-authored-by: Sergey Savelyev <sergeysav.nn@gmail.com>
Co-committed-by: Sergey Savelyev <sergeysav.nn@gmail.com>
This commit was merged in pull request #10.
This commit is contained in:
2026-01-01 10:11:53 -08:00
committed by sergeysav
parent f658b55586
commit 788dd10a91
68 changed files with 3934 additions and 1504 deletions

View File

@@ -4,9 +4,11 @@ name = "simple_producer"
edition = "2021"
[dependencies]
server = { path = "../../server" }
tonic = "0.12.3"
tokio = { version = "1.43.0", features = ["rt-multi-thread", "signal"] }
chrono = "0.4.39"
tokio-util = "0.7.13"
num-traits = "0.2.19"
anyhow = { workspace = true }
api = { path = "../../api" }
chrono = { workspace = true }
env_logger = { workspace = true }
futures-util = { workspace = true }
num-traits = { workspace = true }
tokio = { workspace = true, features = ["rt-multi-thread", "signal", "time", "macros"] }
tokio-util = { workspace = true }

View File

@@ -1,213 +1,44 @@
use chrono::DateTime;
use num_traits::float::FloatConst;
use server::core::telemetry_service_client::TelemetryServiceClient;
use server::core::telemetry_value::Value;
use server::core::{
TelemetryDataType, TelemetryDefinitionRequest, TelemetryItem, TelemetryValue, Timestamp, Uuid,
};
use std::error::Error;
use api::client::telemetry::TelemetryRegistry;
use api::client::Client;
use chrono::{TimeDelta, Utc};
use futures_util::future::join_all;
use num_traits::FloatConst;
use std::f64;
use std::sync::Arc;
use std::time::Duration;
use tokio::select;
use tokio::sync::mpsc;
use tokio::sync::mpsc::Sender;
use tokio::time::Instant;
use tokio::time::{sleep_until, Instant};
use tokio_util::sync::CancellationToken;
use tonic::codegen::tokio_stream::wrappers::ReceiverStream;
use tonic::codegen::tokio_stream::StreamExt;
use tonic::codegen::StdError;
use tonic::transport::Channel;
use tonic::Request;
struct Telemetry {
client: TelemetryServiceClient<Channel>,
tx: Sender<TelemetryItem>,
cancel: CancellationToken,
}
struct TelemetryItemHandle {
uuid: String,
tx: Sender<TelemetryItem>,
}
impl Telemetry {
pub async fn new<D>(dst: D) -> Result<Self, Box<dyn Error>>
where
D: TryInto<tonic::transport::Endpoint>,
D::Error: Into<StdError>,
{
let mut client = TelemetryServiceClient::connect(dst).await?;
let client_stored = client.clone();
let cancel = CancellationToken::new();
let cancel_stored = cancel.clone();
let (local_tx, mut local_rx) = mpsc::channel(16);
tokio::spawn(async move {
while !cancel.is_cancelled() {
let (server_tx, server_rx) = mpsc::channel(16);
let response_stream = client
.insert_telemetry(ReceiverStream::new(server_rx))
.await;
if let Ok(response_stream) = response_stream {
let mut response_stream = response_stream.into_inner();
loop {
select! {
_ = cancel.cancelled() => {
break;
},
Some(item) = local_rx.recv() => {
match server_tx.send(item).await {
Ok(_) => {}
Err(_) => break,
}
},
Some(response) = response_stream.next() => {
match response {
Ok(_) => {}
Err(_) => {
break;
}
}
},
else => break,
}
}
} else {
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
});
Ok(Self {
client: client_stored,
tx: local_tx,
cancel: cancel_stored,
})
}
pub async fn register(
&mut self,
name: String,
data_type: TelemetryDataType,
) -> Result<TelemetryItemHandle, Box<dyn Error>> {
let response = self
.client
.new_telemetry(Request::new(TelemetryDefinitionRequest {
name,
data_type: data_type.into(),
}))
.await?
.into_inner();
let Some(uuid) = response.uuid else {
return Err("UUID Missing".into());
};
Ok(TelemetryItemHandle {
uuid: uuid.value,
tx: self.tx.clone(),
})
}
}
impl Drop for Telemetry {
fn drop(&mut self) {
self.cancel.cancel();
}
}
impl TelemetryItemHandle {
pub async fn publish(
&self,
value: Value,
timestamp: DateTime<chrono::Utc>,
) -> Result<(), Box<dyn Error>> {
let offset_from_unix_epoch =
timestamp - DateTime::from_timestamp(0, 0).expect("Could not get Unix epoch");
self.tx
.send(TelemetryItem {
uuid: Some(Uuid {
value: self.uuid.clone(),
}),
value: Some(TelemetryValue { value: Some(value) }),
timestamp: Some(Timestamp {
secs: offset_from_unix_epoch.num_seconds(),
nanos: offset_from_unix_epoch.subsec_nanos(),
}),
})
.await?;
Ok(())
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mut tlm = Telemetry::new("http://[::1]:50051").await?;
async fn main() -> anyhow::Result<()> {
env_logger::init();
let index_handle = tlm
.register(
"simple_producer/time_offset".into(),
TelemetryDataType::Float64,
)
.await?;
let client = Arc::new(Client::connect("ws://localhost:8080/backend")?);
let tlm = TelemetryRegistry::new(client);
let publish_offset = tlm
.register(
"simple_producer/publish_offset".into(),
TelemetryDataType::Float64,
)
.await?;
let time_offset = tlm.register::<f64>("simple_producer/time_offset").await;
let await_offset = tlm
.register(
"simple_producer/await_offset".into(),
TelemetryDataType::Float64,
)
.await?;
let publish_offset = tlm.register::<f64>("simple_producer/publish_offset").await;
let sin_tlm_handle = tlm
.register("simple_producer/sin".into(), TelemetryDataType::Float32)
.await?;
let cos_tlm_handle = tlm
.register("simple_producer/cos".into(), TelemetryDataType::Float64)
.await?;
let bool_tlm_handle = tlm
.register("simple_producer/bool".into(), TelemetryDataType::Boolean)
.await?;
let await_offset = tlm.register::<f64>("simple_producer/await_offset").await;
let sin2_tlm_handle = tlm
.register("simple_producer/sin2".into(), TelemetryDataType::Float32)
.await?;
let cos2_tlm_handle = tlm
.register("simple_producer/cos2".into(), TelemetryDataType::Float64)
.await?;
let bool_tlm_handle = tlm.register::<bool>("simple_producer/bool").await;
let sin3_tlm_handle = tlm
.register("simple_producer/sin3".into(), TelemetryDataType::Float32)
.await?;
let cos3_tlm_handle = tlm
.register("simple_producer/cos3".into(), TelemetryDataType::Float64)
.await?;
let sin_handles = join_all((1..=6).map(|i| {
tlm.register::<f32>(format!(
"simple_producer/sin{}",
if i == 1 { "".into() } else { i.to_string() }
))
}))
.await;
let sin4_tlm_handle = tlm
.register("simple_producer/sin4".into(), TelemetryDataType::Float32)
.await?;
let cos4_tlm_handle = tlm
.register("simple_producer/cos4".into(), TelemetryDataType::Float64)
.await?;
let sin5_tlm_handle = tlm
.register("simple_producer/sin5".into(), TelemetryDataType::Float32)
.await?;
let cos5_tlm_handle = tlm
.register("simple_producer/cos5".into(), TelemetryDataType::Float64)
.await?;
let sin6_tlm_handle = tlm
.register("simple_producer/sin6".into(), TelemetryDataType::Float32)
.await?;
let cos6_tlm_handle = tlm
.register("simple_producer/cos6".into(), TelemetryDataType::Float64)
.await?;
let cos_handles = join_all((1..=6).map(|i| {
tlm.register::<f64>(format!(
"simple_producer/cos{}",
if i == 1 { "".into() } else { i.to_string() }
))
}))
.await;
let cancellation_token = CancellationToken::new();
{
@@ -218,85 +49,48 @@ async fn main() -> Result<(), Box<dyn Error>> {
});
}
let start_time = chrono::Utc::now();
let start_time = Utc::now();
let start_instant = Instant::now();
let mut next_time = start_instant;
let mut index = 0;
let mut tasks = vec![];
while !cancellation_token.is_cancelled() {
next_time += Duration::from_millis(10);
index += 1;
tokio::time::sleep_until(next_time).await;
let publish_time =
start_time + chrono::TimeDelta::from_std(next_time - start_instant).unwrap();
sleep_until(next_time).await;
let publish_time = start_time + TimeDelta::from_std(next_time - start_instant).unwrap();
let actual_time = Instant::now();
tasks.push(index_handle.publish(
Value::Float64((actual_time - next_time).as_secs_f64()),
chrono::Utc::now(),
));
tasks.push(sin_tlm_handle.publish(
Value::Float32((f32::TAU() * (index as f32) / (1000.0_f32)).sin()),
publish_time,
));
tasks.push(cos_tlm_handle.publish(
Value::Float64((f64::TAU() * (index as f64) / (1000.0_f64)).cos()),
publish_time,
));
tasks.push(bool_tlm_handle.publish(Value::Boolean(index % 1000 > 500), publish_time));
tasks.push(sin2_tlm_handle.publish(
Value::Float32((f32::TAU() * (index as f32) / (500.0_f32)).sin()),
publish_time,
));
tasks.push(cos2_tlm_handle.publish(
Value::Float64((f64::TAU() * (index as f64) / (500.0_f64)).cos()),
publish_time,
));
tasks.push(sin3_tlm_handle.publish(
Value::Float32((f32::TAU() * (index as f32) / (333.0_f32)).sin()),
publish_time,
));
tasks.push(cos3_tlm_handle.publish(
Value::Float64((f64::TAU() * (index as f64) / (333.0_f64)).cos()),
publish_time,
));
tasks.push(sin4_tlm_handle.publish(
Value::Float32((f32::TAU() * (index as f32) / (250.0_f32)).sin()),
publish_time,
));
tasks.push(cos4_tlm_handle.publish(
Value::Float64((f64::TAU() * (index as f64) / (250.0_f64)).cos()),
publish_time,
));
tasks.push(sin5_tlm_handle.publish(
Value::Float32((f32::TAU() * (index as f32) / (200.0_f32)).sin()),
publish_time,
));
tasks.push(cos5_tlm_handle.publish(
Value::Float64((f64::TAU() * (index as f64) / (200.0_f64)).cos()),
publish_time,
));
tasks.push(sin6_tlm_handle.publish(
Value::Float32((f32::TAU() * (index as f32) / (166.0_f32)).sin()),
publish_time,
));
tasks.push(cos6_tlm_handle.publish(
Value::Float64((f64::TAU() * (index as f64) / (166.0_f64)).cos()),
publish_time,
));
tasks.push(publish_offset.publish(
Value::Float64((Instant::now() - actual_time).as_secs_f64()),
chrono::Utc::now(),
));
// Due to how telemetry handles are implemented, unless the send buffer is full awaiting
// these will return immediately
time_offset
.publish_now((actual_time - next_time).as_secs_f64())
.await?;
bool_tlm_handle
.publish(index % 1000 > 500, publish_time)
.await?;
for task in tasks.drain(..) {
task.await?;
for (i, sin) in sin_handles.iter().enumerate() {
sin.publish(
(f32::TAU() * (index as f32) / (1000.0_f32 / (i + 1) as f32)).sin(),
publish_time,
)
.await?;
}
for (i, cos) in cos_handles.iter().enumerate() {
cos.publish(
(f64::TAU() * (index as f64) / (1000.0_f64 / (i + 1) as f64)).cos(),
publish_time,
)
.await?;
}
tasks.push(await_offset.publish(
Value::Float64((Instant::now() - actual_time).as_secs_f64()),
chrono::Utc::now(),
));
publish_offset
.publish((Instant::now() - actual_time).as_secs_f64(), Utc::now())
.await?;
await_offset
.publish((Instant::now() - actual_time).as_secs_f64(), Utc::now())
.await?;
}
Ok(())