pub struct NetworkEventBus {
pub config: EventBusConfig,
pub subscriptions: HashMap<u64, NebSubscription>,
pub replay_buffer: VecDeque<NebNetworkEvent>,
pub next_event_id: u64,
pub next_sub_id: u64,
pub total_published: u64,
pub total_delivered: u64,
}Expand description
Synchronous, in-process publish-subscribe bus for network events.
§Thread safety
NetworkEventBus is intentionally not Send or Sync. If you need
to share it across threads, wrap it in Arc<Mutex<NetworkEventBus>>.
§Example
use ipfrs_network::{
NebNetworkEvent, NebSubscription,
EventBusConfig, EventFilter, EventTopic, NetworkEventBus,
};
let mut bus = NetworkEventBus::new(EventBusConfig::default());
// Subscribe to peer-connection events only.
let sub_id = bus.subscribe(EventFilter::TopicIn(vec![EventTopic::PeerConnected]));
// Publish an event.
let _event_id = bus
.publish(
EventTopic::PeerConnected,
b"hello".to_vec(),
Some("peer-abc".to_string()),
1_000,
)
.expect("publish failed");
// Retrieve matching events for the subscriber.
let events = bus.drain_events_for(sub_id).expect("drain failed");
assert_eq!(events.len(), 1);Fields§
§config: EventBusConfigConfiguration that governs limits and buffer sizes.
subscriptions: HashMap<u64, NebSubscription>Live subscriptions, keyed by their numeric id.
replay_buffer: VecDeque<NebNetworkEvent>Ring buffer of recent events available for replay.
next_event_id: u64Counter used to assign unique ids to new events.
next_sub_id: u64Counter used to assign unique ids to new subscriptions.
total_published: u64Total number of events that have been successfully published.
total_delivered: u64Total number of (subscriber, event) delivery pairs.
Implementations§
Source§impl NetworkEventBus
impl NetworkEventBus
Sourcepub fn new(config: EventBusConfig) -> Self
pub fn new(config: EventBusConfig) -> Self
Create a new bus with the supplied configuration.
Sourcepub fn matches_filter(filter: &EventFilter, event: &NebNetworkEvent) -> bool
pub fn matches_filter(filter: &EventFilter, event: &NebNetworkEvent) -> bool
Returns true if event satisfies filter.
Sourcepub fn subscribe(&mut self, filter: EventFilter) -> SubscriberId
pub fn subscribe(&mut self, filter: EventFilter) -> SubscriberId
Register a new subscriber with the given filter.
Returns the assigned SubscriberId. If the bus is already at
capacity, SubscriberId(0) is returned as a sentinel (no subscription
is created).
Sourcepub fn unsubscribe(&mut self, id: SubscriberId) -> bool
pub fn unsubscribe(&mut self, id: SubscriberId) -> bool
Remove the subscription identified by id.
Returns true if the subscription existed and was removed, false if
no subscription with that id was found.
Sourcepub fn subscriber_stats(&self, id: SubscriberId) -> Option<&NebSubscription>
pub fn subscriber_stats(&self, id: SubscriberId) -> Option<&NebSubscription>
Return an immutable reference to the NebSubscription for id, or
None if no such subscription exists.
Sourcepub fn publish(
&mut self,
topic: EventTopic,
payload: Vec<u8>,
source_peer: Option<String>,
now: u64,
) -> Result<u64, BusError>
pub fn publish( &mut self, topic: EventTopic, payload: Vec<u8>, source_peer: Option<String>, now: u64, ) -> Result<u64, BusError>
Publish an event to the bus.
§Errors
BusError::PayloadTooLarge–payload.len() > config.max_payload_bytes.
§Returns
The assigned event id (monotonically increasing).
Sourcepub fn replay_for_subscriber(
&self,
id: SubscriberId,
since_event_id: u64,
) -> Result<Vec<&NebNetworkEvent>, BusError>
pub fn replay_for_subscriber( &self, id: SubscriberId, since_event_id: u64, ) -> Result<Vec<&NebNetworkEvent>, BusError>
Return all buffered events with id > since_event_id that match the
subscriber’s filter.
§Errors
BusError::SubscriberNotFoundifiddoes not correspond to an active subscription.
Sourcepub fn drain_events_for(
&mut self,
id: SubscriberId,
) -> Result<Vec<NebNetworkEvent>, BusError>
pub fn drain_events_for( &mut self, id: SubscriberId, ) -> Result<Vec<NebNetworkEvent>, BusError>
Clone all buffered events that match the subscriber’s filter and return them. The replay buffer is not modified.
§Errors
BusError::SubscriberNotFoundifiddoes not correspond to an active subscription.
Sourcepub fn event_bus_stats(&self) -> EventBusStats
pub fn event_bus_stats(&self) -> EventBusStats
Return a snapshot of bus-wide statistics.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for NetworkEventBus
impl RefUnwindSafe for NetworkEventBus
impl Send for NetworkEventBus
impl Sync for NetworkEventBus
impl Unpin for NetworkEventBus
impl UnsafeUnpin for NetworkEventBus
impl UnwindSafe for NetworkEventBus
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more