r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
utl::EventBus Class Reference

Thread-safe event bus for decoupled component communication. More...

#include <EventBus.hpp>

+ Collaboration diagram for utl::EventBus:

Classes

struct  EventQueue
 Internal event queue structure. More...
 
struct  PriorityEventQueue
 Priority queue for events. More...
 

Public Member Functions

 EventBus ()
 Constructor.
 
 ~EventBus ()
 Destructor.
 
 EventBus (const EventBus &)=delete
 
EventBusoperator= (const EventBus &)=delete
 
 EventBus (EventBus &&)=delete
 
EventBusoperator= (EventBus &&)=delete
 
bool publish (const Event &event)
 Publish an event to the bus.
 
template<typename T >
bool publish (EventType type, const T &data, std::uint32_t sourceId=0, std::uint32_t targetId=0, EventPriority priority=EventPriority::NORMAL)
 Publish event with serialized data.
 
std::vector< Eventconsume (std::uint32_t maxEvents=100)
 Consume events from the bus (non-blocking)
 
std::vector< EventwaitForEvents (std::chrono::milliseconds timeout=std::chrono::milliseconds(100), std::uint32_t maxEvents=100)
 Consume events with timeout (blocking)
 
std::vector< EventconsumeType (EventType type, std::uint32_t maxEvents=100)
 Consume events of specific type.
 
std::vector< EventconsumeForTarget (std::uint32_t targetId, std::uint32_t maxEvents=100)
 Consume events targeted to specific component.
 
void subscribe (std::uint32_t componentId, EventType type)
 Subscribe component to specific event types.
 
void unsubscribe (std::uint32_t componentId, EventType type)
 Unsubscribe component from specific event type.
 
void registerComponent (std::uint32_t componentId, const std::string &name)
 Register component name for better debugging.
 
void unregisterComponent (std::uint32_t componentId)
 Unregister component.
 
void filterEventType (EventType type)
 Add event type to filter (filtered events will be dropped)
 
void unfilterEventType (EventType type)
 Remove event type from filter.
 
void setMaxQueueSize (std::uint64_t maxSize)
 Set maximum queue size.
 
std::uint64_t getQueueSize () const
 Get current queue size.
 
EventStats getStats () const
 Get event bus statistics.
 
void clearStats ()
 Clear all statistics.
 
void clear ()
 Clear all events from queue.
 
void stop ()
 Stop the event bus.
 
bool isRunning () const
 Check if event bus is running.
 

Static Public Member Functions

static EventBusgetInstance ()
 Get singleton instance of EventBus.
 

Private Member Functions

void updateStats (const Event &event, bool isPublish, bool isExpired=false)
 Update statistics for an event.
 

Private Attributes

EventQueue m_eventQueue
 
PriorityEventQueue m_priorityQueue
 
std::unordered_map< EventType, std::unordered_set< std::uint32_t > > m_subscribers
 
std::mutex m_subscribersMutex
 
EventStats m_stats
 
std::mutex m_statsMutex
 
std::atomic< bool > m_running {true}
 
std::atomic< std::uint64_t > m_maxQueueSize {10000}
 
std::unordered_set< EventTypem_filteredTypes
 
std::mutex m_filterMutex
 
std::unordered_map< std::uint32_t, std::string > m_componentNames
 
std::mutex m_componentMutex
 

Detailed Description

Thread-safe event bus for decoupled component communication.

Definition at line 30 of file EventBus.hpp.

Constructor & Destructor Documentation

◆ EventBus() [1/3]

utl::EventBus::EventBus ( )
inline

Constructor.

Definition at line 91 of file EventBus.hpp.

References utl::INFO, utl::Logger::log(), m_stats, and utl::EventStats::reset().

+ Here is the call graph for this function:

◆ ~EventBus()

utl::EventBus::~EventBus ( )
inline

Destructor.

Definition at line 100 of file EventBus.hpp.

