pub struct EventBus { /* private fields */ }Expand description
Event bus for publishing and subscribing to node events
The EventBus is the central coordination point for the event system. It allows
components to publish events and provides two mechanisms for subscribing to events:
- Callback-based: Register an
EventSubscriberto receive events via callbacks - Channel-based: Get a
broadcast::Receiver<NodeEvent>for async event processing
§Thread Safety
The EventBus is designed to be thread-safe, with all mutable state protected
by appropriate synchronization primitives. It can be safely shared across threads
using Arc<EventBus>.
§Example Usage
use std::sync::Arc;
use tap_node::event::{EventBus, NodeEvent};
async fn example() {
// Create a new event bus
let event_bus = Arc::new(EventBus::new());
// Subscribe to events using a channel
let mut receiver = event_bus.subscribe_channel();
// Publish an event using public methods
let did = "did:example:123".to_string();
event_bus.publish_agent_registered(did).await;
// Process events from the channel
tokio::spawn(async move {
while let Ok(event) = receiver.recv().await {
println!("Received event: {:?}", event);
}
});
}Implementations§
Source§impl EventBus
impl EventBus
Sourcepub async fn subscribe(&self, subscriber: Arc<dyn EventSubscriber>)
pub async fn subscribe(&self, subscriber: Arc<dyn EventSubscriber>)
Subscribe to node events
Sourcepub fn subscribe_channel(&self) -> Receiver<NodeEvent>
pub fn subscribe_channel(&self) -> Receiver<NodeEvent>
Get a receiver for node events
Sourcepub async fn unsubscribe(&self, subscriber: &Arc<dyn EventSubscriber>)
pub async fn unsubscribe(&self, subscriber: &Arc<dyn EventSubscriber>)
Remove a subscriber from the event bus
Sourcepub async fn publish_message_received(&self, message: PlainMessage)
pub async fn publish_message_received(&self, message: PlainMessage)
Publish a message received event
Sourcepub async fn publish_message_sent(
&self,
message: PlainMessage,
from: String,
to: String,
)
pub async fn publish_message_sent( &self, message: PlainMessage, from: String, to: String, )
Publish a message sent event
Sourcepub async fn publish_agent_registered(&self, did: String)
pub async fn publish_agent_registered(&self, did: String)
Publish an agent registered event
Sourcepub async fn publish_agent_unregistered(&self, did: String)
pub async fn publish_agent_unregistered(&self, did: String)
Publish an agent unregistered event
Sourcepub async fn publish_agent_message(&self, did: String, message: Vec<u8>)
pub async fn publish_agent_message(&self, did: String, message: Vec<u8>)
Publish an agent message event
Sourcepub async fn publish_did_resolved(&self, did: String, success: bool)
pub async fn publish_did_resolved(&self, did: String, success: bool)
Publish a DID resolved event
Sourcepub async fn publish_message_rejected(
&self,
message_id: String,
reason: String,
from: String,
to: String,
)
pub async fn publish_message_rejected( &self, message_id: String, reason: String, from: String, to: String, )
Publish a message rejected event
Sourcepub async fn publish_message_accepted(
&self,
message_id: String,
message_type: String,
from: String,
to: String,
)
pub async fn publish_message_accepted( &self, message_id: String, message_type: String, from: String, to: String, )
Publish a message accepted event
Sourcepub async fn publish_reply_received(
&self,
original_message_id: String,
reply_message: PlainMessage,
original_message: PlainMessage,
)
pub async fn publish_reply_received( &self, original_message_id: String, reply_message: PlainMessage, original_message: PlainMessage, )
Publish a reply received event
Sourcepub async fn publish_transaction_state_changed(
&self,
transaction_id: String,
old_state: String,
new_state: String,
agent_did: Option<String>,
)
pub async fn publish_transaction_state_changed( &self, transaction_id: String, old_state: String, new_state: String, agent_did: Option<String>, )
Publish a transaction state changed event
Sourcepub async fn publish_decision_required(
&self,
transaction_id: String,
transaction_state: String,
decision: Value,
pending_agents: Vec<String>,
)
pub async fn publish_decision_required( &self, transaction_id: String, transaction_state: String, decision: Value, pending_agents: Vec<String>, )
Publish a decision required event
Sourcepub async fn publish_event(&self, event: NodeEvent)
pub async fn publish_event(&self, event: NodeEvent)
Publish an event to all subscribers
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for EventBus
impl !RefUnwindSafe for EventBus
impl Send for EventBus
impl Sync for EventBus
impl Unpin for EventBus
impl UnsafeUnpin for EventBus
impl !UnwindSafe for EventBus
Blanket Implementations§
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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