Expand description
Sans-io core trait and test harness: protocol logic with no sockets, timers or async runtime.
A broker connector is split in two. The protocol layer decides what a frame means and what to send next. The runtime owns the sockets and the clock. This crate is the seam between them:
BrokerCoreis the trait a protocol core implements. Frames and the current time go in. Events (EventSink), outbound messages (Outbound) and the next wake-up time come out.Harnessdrives a core with recorded frames and virtual time, so a protocol can be tested without a network and without waiting.- Helpers every core needs:
JsonFrameBuffer(reassemble JSON split over frames),SubscriptionSet(the wanted subscriptions),OrderFsmandOrderRegistry(the life of an order, including theUnknownoutcome).
The crate does no I/O at all, and the examples in it run with no mock. The connectors that
use it run against the broker mocks, see brokapi-mocks.
The book is at https://brokapi.ru/docs/.
§Examples
Reassemble a JSON message that arrived in two pieces, then track an order through its life:
use std::time::Instant;
use brokapi_core::{BrokerOrderId, ClientOrderId, OrderState};
use brokapi_sansio::{BufferPush, JsonFrameBuffer, OrderFsm, OrderSignal};
let mut buf = JsonFrameBuffer::new();
assert_eq!(buf.push(r#"{"type":"order","#), BufferPush::Incomplete);
let BufferPush::Complete(frames) = buf.push(r#""id":"42"}"#) else { panic!("expected a frame") };
assert_eq!(frames, vec![r#"{"type":"order","id":"42"}"#.to_string()]);
let mut order = OrderFsm::new(ClientOrderId::new_v4(), Instant::now());
order.apply(OrderSignal::Ack { broker_id: BrokerOrderId::new("42") }, Instant::now())?;
assert!(matches!(order.state(), OrderState::Acked { .. }));Structs§
- Core
Event - An event produced by a sans-io core, before it is wrapped in an
Envelope. - Desired
Subscription - One wanted subscription: what to follow, under which id.
- Event
Sink - A list the core pushes events into while it handles a frame or a timeout.
- Frame
Record - One inbound frame of a recorded session, with the instant it arrived.
- Harness
- Drives a
BrokerCorewith virtual time and records what comes out. - Json
Frame Buffer - Puts the pieces of a JSON stream back together and cuts it into whole top-level values.
- Order
Fsm - The life of one order as a finite state machine over [
OrderState]. - Order
Registry - Many
OrderFsms keyed by client order id. - Subscription
Set - The full set of subscriptions the consumer wants: the input of
BrokerCore::apply.
Enums§
- Buffer
Push - Result of feeding a chunk into
JsonFrameBuffer. - FsmError
- Why a signal could not be applied.
- Order
Signal - Something that happened to an order, fed to
OrderFsm::apply. - Outbound
- A wire message a core wants sent, returned by
BrokerCore::poll_transmit. - Subscription
Kind - Kind of streaming subscription the connector should maintain.
Traits§
- Broker
Core - Protocol logic of one broker stream, written without sockets, timers or an async runtime.