References utl::EventBus::EventQueue::cv, utl::INFO, utl::Logger::log(), m_eventQueue, and m_running.

+ Here is the call graph for this function:

◆ EventBus() [2/3]

utl::EventBus::EventBus ( const EventBus & )
delete

◆ EventBus() [3/3]

utl::EventBus::EventBus ( EventBus && )
delete

Member Function Documentation

◆ clear()

void utl::EventBus::clear ( )
inline

Clear all events from queue.

Definition at line 526 of file EventBus.hpp.

References utl::EventBus::EventQueue::events, utl::INFO, utl::Logger::log(), m_eventQueue, and utl::EventBus::EventQueue::mutex.

+ Here is the call graph for this function:

◆ clearStats()

void utl::EventBus::clearStats ( )
inline

Clear all statistics.

Definition at line 517 of file EventBus.hpp.

References m_stats, m_statsMutex, and utl::EventStats::reset().

+ Here is the call graph for this function:

◆ consume()

std::vector< Event > utl::EventBus::consume ( std::uint32_t maxEvents = 100)
inline

Consume events from the bus (non-blocking)

Parameters
maxEventsMaximum number of events to consume
Returns
Vector of consumed events

Definition at line 271 of file EventBus.hpp.

References utl::EventBus::EventQueue::events, m_eventQueue, utl::EventBus::EventQueue::mutex, and updateStats().

Referenced by consumeForTarget(), consumeType(), and waitForEvents().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ consumeForTarget()

std::vector< Event > utl::EventBus::consumeForTarget ( std::uint32_t targetId,
std::uint32_t maxEvents = 100 )
inline

Consume events targeted to specific component.

Parameters
targetIdTarget component ID
maxEventsMaximum number of events to consume
Returns
Vector of events targeted to the component

Definition at line 357 of file EventBus.hpp.

References consume(), utl::EventBus::EventQueue::events, m_eventQueue, and utl::EventBus::EventQueue::mutex.

Referenced by eng::AsioClient::processBusEvent(), gme::WaitingRoomScene::processEventBus(), srv::AsioServer::processEventBusEvents(), and gme::RTypeServer::update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ consumeType()

std::vector< Event > utl::EventBus::consumeType ( EventType type,
std::uint32_t maxEvents = 100 )
inline

Consume events of specific type.

Parameters
typeEvent type to filter
maxEventsMaximum number of events to consume
Returns
Vector of events of the specified type

Definition at line 329 of file EventBus.hpp.

References consume(), utl::EventBus::EventQueue::events, m_eventQueue, and utl::EventBus::EventQueue::mutex.

+ Here is the call graph for this function:

◆ filterEventType()

void utl::EventBus::filterEventType ( EventType type)
inline

Add event type to filter (filtered events will be dropped)

Parameters
typeEvent type to filter

Definition at line 462 of file EventBus.hpp.

References utl::INFO, utl::Logger::log(), m_filteredTypes, and m_filterMutex.

+ Here is the call graph for this function:

◆ getInstance()

static EventBus & utl::EventBus::getInstance ( )
inlinestatic

Get singleton instance of EventBus.

Returns
Reference to the singleton EventBus instance

Definition at line 82 of file EventBus.hpp.

Referenced by gme::GameMulti::handleWorldStateUpdate(), gme::GameMulti::processEventBus(), and gme::GameMulti::setupEventSubscriptions().

+ Here is the caller graph for this function:

◆ getQueueSize()

std::uint64_t utl::EventBus::getQueueSize ( ) const
inline

Get current queue size.

Returns
Current number of events in queue

Definition at line 496 of file EventBus.hpp.

References utl::EventBus::EventQueue::events, m_eventQueue, and utl::EventBus::EventQueue::mutex.

Referenced by getStats().

+ Here is the caller graph for this function:

◆ getStats()

EventStats utl::EventBus::getStats ( ) const
inline

Get event bus statistics.

Returns
Copy of current statistics

Definition at line 506 of file EventBus.hpp.

