pub struct MessageBus {
pub trader_id: TraderId,
pub instance_id: UUID4,
pub name: String,
pub has_backing: bool,
/* private fields */
}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: TraderIdThe trader ID associated with the message bus.
instance_id: UUID4The instance ID associated with the message bus.
name: StringThe name for the message bus.
has_backing: boolIf the message bus is backed by a database.
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 register_message_bus(self) -> Rc<RefCell<Self>>
pub fn register_message_bus(self) -> Rc<RefCell<Self>>
Registers message bus for the current thread.
Sourcepub fn router<T: 'static>(&mut self) -> &mut TopicRouter<T>
pub fn router<T: 'static>(&mut self) -> &mut TopicRouter<T>
Gets or creates a typed router for custom message type T.
§Panics
Panics if the stored router type doesn’t match T (internal bug).
Sourcepub fn endpoint_map<T: 'static>(&mut self) -> &mut EndpointMap<T>
pub fn endpoint_map<T: 'static>(&mut self) -> &mut EndpointMap<T>
Gets or creates a typed endpoint map for custom message type T.
§Panics
Panics if the stored endpoint map type doesn’t match T (internal bug).
Sourcepub fn set_external_egress(
&mut self,
external_egress: Box<dyn MessageBusExternalEgress>,
encoding: SerializationEncoding,
)
pub fn set_external_egress( &mut self, external_egress: Box<dyn MessageBusExternalEgress>, encoding: SerializationEncoding, )
Sets external egress for serialized published messages.
Sourcepub fn set_external_egress_config(
&mut self,
external_egress: Box<dyn MessageBusExternalEgress>,
config: &MessageBusConfig,
) -> ConfigResult<()>
pub fn set_external_egress_config( &mut self, external_egress: Box<dyn MessageBusExternalEgress>, config: &MessageBusConfig, ) -> ConfigResult<()>
Sets external egress and category encoding policy from a validated config.
§Errors
Returns a crate::config::ConfigError if the config selects an unsupported encoding.
Sourcepub fn set_types_filter(&mut self, filter: Vec<String>)
pub fn set_types_filter(&mut self, filter: Vec<String>)
Sets the type names excluded from external publishing.
Sourcepub fn add_streaming_type(&mut self, payload_type: BusPayloadType)
pub fn add_streaming_type(&mut self, payload_type: BusPayloadType)
Registers a payload type for external-to-internal streaming.
Sourcepub fn is_streaming_type(&self, payload_type: BusPayloadType) -> bool
pub fn is_streaming_type(&self, payload_type: BusPayloadType) -> bool
Returns whether the payload type is registered for external-to-internal streaming.
Sourcepub fn clear_streaming_types(&mut self)
pub fn clear_streaming_types(&mut self)
Clears all payload types registered for external-to-internal streaming.
Sourcepub fn dispose(&mut self)
pub fn dispose(&mut self)
Disposes of the message bus, clearing all subscriptions, endpoints, and handler references.
Sourcepub fn mem_address(&self) -> String
pub fn mem_address(&self) -> String
Returns the memory address of this instance as a hexadecimal string.
Sourcepub fn switchboard(&self) -> &MessagingSwitchboard
pub fn switchboard(&self) -> &MessagingSwitchboard
Returns a reference to the switchboard.
Sourcepub const fn sent_count(&self) -> u64
pub const fn sent_count(&self) -> u64
Returns the total count of messages sent to endpoints.
Sourcepub const fn res_count(&self) -> u64
pub const fn res_count(&self) -> u64
Returns the total count of responses sent to registered handlers.
Sourcepub fn has_subscribers<T: AsRef<str>>(&self, topic: T) -> Result<bool>
pub fn has_subscribers<T: AsRef<str>>(&self, topic: T) -> Result<bool>
Returns whether there are subscribers for the topic.
§Errors
Returns an error if the topic is not a valid topic string.
Sourcepub fn subscriptions_count<T: AsRef<str>>(&self, topic: T) -> Result<usize>
pub fn subscriptions_count<T: AsRef<str>>(&self, topic: T) -> Result<usize>
Returns the count of subscribers for the topic.
§Errors
Returns an error if the topic is not a valid topic string.
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: Into<MStr<Endpoint>>>(&self, endpoint: T) -> bool
pub fn is_registered<T: Into<MStr<Endpoint>>>(&self, endpoint: T) -> bool
Returns whether the endpoint is registered.
§Panics
Panics if the endpoint conversion to MStr<Endpoint> fails.
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 fn close(&mut self) -> Result<()>
pub fn close(&mut 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: Into<MStr<Topic>>>(
&mut self,
topic: T,
) -> Vec<Subscription>
pub fn matching_subscriptions<T: Into<MStr<Topic>>>( &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.
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.