Struct PkCommand

Source
pub struct PkCommand<VA, MA>{ /* private fields */ }
Expand description

The main state machine for handling the PK Command protocol.

It manages transaction states, command parsing, command generation, acknowledgments, timeouts, and data slicing.

Implementations§

Source§

impl<VA: PkVariableAccessor, MA: PkMethodAccessor> PkCommand<VA, MA>

Source

pub fn incoming_command( &self, command_bytes: Vec<u8>, ) -> Result<(), &'static str>

Ingests a raw command received from the other party.

The command bytes are parsed, and if successful, the parsed Command is stored in an internal buffer to be processed by the next call to poll().

§Arguments
  • command_bytes: A Vec<u8> containing the raw bytes of the received command.
§Returns

Ok(()) if the command was successfully parsed and buffered. Err(&'static str) if parsing failed.

Source

pub fn poll(&self) -> Option<Command>

Polls the state machine for actions.

This method should be called periodically. It processes incoming commands from the internal buffer (filled by incoming_command), handles timeouts, manages retransmissions, and progresses through the transaction stages.

If the state machine determines that a command needs to be sent to the other party, this method will return Some(Command).

§Returns

Some(Command) if a command needs to be sent, or None otherwise.

Source

pub fn perform( &self, operation: Operation, object: Option<String>, data: Option<Vec<u8>>, ) -> Result<(), &'static str>

Initiates a new root operation from the Host side.

This method should only be called when the PkCommand instance is in an Idle state. It sets up the necessary internal state to begin a new transaction. The actual START command and subsequent root operation command will be generated by subsequent calls to poll().

§Arguments
  • operation: The root Operation to perform (e.g., SENDV, REQUV, INVOK, PKVER).
  • object: An optional String representing the object of the operation (e.g., variable name, method name).
  • data: Optional Vec<u8> containing parameter data for the operation (e.g., for SENDV or INVOK).
§Returns

Ok(()) if the operation can be initiated, or Err(&'static str) if not (e.g., not idle, or not a root operation).

Source

pub fn is_complete(&self) -> bool

Checks if the transaction is complete (i.e., the state machine is in the Idle stage).

§Returns

true if the transaction is complete, false otherwise.

Source

pub fn get_return_data(&self) -> Option<Vec<u8>>

Retrieves the return data from the completed transaction.

This method should only be called when is_complete() returns true and the instance is acting as the Host.

§Returns

Some(Vec<u8>) containing the return data if available, or None if there was no return data.

Source

pub fn wait_for_complete_and<F>(&self, callback: F) -> bool
where F: FnOnce(Option<Vec<u8>>),

Waits for the transaction to complete and then executes a callback with the return data.

This is a polling-based wait. The callback is only executed once the state machine enters the Idle stage.

§Arguments
  • callback: A closure that takes an Option<Vec<u8>> (the return data) and is executed upon completion.
§Returns

true if the callback was executed, or false otherwise

§Note

This method assumes poll() is being called externally to drive the state machine. It does not block the current thread waiting for completion, but rather checks the state and executes the callback if complete. You must ensure poll() is called frequently for the transaction to progress.

Source

pub fn new( config: PkCommandConfig, variable_accessor: VA, method_accessor: MA, ) -> Self

Creates a new PkCommand state machine instance.

§Arguments
  • config: The PkCommandConfig to use.
  • variable_accessor: An implementation of PkVariableAccessor for variable operations.
  • method_accessor: An implementation of PkMethodAccessor for method invocation.

Auto Trait Implementations§

§

impl<VA, MA> !Freeze for PkCommand<VA, MA>

§

impl<VA, MA> !RefUnwindSafe for PkCommand<VA, MA>

§

impl<VA, MA> !Send for PkCommand<VA, MA>

§

impl<VA, MA> !Sync for PkCommand<VA, MA>

§

impl<VA, MA> Unpin for PkCommand<VA, MA>
where VA: Unpin, MA: Unpin,

§

impl<VA, MA> !UnwindSafe for PkCommand<VA, MA>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.