References utl::EventStats::currentQueueSize, getQueueSize(), m_stats, and m_statsMutex.

+ Here is the call graph for this function:

◆ isRunning()

bool utl::EventBus::isRunning ( ) const
inline

Check if event bus is running.

Returns
True if running

Definition at line 550 of file EventBus.hpp.

References m_running.

◆ operator=() [1/2]

EventBus & utl::EventBus::operator= ( const EventBus & )
delete

◆ operator=() [2/2]

EventBus & utl::EventBus::operator= ( EventBus && )
delete

◆ publish() [1/2]

◆ publish() [2/2]

template<typename T >
bool utl::EventBus::publish ( EventType type,
const T & data,
std::uint32_t sourceId = 0,
std::uint32_t targetId = 0,
EventPriority priority = EventPriority::NORMAL )
inline

◆ registerComponent()

void utl::EventBus::registerComponent ( std::uint32_t componentId,
const std::string & name )
inline

Register component name for better debugging.

Parameters
componentIdComponent ID
nameComponent name

Definition at line 423 of file EventBus.hpp.

References utl::INFO, utl::Logger::log(), m_componentMutex, and m_componentNames.

Referenced by eng::AsioClient::AsioClient(), srv::AsioServer::AsioServer(), gme::CreateRoomScene::CreateRoomScene(), gme::JoinRoomScene::JoinRoomScene(), gme::PlayerControllerMulti::PlayerControllerMulti(), gme::RTypeServer::RTypeServer(), gme::ServerScene::ServerScene(), gme::GameMulti::setupEventSubscriptions(), and gme::WaitingRoomScene::WaitingRoomScene().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setMaxQueueSize()

void utl::EventBus::setMaxQueueSize ( std::uint64_t maxSize)
inline

Set maximum queue size.

Parameters
maxSizeMaximum number of events in queue

Definition at line 490 of file EventBus.hpp.

References m_maxQueueSize.

◆ stop()

void utl::EventBus::stop ( )
inline

Stop the event bus.

Definition at line 539 of file EventBus.hpp.

References utl::EventBus::EventQueue::cv, utl::INFO, utl::Logger::log(), m_eventQueue, and m_running.

+ Here is the call graph for this function:

◆ subscribe()

void utl::EventBus::subscribe ( std::uint32_t componentId,
EventType type )
inline

Subscribe component to specific event types.

Parameters
componentIdComponent ID
typeEvent type to subscribe to

Definition at line 384 of file EventBus.hpp.

References utl::Logger::log(), m_subscribers, m_subscribersMutex, and utl::WARNING.

Referenced by eng::AsioClient::AsioClient(), srv::AsioServer::AsioServer(), gme::CreateRoomScene::CreateRoomScene(), gme::RTypeServer::RTypeServer(), gme::GameMulti::setupEventSubscriptions(), and gme::WaitingRoomScene::setupEventSubscriptions().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unfilterEventType()

void utl::EventBus::unfilterEventType ( EventType type)
inline

Remove event type from filter.

Parameters
typeEvent type to unfilter

Definition at line 476 of file EventBus.hpp.

References utl::INFO, utl::Logger::log(), m_filteredTypes, and m_filterMutex.

+ Here is the call graph for this function:

◆ unregisterComponent()

void utl::EventBus::unregisterComponent ( std::uint32_t componentId)
inline

Unregister component.

Parameters
componentIdComponent ID

Definition at line 436 of file EventBus.hpp.

References utl::INFO, utl::Logger::log(), m_componentMutex, m_componentNames, m_subscribers, and m_subscribersMutex.

+ Here is the call graph for this function:

◆ unsubscribe()

void utl::EventBus::unsubscribe ( std::uint32_t componentId,
EventType type )
inline

Unsubscribe component from specific event type.

Parameters
componentIdComponent ID
typeEvent type to unsubscribe from

Definition at line 399 of file EventBus.hpp.

