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:
@@ -4,11 +4,9 @@ name = "simple_command"
|
||||
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"
|
||||
log = "0.4.29"
|
||||
anyhow = "1.0.100"
|
||||
anyhow = { workspace = true }
|
||||
api = { path = "../../api" }
|
||||
env_logger = { workspace = true }
|
||||
log = { workspace = true }
|
||||
tokio = { workspace = true, features = ["rt-multi-thread", "signal"] }
|
||||
tokio-util = { workspace = true }
|
||||
|
||||
@@ -1,82 +1,16 @@
|
||||
use chrono::DateTime;
|
||||
use server::core::client_side_command::Inner;
|
||||
use server::core::command_service_client::CommandServiceClient;
|
||||
use server::core::telemetry_value::Value;
|
||||
use server::core::{
|
||||
ClientSideCommand, Command, CommandDefinitionRequest, CommandParameterDefinition,
|
||||
CommandResponse, TelemetryDataType,
|
||||
};
|
||||
use api::client::command::CommandRegistry;
|
||||
use api::client::Client;
|
||||
use api::macros::IntoCommandDefinition;
|
||||
use api::messages::command::CommandHeader;
|
||||
use log::info;
|
||||
use std::error::Error;
|
||||
use tokio::select;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::task::JoinHandle;
|
||||
use std::sync::Arc;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tonic::codegen::tokio_stream::wrappers::ReceiverStream;
|
||||
use tonic::codegen::tokio_stream::StreamExt;
|
||||
use tonic::transport::Channel;
|
||||
|
||||
struct CommandHandler {
|
||||
handle: JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl CommandHandler {
|
||||
pub async fn new<F: Fn(Command) -> CommandResponse + Send + 'static>(
|
||||
cancellation_token: CancellationToken,
|
||||
client: &mut CommandServiceClient<Channel>,
|
||||
command_definition_request: CommandDefinitionRequest,
|
||||
handler: F,
|
||||
) -> anyhow::Result<Self> {
|
||||
let (tx, rx) = mpsc::channel(4);
|
||||
|
||||
// The buffer size of 4 means this is safe to send immediately
|
||||
tx.send(ClientSideCommand {
|
||||
inner: Some(Inner::Request(command_definition_request)),
|
||||
})
|
||||
.await?;
|
||||
let response = client.new_command(ReceiverStream::new(rx)).await?;
|
||||
let mut cmd_stream = response.into_inner();
|
||||
|
||||
let handle = tokio::spawn(async move {
|
||||
loop {
|
||||
select! {
|
||||
_ = cancellation_token.cancelled() => break,
|
||||
Some(msg) = cmd_stream.next() => {
|
||||
match msg {
|
||||
Ok(cmd) => {
|
||||
let uuid = cmd.uuid.clone();
|
||||
let mut response = handler(cmd);
|
||||
response.uuid = uuid;
|
||||
match tx.send(ClientSideCommand {
|
||||
inner: Some(Inner::Response(response))
|
||||
}).await {
|
||||
Ok(()) => {},
|
||||
Err(e) => {
|
||||
println!("SendError: {e}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error: {e}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
else => break,
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(Self { handle })
|
||||
}
|
||||
|
||||
pub async fn join(self) -> anyhow::Result<()> {
|
||||
Ok(self.handle.await?)
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
|
||||
let cancellation_token = CancellationToken::new();
|
||||
{
|
||||
let cancellation_token = cancellation_token.clone();
|
||||
@@ -86,56 +20,34 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
});
|
||||
}
|
||||
|
||||
let mut client = CommandServiceClient::connect("http://[::1]:50051").await?;
|
||||
let client = Arc::new(Client::connect("ws://localhost:8080/backend")?);
|
||||
let cmd = CommandRegistry::new(client);
|
||||
|
||||
let cmd_handler = CommandHandler::new(
|
||||
cancellation_token,
|
||||
&mut client,
|
||||
CommandDefinitionRequest {
|
||||
name: "simple_command/a".to_string(),
|
||||
parameters: vec![
|
||||
CommandParameterDefinition {
|
||||
name: "a".to_string(),
|
||||
data_type: TelemetryDataType::Float32.into(),
|
||||
},
|
||||
CommandParameterDefinition {
|
||||
name: "b".to_string(),
|
||||
data_type: TelemetryDataType::Float64.into(),
|
||||
},
|
||||
CommandParameterDefinition {
|
||||
name: "c".to_string(),
|
||||
data_type: TelemetryDataType::Boolean.into(),
|
||||
},
|
||||
],
|
||||
},
|
||||
|command| {
|
||||
let timestamp = command.timestamp.expect("Missing Timestamp");
|
||||
let timestamp = DateTime::from_timestamp(timestamp.secs, timestamp.nanos as u32)
|
||||
.expect("Could not construct date time");
|
||||
let Value::Float32(a) = command.parameters["a"].value.expect("Missing Value a") else {
|
||||
panic!("Wrong Type a");
|
||||
};
|
||||
let Value::Float64(b) = command.parameters["b"].value.expect("Missing Value b") else {
|
||||
panic!("Wrong Type b");
|
||||
};
|
||||
let Value::Boolean(c) = command.parameters["c"].value.expect("Missing Value c") else {
|
||||
panic!("Wrong Type c");
|
||||
};
|
||||
let handle = cmd.register_handler("simple_command/a", handle_command);
|
||||
|
||||
println!("Command Received:\n timestamp: {timestamp}\n a: {a}\n b: {b}\n c: {c}");
|
||||
cancellation_token.cancelled().await;
|
||||
|
||||
CommandResponse {
|
||||
uuid: command.uuid.clone(),
|
||||
success: true,
|
||||
response: format!(
|
||||
"Successfully Received Command! timestamp: {timestamp} a: {a} b: {b} c: {c}"
|
||||
),
|
||||
}
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
cmd_handler.join().await?;
|
||||
// This will automatically drop when we return
|
||||
drop(handle);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(IntoCommandDefinition)]
|
||||
struct SimpleCommandA {
|
||||
a: f32,
|
||||
b: f64,
|
||||
c: bool,
|
||||
}
|
||||
|
||||
fn handle_command(header: CommandHeader, command: SimpleCommandA) -> anyhow::Result<String> {
|
||||
let timestamp = header.timestamp;
|
||||
let SimpleCommandA { a, b, c } = command;
|
||||
|
||||
info!("Command Received:\n timestamp: {timestamp}\n a: {a}\n b: {b}\n c: {c}");
|
||||
|
||||
// This gets sent back to the source of the command
|
||||
Ok(format!(
|
||||
"Successfully Received Command! timestamp: {timestamp} a: {a} b: {b} c: {c}"
|
||||
))
|
||||
}
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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(())
|
||||
|
||||
Reference in New Issue
Block a user