pub struct NetworkEventBus { /* private fields */ }Expand description
Synchronous in-process publish-subscribe bus for NetworkEvents.
§Example
use ipfrs_network::event_bus::{NetworkEventBus, NetworkEvent, EventFilter};
let mut bus = NetworkEventBus::new();
let id = bus.subscribe(EventFilter::PeerEvents);
bus.publish(NetworkEvent::PeerConnected {
peer_id: "peer1".into(),
address: "/ip4/1.2.3.4/tcp/4001".into(),
});
let events = bus.drain(id);
assert_eq!(events.len(), 1);Implementations§
Source§impl NetworkEventBus
impl NetworkEventBus
Sourcepub fn subscribe(&mut self, filter: EventFilter) -> u64
pub fn subscribe(&mut self, filter: EventFilter) -> u64
Subscribe to events matching filter.
Returns a unique subscription id that can be used to drain events or unsubscribe later.
Sourcepub fn unsubscribe(&mut self, id: u64) -> bool
pub fn unsubscribe(&mut self, id: u64) -> bool
Remove the subscription identified by id.
Returns true if the subscription existed, false otherwise.
Sourcepub fn publish(&mut self, event: NetworkEvent)
pub fn publish(&mut self, event: NetworkEvent)
Publish an event to all matching subscribers.
Each matching subscriber receives its own clone of event. If a
subscriber’s queue is full (>= 200 entries) the event is dropped and
both the per-subscription and global drop counters are incremented.
Sourcepub fn drain(&mut self, id: u64) -> Vec<NetworkEvent>
pub fn drain(&mut self, id: u64) -> Vec<NetworkEvent>
Drain all buffered events for the subscription identified by id.
Returns an empty Vec if the id does not exist.
Sourcepub fn peek_count(&self, id: u64) -> usize
pub fn peek_count(&self, id: u64) -> usize
Return the number of events currently buffered for subscription id.
Returns 0 for unknown ids.
Sourcepub fn clear_all_queues(&mut self)
pub fn clear_all_queues(&mut self)
Drain all queues for all active subscriptions, discarding pending events.
Trait Implementations§
Source§impl Debug for NetworkEventBus
impl Debug for NetworkEventBus
Source§impl Default for NetworkEventBus
impl Default for NetworkEventBus
Source§fn default() -> NetworkEventBus
fn default() -> NetworkEventBus
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