References utl::Logger::log(), m_subscribers, m_subscribersMutex, and utl::WARNING.

+ Here is the call graph for this function:

◆ updateStats()

void utl::EventBus::updateStats ( const Event & event,
bool isPublish,
bool isExpired = false )
inlineprivate

Update statistics for an event.

Parameters
eventEvent to update stats for
isPublishTrue if this is a publish operation
isExpiredTrue if event expired

Definition at line 559 of file EventBus.hpp.

References utl::EventStats::eventTypeCount, m_stats, m_statsMutex, utl::EventStats::priorityCount, utl::EventStats::sourceCount, utl::EventStats::targetCount, utl::Event::targetId, utl::EventStats::totalEventsConsumed, utl::EventStats::totalEventsExpired, and utl::EventStats::totalEventsPublished.

Referenced by consume(), and publish().

+ Here is the caller graph for this function:

◆ waitForEvents()

std::vector< Event > utl::EventBus::waitForEvents ( std::chrono::milliseconds timeout = std::chrono::milliseconds(100),
std::uint32_t maxEvents = 100 )
inline

Consume events with timeout (blocking)

Parameters
timeoutMaximum time to wait for events
maxEventsMaximum number of events to consume
Returns
Vector of consumed events

Definition at line 309 of file EventBus.hpp.

References consume(), utl::EventBus::EventQueue::cv, m_eventQueue, and utl::EventBus::EventQueue::mutex.

+ Here is the call graph for this function:

Member Data Documentation

◆ m_componentMutex

std::mutex utl::EventBus::m_componentMutex
mutableprivate

Definition at line 75 of file EventBus.hpp.

Referenced by registerComponent(), and unregisterComponent().

◆ m_componentNames

std::unordered_map<std::uint32_t, std::string> utl::EventBus::m_componentNames
private

Definition at line 74 of file EventBus.hpp.

Referenced by registerComponent(), and unregisterComponent().

◆ m_eventQueue

EventQueue utl::EventBus::m_eventQueue
private

◆ m_filteredTypes

std::unordered_set<EventType> utl::EventBus::m_filteredTypes
private

Definition at line 70 of file EventBus.hpp.

Referenced by filterEventType(), publish(), and unfilterEventType().

◆ m_filterMutex

std::mutex utl::EventBus::m_filterMutex
mutableprivate

Definition at line 71 of file EventBus.hpp.

Referenced by filterEventType(), publish(), and unfilterEventType().

◆ m_maxQueueSize

std::atomic<std::uint64_t> utl::EventBus::m_maxQueueSize {10000}
private

Definition at line 67 of file EventBus.hpp.

Referenced by publish(), and setMaxQueueSize().

◆ m_priorityQueue

PriorityEventQueue utl::EventBus::m_priorityQueue
private

Definition at line 61 of file EventBus.hpp.

◆ m_running

std::atomic<bool> utl::EventBus::m_running {true}
private

Definition at line 66 of file EventBus.hpp.

Referenced by isRunning(), publish(), stop(), and ~EventBus().

◆ m_stats

EventStats utl::EventBus::m_stats
private

Definition at line 64 of file EventBus.hpp.

Referenced by clearStats(), EventBus(), getStats(), and updateStats().

◆ m_statsMutex

std::mutex utl::EventBus::m_statsMutex
mutableprivate

Definition at line 65 of file EventBus.hpp.

Referenced by clearStats(), getStats(), and updateStats().

◆ m_subscribers

std::unordered_map<EventType, std::unordered_set<std::uint32_t> > utl::EventBus::m_subscribers
private

Definition at line 62 of file EventBus.hpp.

Referenced by subscribe(), unregisterComponent(), and unsubscribe().

◆ m_subscribersMutex

std::mutex utl::EventBus::m_subscribersMutex
mutableprivate

Definition at line 63 of file EventBus.hpp.

Referenced by subscribe(), unregisterComponent(), and unsubscribe().


The documentation for this class was generated from the following file: