use crate::{address::Address, index::OpRef, peer::Peer, peer_id::PeerId};
pub struct Step<P: OverlayProtocol + ?Sized> {
pub events: Vec<P::Event>,
pub messages: Vec<OutMessage<P>>,
}
impl<P: OverlayProtocol + ?Sized> Step<P> {
pub fn new() -> Self {
Step {
events: Vec::new(),
messages: Vec::new(),
}
}
pub fn with_event(mut self, event: P::Event) -> Self {
self.events.push(event);
self
}
pub fn with_message(mut self, message: OutMessage<P>) -> Self {
self.messages.push(message);
self
}
pub fn is_empty(&self) -> bool {
self.events.is_empty() && self.messages.is_empty()
}
pub fn extend(&mut self, other: Step<P>) {
self.events.extend(other.events);
self.messages.extend(other.messages);
}
}
impl<P: OverlayProtocol + ?Sized> Default for Step<P> {
fn default() -> Self {
Self::new()
}
}
pub struct OutMessage<P: OverlayProtocol + ?Sized> {
pub destination: Peer<P::Address>,
pub message: P::Message,
}
pub trait OverlayProtocol {
type Address: Address;
type Message;
type Event;
type Peer: Clone;
type BootstrapConfig;
type BootstrapRef<'a>: OpRef
where
Self: 'a;
const PROTOCOL_ID: &'static str;
fn poll(&mut self) -> Step<Self>;
fn on_message(&mut self, from: Peer<Self::Address>, msg: Self::Message) -> Step<Self>;
fn local_peer_id(&self) -> &PeerId;
fn bootstrap(&mut self, config: Self::BootstrapConfig) -> Self::BootstrapRef<'_>;
fn on_connection_failed(&mut self, peer_id: &PeerId) -> Step<Self>;
fn add_peer(&mut self, peer: Self::Peer) -> Step<Self>;
fn remove_peer(&mut self, peer_id: &PeerId) -> Option<Self::Peer>;
fn add_address(&mut self, peer_id: &PeerId, address: Self::Address);
fn remove_address(&mut self, peer_id: &PeerId, address: &Self::Address) -> Option<Self::Peer>;
}