Trait nakamoto_net::StateMachine
source · pub trait StateMachine<Id: PeerId = SocketAddr>: Iterator<Item = Io<<Self::Message as ToOwned>::Owned, Self::Event, Self::DisconnectReason, Id>> {
type Message: Debug + ToOwned + ?Sized;
type Event: Debug;
type DisconnectReason: Debug + Display + Into<Disconnect<Self::DisconnectReason>>;
fn message_received(&mut self, addr: &Id, message: Cow<'_, Self::Message>);
fn attempted(&mut self, addr: &Id);
fn connected(&mut self, addr: Id, local_addr: &SocketAddr, link: Link);
fn disconnected(
&mut self,
addr: &Id,
reason: Disconnect<Self::DisconnectReason>
);
fn tick(&mut self, local_time: LocalTime);
fn timer_expired(&mut self);
fn initialize(&mut self, _time: LocalTime) { ... }
}
Expand description
A service state-machine to implement a network protocol’s logic.
This trait defines an API for connecting specific protocol domain logic to a
Reactor
. It is parametrized by a peer id, which is shared between the reactor
and state machine.
The state machine emits Io
instructions to the reactor via its Iterator
trait.
Required Associated Types
sourcetype Event: Debug
type Event: Debug
Events emitted by the state machine. These are forwarded by the reactor to the user thread.
sourcetype DisconnectReason: Debug + Display + Into<Disconnect<Self::DisconnectReason>>
type DisconnectReason: Debug + Display + Into<Disconnect<Self::DisconnectReason>>
Reason a peer was disconnected, in case the peer was disconnected by the internal state-machine logic.
Required Methods
sourcefn message_received(&mut self, addr: &Id, message: Cow<'_, Self::Message>)
fn message_received(&mut self, addr: &Id, message: Cow<'_, Self::Message>)
Called by the reactor upon receiving a message from a remote peer.
sourcefn attempted(&mut self, addr: &Id)
fn attempted(&mut self, addr: &Id)
Connection attempt underway.
This is only encountered when an outgoing connection attempt is made,
and is always called before StateMachine::connected
.
For incoming connections, StateMachine::connected
is called directly.
sourcefn connected(&mut self, addr: Id, local_addr: &SocketAddr, link: Link)
fn connected(&mut self, addr: Id, local_addr: &SocketAddr, link: Link)
New connection with a peer.
sourcefn disconnected(&mut self, addr: &Id, reason: Disconnect<Self::DisconnectReason>)
fn disconnected(&mut self, addr: &Id, reason: Disconnect<Self::DisconnectReason>)
Called whenever a remote peer was disconnected, either because of a
network-related event or due to a local instruction from this state machine,
using Io::Disconnect
.
sourcefn tick(&mut self, local_time: LocalTime)
fn tick(&mut self, local_time: LocalTime)
Called by the reactor every time the event loop gets data from the network, or times out. Used to update the state machine’s internal clock.
“a regular short, sharp sound, especially that made by a clock or watch, typically every second.”
sourcefn timer_expired(&mut self)
fn timer_expired(&mut self)
A timer set with Io::SetTimer
has expired.
Provided Methods
sourcefn initialize(&mut self, _time: LocalTime)
fn initialize(&mut self, _time: LocalTime)
Initialize the state machine. Called once before any event is sent to the state machine.