pub struct MessageBus {
pub trader_id: TraderId,
pub instance_id: UUID4,
pub name: String,
pub has_backing: bool,
pub switchboard: MessagingSwitchboard,
pub subscriptions: AHashSet<Subscription>,
pub topics: IndexMap<MStr<Topic>, Vec<Subscription>>,
pub endpoints: IndexMap<MStr<Endpoint>, ShareableMessageHandler>,
pub correlation_index: AHashMap<UUID4, ShareableMessageHandler>,
}
Expand description
A generic message bus to facilitate various messaging patterns.
The bus provides both a producer and consumer API for Pub/Sub, Req/Rep, as well as direct point-to-point messaging to registered endpoints.
Pub/Sub wildcard patterns for hierarchical topics are possible:
*
asterisk represents one or more characters in a pattern.?
question mark represents a single character in a pattern.
Given a topic and pattern potentially containing wildcard characters, i.e.
*
and ?
, where ?
can match any single character in the topic, and *
can match any number of characters including zero characters.
The asterisk in a wildcard matches any character zero or more times. For
example, comp*
matches anything beginning with comp
which means comp
,
complete
, and computer
are all matched.
A question mark matches a single character once. For example, c?mp
matches
camp
and comp
. The question mark can also be used more than once.
For example, c??p
would match both of the above examples and coop
.
Fields§
§trader_id: TraderId
The trader ID associated with the message bus.
instance_id: UUID4
The instance ID associated with the message bus.
name: String
The name for the message bus.
has_backing: bool
If the message bus is backed by a database.
switchboard: MessagingSwitchboard
The switchboard for built-in endpoints.
subscriptions: AHashSet<Subscription>
Active subscriptions.
topics: IndexMap<MStr<Topic>, Vec<Subscription>>
Maps a topic to all the handlers registered for it this is updated whenever a new subscription is created.
endpoints: IndexMap<MStr<Endpoint>, ShareableMessageHandler>
Index of endpoint addresses and their handlers.
correlation_index: AHashMap<UUID4, ShareableMessageHandler>
Index of request correlation IDs and their response handlers.
Implementations§
Source§impl MessageBus
impl MessageBus
Sourcepub fn new(
trader_id: TraderId,
instance_id: UUID4,
name: Option<String>,
_config: Option<HashMap<String, Value>>,
) -> Self
pub fn new( trader_id: TraderId, instance_id: UUID4, name: Option<String>, _config: Option<HashMap<String, Value>>, ) -> Self
Creates a new MessageBus
instance.
Sourcepub fn memory_address(&self) -> String
pub fn memory_address(&self) -> String
Returns the message bus instances memory address.
Sourcepub fn has_subscribers<T: AsRef<str>>(&self, topic: T) -> bool
pub fn has_subscribers<T: AsRef<str>>(&self, topic: T) -> bool
Returns whether there are subscribers for the topic
.
Sourcepub fn subscriptions_count<T: AsRef<str>>(&self, topic: T) -> usize
pub fn subscriptions_count<T: AsRef<str>>(&self, topic: T) -> usize
Sourcepub fn subscriptions(&self) -> Vec<&Subscription>
pub fn subscriptions(&self) -> Vec<&Subscription>
Returns active subscriptions.
Sourcepub fn subscription_handler_ids(&self) -> Vec<&str>
pub fn subscription_handler_ids(&self) -> Vec<&str>
Returns the handler IDs for actively subscribed patterns.
Sourcepub fn is_registered<T: AsRef<str>>(&self, endpoint: T) -> bool
pub fn is_registered<T: AsRef<str>>(&self, endpoint: T) -> bool
Returns whether the endpoint is registered.
§Panics
Returns an error if the endpoint is not valid topic string.
Sourcepub fn is_subscribed<T: AsRef<str>>(
&self,
pattern: T,
handler: ShareableMessageHandler,
) -> bool
pub fn is_subscribed<T: AsRef<str>>( &self, pattern: T, handler: ShareableMessageHandler, ) -> bool
Returns whether the handler
is subscribed to the pattern
.
Sourcepub const fn close(&self) -> Result<()>
pub const fn close(&self) -> Result<()>
Close the message bus which will close the sender channel and join the thread.
§Errors
This function never returns an error (TBD once backing database added).
Sourcepub fn get_endpoint(
&self,
endpoint: MStr<Endpoint>,
) -> Option<&ShareableMessageHandler>
pub fn get_endpoint( &self, endpoint: MStr<Endpoint>, ) -> Option<&ShareableMessageHandler>
Returns the handler for the endpoint
.
Sourcepub fn get_response_handler(
&self,
correlation_id: &UUID4,
) -> Option<&ShareableMessageHandler>
pub fn get_response_handler( &self, correlation_id: &UUID4, ) -> Option<&ShareableMessageHandler>
Returns the handler for the correlation_id
.
Sourcepub fn matching_subscriptions<T: AsRef<str>>(
&mut self,
topic: T,
) -> Vec<Subscription>
pub fn matching_subscriptions<T: AsRef<str>>( &mut self, topic: T, ) -> Vec<Subscription>
Finds the subscriptions which match the topic
and caches the
results in the patterns
map.
Sourcepub fn register_response_handler(
&mut self,
correlation_id: &UUID4,
handler: ShareableMessageHandler,
) -> Result<()>
pub fn register_response_handler( &mut self, correlation_id: &UUID4, handler: ShareableMessageHandler, ) -> Result<()>
Registers a response handler for a specific correlation ID.
§Errors
Returns an error if handler
is already registered for the correlation_id
.
Source§impl MessageBus
Data specific functions.
impl MessageBus
Data specific functions.
Sourcepub fn register_message_bus(self) -> Rc<RefCell<MessageBus>>
pub fn register_message_bus(self) -> Rc<RefCell<MessageBus>>
Registers message bus for the current thread.
Trait Implementations§
Source§impl Debug for MessageBus
impl Debug for MessageBus
Source§impl Default for MessageBus
impl Default for MessageBus
Source§fn default() -> Self
fn default() -> Self
Creates a new default MessageBus
instance.