microrti_client/lib.rs
1//! Federate side of HLA 4 (IEEE Std 1516.1-2025): the RTI ambassador and the federate ambassador in Rust.
2//!
3//! * [`RtiAmbassador`] offers every federate-initiated service of clauses 4 to 10 as a typed, blocking
4//! method. The methods are named after the `CallRequest` alternatives of the Federate Protocol
5//! (12.13.3): `UpdateAttributeValuesWithTimeRequest` is [`RtiAmbassador::update_attribute_values_with_time`].
6//! * [`FederateAmbassador`] is the trait a federate implements to receive the RTI-initiated services, one
7//! method per `CallbackRequest` alternative, all with empty default bodies. Federates that prefer a
8//! `match` handle the [`Callback`] enum instead, through [`CallbackFn`].
9//! * [`RtiLink`] is the seam to the RTI: anything that can exchange the service messages, such as a
10//! Federate Protocol session or an in-process RTI ([`local`]). [`Connectors`] opens links by RTI address.
11//!
12//! # Links
13//!
14//! | Cargo feature | Module | Link |
15//! | --- | --- | --- |
16//! | `fedpro` (default) | `fedpro` | a Federate Protocol session (12.13) to any HLA 4 RTI; [`RtiAmbassador::new`] uses it for the RTI addresses `tcp://host:port`, `tls://`, `ws://host:port/path`, `wss://` and bare `host[:port]` |
17//! | `embedded` | `embedded` | an RTI inside the federate's process, shared by any number of ambassadors |
18//! | `test-util` | `testing` | a scripted in-memory link for tests |
19//!
20//! Handles are opaque byte strings ([`ObjectClassHandle`] and friends), sets and maps are standard
21//! collections, logical times are [`LogicalTime`] / [`LogicalTimeInterval`] values in the representation
22//! of the joined federation execution ([`RtiAmbassador::time_kind`]). Every service returns
23//! [`RtiResult`]; the error names the standard exception ([`ExceptionKind`]).
24//!
25//! Both callback models are implemented here, as 12.13.3.2 requires of a Federate Protocol client:
26//! with [`CallbackModel::Evoked`] callbacks wait for [`RtiAmbassador::evoke_callback`] or
27//! [`RtiAmbassador::evoke_multiple_callbacks`], with [`CallbackModel::Immediate`] a thread of the
28//! ambassador delivers them as they arrive. Callbacks of one federate never run concurrently, and they may
29//! invoke any service except the six listed in 12.8.
30//!
31//! # Generated code
32//!
33//! `src/services.rs`, `src/callback.rs`, `src/testing/replies.rs` and the two `tests/generated_*.rs` files
34//! are written by `tools/gen_client.py` from the protobuf files in `proto/`. Everything with behaviour of
35//! its own (connection handling, callback delivery, conversions) is written by hand.
36//!
37//! # Example
38//!
39//! The example runs against the scripted link of the `test-util` feature, which answers every call with
40//! an empty success response; with a real link the same code talks to an RTI.
41//!
42//! ```
43//! use std::time::Duration;
44//!
45//! use microrti_client::testing::MockLink;
46//! use microrti_client::{
47//! AttributeHandleSet, AttributeHandleValueMap, Bytes, Callback, CallbackFn, CallbackModel, FomModule,
48//! ResignAction, RtiAmbassador, RtiResult,
49//! };
50//!
51//! fn main() -> RtiResult<()> {
52//! let link = MockLink::new();
53//! let rti = RtiAmbassador::with_link(link.clone());
54//!
55//! // The federate ambassador: here a closure matching on the callbacks it cares about.
56//! let federate = CallbackFn::new(|callback: Callback| {
57//! if let Callback::DiscoverObjectInstance { object_instance_name, .. } = callback {
58//! println!("discovered {object_instance_name}");
59//! }
60//! Ok(())
61//! });
62//! rti.connect(federate, CallbackModel::Evoked)?;
63//!
64//! let fom = FomModule::content("Restaurant.xml", "<objectModel/>");
65//! rti.create_federation_execution("Lunch", &fom)?;
66//! let joined = rti.join_federation_execution("Chef", "Lunch")?;
67//! println!("joined as {} using {}", joined.federate_handle, rti.time_kind()?);
68//!
69//! let food = rti.get_object_class_handle("HLAobjectRoot.Food")?;
70//! let name = rti.get_attribute_handle(&food, "Name")?;
71//! let attributes = AttributeHandleSet::from([name.clone()]);
72//! rti.publish_object_class_attributes(&food, &attributes)?;
73//! rti.subscribe_object_class_attributes(&food, &attributes)?;
74//!
75//! let soup = rti.register_object_instance(&food)?;
76//! let values = AttributeHandleValueMap::from([(name, Bytes::from_static(b"soup"))]);
77//! rti.update_attribute_values(&soup, &values, b"lunch time")?;
78//!
79//! // EVOKED model: callbacks are delivered here, on this thread.
80//! rti.evoke_multiple_callbacks(Duration::ZERO, Duration::from_millis(10))?;
81//!
82//! rti.resign_federation_execution(ResignAction::DeleteObjectsThenDivest)?;
83//! rti.destroy_federation_execution("Lunch")?;
84//! rti.disconnect()?;
85//! assert!(link.is_closed());
86//! Ok(())
87//! }
88//! ```
89#![warn(missing_docs)]
90
91mod ambassador;
92mod callback;
93#[cfg(feature = "embedded")]
94pub mod embedded;
95mod federate;
96#[cfg(feature = "fedpro")]
97pub mod fedpro;
98mod handles;
99mod link;
100pub mod local;
101mod services;
102#[cfg(feature = "test-util")]
103pub mod testing;
104mod types;
105mod wire;
106
107pub use ambassador::{RtiAmbassador, WeakRtiAmbassador};
108pub use bytes::Bytes;
109pub use callback::{Callback, FederateAmbassador};
110pub use federate::{CallbackFn, CallbackResult, FederateInternalError, NullFederateAmbassador};
111pub use handles::{
112 AttributeHandle, DimensionHandle, FederateHandle, InteractionClassHandle, MessageRetractionHandle,
113 ObjectClassHandle, ObjectInstanceHandle, ParameterHandle, RegionHandle, TransportationTypeHandle,
114};
115pub use link::{
116 Connectors, LinkCallback, LinkError, LinkFactory, LinkState, RtiLink, global_connectors,
117 register_connector, register_default_connector,
118};
119pub use microrti_core::{
120 ExceptionKind, HLA_VERSION, LogicalTime, LogicalTimeInterval, RtiError, RtiResult, TimeKind,
121};
122/// The Federate Protocol message types (Annex D), as exchanged over an [`RtiLink`].
123pub use microrti_proto as proto;
124pub use services::request_name;
125pub use types::{
126 AdditionalSettingsResultCode, AttributeHandleSet, AttributeHandleValueMap, AttributeSetRegionSetPair,
127 AttributeSetRegionSetPairList, CallbackModel, ConfigurationResult, ConveyedRegion, ConveyedRegionSet,
128 Credentials, DimensionHandleSet, FederateHandleSaveStatusPair, FederateHandleSet, FederateRestoreStatus,
129 FederationExecutionInformation, FederationExecutionMemberInformation, FomModule,
130 InteractionClassHandleSet, JoinResult, MessageRetractionReturn, OrderType, ParameterHandleValueMap,
131 RangeBounds, RegionHandleSet, ResignAction, RestoreFailureReason, RestoreStatus, RtiConfiguration,
132 SaveFailureReason, SaveStatus, ServiceGroup, SupplementalReceiveInfo, SupplementalReflectInfo,
133 SynchronizationPointFailureReason, TimeQueryReturn, decode_hla_unicode_string, encode_hla_unicode_string,
134};