pub struct Engine<S: MessageStore = MemStore> { /* private fields */ }Expand description
The sans-I/O OCPP protocol engine.
See the module documentation for the rules it enforces.
Implementations§
Source§impl Engine<MemStore>
impl Engine<MemStore>
Sourcepub fn new(config: EngineConfig) -> Self
pub fn new(config: EngineConfig) -> Self
An engine with an in-memory queue.
Source§impl<S: MessageStore> Engine<S>
impl<S: MessageStore> Engine<S>
Sourcepub fn with_store(config: EngineConfig, store: S) -> Result<Self, StoreError>
pub fn with_store(config: EngineConfig, store: S) -> Result<Self, StoreError>
An engine backed by a durable MessageStore.
Anything the store still holds is loaded into the queue, so a Charging Station replays the transaction messages a power cut interrupted.
Sourcepub fn require_boot(&mut self, now: Instant)
pub fn require_boot(&mut self, now: Instant)
Re-opens the boot gate, so the station registers again before ordinary traffic.
The engine keeps its boot state across reconnects (Part 4 §5.4). The exception is
B10.FR.10 (2.0.1 edition 4, 2.1 edition 2): a station that “successfully connected after
having switched to a different NetworkConnectionProfile” SHALL send a
BootNotificationRequest “even if it has not rebooted”. This returns the engine to
BootState::Idle and reports it as Output::BootState, the same signal a
Pending/Rejected retry gives; the application then sends the BootNotification.
The Tokio station calls it when it connects on a different configuration slot.
Sourcepub fn adopt_pending(&mut self, now: Instant) -> Result<usize, StoreError>
pub fn adopt_pending(&mut self, now: Instant) -> Result<usize, StoreError>
Queues what another process added to the store since this engine was built.
The engine reads its store when it is built (with_store) and
otherwise only writes it. With a store shared between processes — a station and a
metering daemon on one SQLite file — a message the other process pushes is durable at
once but would leave only on the next start. This reads MessageStore::pending again
and queues every entry whose Seq the engine does not already hold, queued or in
flight, so calling it twice queues nothing twice.
Among messages not yet sent, stored ones are ordered by Seq, so each producer’s
messages leave in the order it pushed them — what per-transaction ordering requires
(1.6 §3.7, E04.FR.01–03), since one transaction’s messages come from one producer. A
message already sent is never reordered. Adoption obeys
OfflinePolicy::max_queued: what does not fit stays in the store, still counted by
MessageStore::len, and is adopted by a later call. During a
shutdown drain nothing is adopted.
Only one engine may drain a store. A second one would adopt — and send — every entry the first holds; other processes push and never drain.
Returns how many entries were queued. The Tokio station calls this on connect and
periodically when built with store_poll.
§Errors
The store’s error from pending(). The queue is unchanged.
Sourcepub fn set_call_timeout(&mut self, timeout: Duration)
pub fn set_call_timeout(&mut self, timeout: Duration)
Changes the message timeout.
2.x sources it from the active NetworkConnectionProfile.messageTimeout, falling back
to OCPPCommCtrlr.MessageTimeout[Default] — so it changes when the station fails over
to a different network configuration slot, and it must be changeable at run time.
A call that is already outstanding keeps the deadline it was given.
Sourcepub fn set_id_generator(&mut self, ids: Box<dyn IdGenerator + Send>)
pub fn set_id_generator(&mut self, ids: Box<dyn IdGenerator + Send>)
Replaces the IdGenerator.
A Local Controller uses this to guarantee that the ids it invents cannot collide with the CSMS’s (Part 4 §6.4).
Sourcepub fn config(&self) -> &EngineConfig
pub fn config(&self) -> &EngineConfig
The configuration this engine was built with.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Whether the transport is currently up.
Sourcepub fn boot_state(&self) -> BootState
pub fn boot_state(&self) -> BootState
The boot state machine’s current state.
Sourcepub fn heartbeat_interval(&self) -> Option<Duration>
pub fn heartbeat_interval(&self) -> Option<Duration>
The heartbeat interval the CSMS asked for, once boot succeeded.
Sourcepub fn queued(&self) -> usize
pub fn queued(&self) -> usize
How many messages are waiting to be sent.
This is what GetTransactionStatus.messagesInQueue reports.
Sourcepub fn has_outstanding_call(&self) -> bool
pub fn has_outstanding_call(&self) -> bool
Whether one of our calls is currently outstanding.
Sourcepub fn awaiting_response(&self) -> usize
pub fn awaiting_response(&self) -> usize
How many of the peer’s calls are waiting for an answer.
More than one means the peer broke the one-outstanding-CALL rule and
InboundConcurrency::Serve let it.
Sourcepub fn handle(&mut self, now: Instant, input: Input<'_>)
pub fn handle(&mut self, now: Instant, input: Input<'_>)
Feeds one event into the state machine at time now.
now is the driver’s monotonic clock, taken on every input rather than only on
Input::Timeout: every input can arm a deadline, and one computed from a stale
clock fires the moment it is armed.
Sourcepub fn poll_output(&mut self) -> Option<Output>
pub fn poll_output(&mut self) -> Option<Output>
Takes the next thing the driver must do.
Sourcepub fn call(
&mut self,
now: Instant,
action: &str,
payload: Box<RawValue>,
) -> Result<CallToken, EngineError>
pub fn call( &mut self, now: Instant, action: &str, payload: Box<RawValue>, ) -> Result<CallToken, EngineError>
Starts a CALL (or a SEND, if that is what the action is).
The call is accepted immediately and reaches the wire when the one-outstanding rule,
the boot gate and the connection allow it. The returned CallToken appears again
in the matching CallOutcome.
Sourcepub fn call_with(
&mut self,
now: Instant,
action: &str,
payload: Box<RawValue>,
options: CallOptions,
) -> Result<CallToken, EngineError>
pub fn call_with( &mut self, now: Instant, action: &str, payload: Box<RawValue>, options: CallOptions, ) -> Result<CallToken, EngineError>
Starts a call with per-call overrides.
Sourcepub fn respond(
&mut self,
now: Instant,
id: &MessageId,
payload: &RawValue,
) -> Result<(), EngineError>
pub fn respond( &mut self, now: Instant, id: &MessageId, payload: &RawValue, ) -> Result<(), EngineError>
Answers an outstanding request with a CALLRESULT.
Sourcepub fn respond_error(
&mut self,
now: Instant,
id: &MessageId,
error: CallError,
) -> Result<(), EngineError>
pub fn respond_error( &mut self, now: Instant, id: &MessageId, error: CallError, ) -> Result<(), EngineError>
Answers an outstanding request with a CALLERROR.
Sourcepub fn reject_result(
&mut self,
now: Instant,
id: &MessageId,
error: CallError,
) -> Result<(), EngineError>
pub fn reject_result( &mut self, now: Instant, id: &MessageId, error: CallError, ) -> Result<(), EngineError>
Tells the peer that the CALLRESULT it sent could not be used (OCPP 2.1
CALLRESULTERROR).
Before 2.1 no such message exists, and the failure can only be logged locally.
Sourcepub fn cancel(&mut self, token: CallToken) -> bool
pub fn cancel(&mut self, token: CallToken) -> bool
Abandons a call that has not been transmitted yet.
Returns true if it was still in the queue. A call that is already outstanding
cannot be withdrawn — the peer will answer it.
Sourcepub fn shutdown(&mut self, now: Instant, deadline: Instant)
pub fn shutdown(&mut self, now: Instant, deadline: Instant)
Starts a graceful drain: no new calls are accepted, the outstanding call is allowed
to finish and the queue is flushed, then Output::Close is emitted.
If deadline passes first, the connection is closed with
CloseReason::DrainTimedOut and the queue is left in the durable store.