1
0
forked from ROMEO/nexosim

Simplify gRPC bench API

This commit is contained in:
Serge Barral 2024-11-16 20:22:56 +01:00
parent 0a10f256bc
commit f89624daf0
4 changed files with 34 additions and 20 deletions

View File

@ -8,7 +8,7 @@ use serde::de::DeserializeOwned;
use tonic::{transport::Server, Request, Response, Status};
use crate::registry::EndpointRegistry;
use crate::simulation::{Scheduler, Simulation, SimulationError};
use crate::simulation::{Simulation, SimulationError};
use super::codegen::simulation::*;
use super::key_registry::KeyRegistry;
@ -19,13 +19,11 @@ use super::services::{ControllerService, MonitorService};
///
/// The first argument is a closure that takes an initialization configuration
/// and is called every time the simulation is (re)started by the remote client.
/// It must create a new simulation and its scheduler, complemented by a
/// registry that exposes the public event and query interface.
/// It must create a new simulation, complemented by a registry that exposes the
/// public event and query interface.
pub fn run<F, I>(sim_gen: F, addr: SocketAddr) -> Result<(), Box<dyn std::error::Error>>
where
F: FnMut(I) -> Result<(Simulation, Scheduler, EndpointRegistry), SimulationError>
+ Send
+ 'static,
F: FnMut(I) -> Result<(Simulation, EndpointRegistry), SimulationError> + Send + 'static,
I: DeserializeOwned,
{
run_service(GrpcSimulationService::new(sim_gen), addr)
@ -67,13 +65,11 @@ impl GrpcSimulationService {
///
/// The argument is a closure that takes an initialization configuration and
/// is called every time the simulation is (re)started by the remote client.
/// It must create a new simulation and its scheduler, complemented by a
/// registry that exposes the public event and query interface.
/// It must create a new simulation, complemented by a registry that exposes
/// the public event and query interface.
pub(crate) fn new<F, I>(sim_gen: F) -> Self
where
F: FnMut(I) -> Result<(Simulation, Scheduler, EndpointRegistry), SimulationError>
+ Send
+ 'static,
F: FnMut(I) -> Result<(Simulation, EndpointRegistry), SimulationError> + Send + 'static,
I: DeserializeOwned,
{
Self {

View File

@ -8,7 +8,7 @@ use super::{map_simulation_error, to_error};
use super::super::codegen::simulation::*;
type InitResult = Result<(Simulation, Scheduler, EndpointRegistry), SimulationError>;
type InitResult = Result<(Simulation, EndpointRegistry), SimulationError>;
type DeserializationError = ciborium::de::Error<std::io::Error>;
type SimGen = Box<dyn FnMut(&[u8]) -> Result<InitResult, DeserializationError> + Send + 'static>;
@ -27,14 +27,11 @@ impl InitService {
///
/// The argument is a closure that takes a CBOR-serialized initialization
/// configuration and is called every time the simulation is (re)started by
/// the remote client. It must create a new simulation and its scheduler,
/// complemented by a registry that exposes the public event and query
/// interface.
/// the remote client. It must create a new simulation complemented by a
/// registry that exposes the public event and query interface.
pub(crate) fn new<F, I>(mut sim_gen: F) -> Self
where
F: FnMut(I) -> Result<(Simulation, Scheduler, EndpointRegistry), SimulationError>
+ Send
+ 'static,
F: FnMut(I) -> Result<(Simulation, EndpointRegistry), SimulationError> + Send + 'static,
I: DeserializeOwned,
{
// Wrap `sim_gen` so it accepts a serialized init configuration.
@ -67,7 +64,13 @@ impl InitService {
.and_then(|init_result| init_result.map_err(map_simulation_error));
let (reply, bench) = match reply {
Ok(bench) => (init_reply::Result::Empty(()), Some(bench)),
Ok((simulation, registry)) => {
let scheduler = simulation.scheduler();
(
init_reply::Result::Empty(()),
Some((simulation, scheduler, registry)),
)
}
Err(e) => (init_reply::Result::Error(e), None),
};

View File

@ -394,11 +394,20 @@
//!
//! ```toml
//! [dependencies]
//! nexosim = { version = "0.3", features = ["tracing"] }
//! nexosim = { version = "0.3.0-beta.0", features = ["tracing"] }
//! ```
//!
//! See the [`tracing`] module for more information.
//!
//! ## gRPC server
//!
//! The `grpc` feature provides a gRPC server for remote control and monitoring,
//! e.g. from a Python client. It can be activated with:
//!
//! ```toml
//! [dependencies]
//! nexosim = { version = "0.3.0-beta.0", features = ["grpc"] }
//! ```
//!
//! # Other resources
//!

View File

@ -494,6 +494,12 @@ impl Simulation {
}
}
}
/// Returns a scheduler handle.
#[cfg(feature = "grpc")]
pub(crate) fn scheduler(&self) -> Scheduler {
Scheduler::new(self.scheduler_queue.clone(), self.time.reader())
}
}
impl fmt::Debug for Simulation {