pub struct PkCommand<VA, MA>where
VA: PkVariableAccessor,
MA: PkMethodAccessor,{ /* 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>
impl<VA: PkVariableAccessor, MA: PkMethodAccessor> PkCommand<VA, MA>
Sourcepub fn incoming_command(
&self,
command_bytes: Vec<u8>,
) -> Result<(), &'static str>
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: AVec<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.
Sourcepub fn poll(&self) -> Option<Command>
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.
Sourcepub fn perform(
&self,
operation: Operation,
object: Option<String>,
data: Option<Vec<u8>>,
) -> Result<(), &'static str>
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 rootOperationto perform (e.g.,SENDV,REQUV,INVOK,PKVER).object: An optionalStringrepresenting the object of the operation (e.g., variable name, method name).data: OptionalVec<u8>containing parameter data for the operation (e.g., forSENDVorINVOK).
§Returns
Ok(()) if the operation can be initiated, or Err(&'static str) if not (e.g., not idle, or not a root operation).
Sourcepub fn is_complete(&self) -> bool
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.
Sourcepub fn get_return_data(&self) -> Option<Vec<u8>>
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.
Sourcepub fn wait_for_complete_and<F>(&self, callback: F) -> bool
pub fn wait_for_complete_and<F>(&self, callback: F) -> bool
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 anOption<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.
Sourcepub fn new(
config: PkCommandConfig,
variable_accessor: VA,
method_accessor: MA,
) -> Self
pub fn new( config: PkCommandConfig, variable_accessor: VA, method_accessor: MA, ) -> Self
Creates a new PkCommand state machine instance.
§Arguments
config: ThePkCommandConfigto use.variable_accessor: An implementation ofPkVariableAccessorfor variable operations.method_accessor: An implementation ofPkMethodAccessorfor method invocation.