Files
EchoionCore/src/EventBus.cpp
T
2026-02-01 23:04:49 +03:00

186 lines
4.8 KiB
C++

#include "echoion/api/EventBus.hpp"
#include "echoion/core/Logger.hpp"
#include <format>
namespace Echoion::Api {
// Global event bus instance
EventBus& GlobalEventBus::instance() {
static EventBus instance;
return instance;
}
EventBus::EventBus() = default;
EventBus::~EventBus() {
stop();
}
void EventBus::start() {
if (running_.exchange(true)) {
return; // Already running
}
stopRequested_ = false;
eventThread_ = std::jthread([this] { processEvents(); });
ECHOION_LOG_INFO("EventBus started");
}
void EventBus::stop() {
if (!running_.exchange(false)) {
return; // Not running
}
stopRequested_ = true;
queueCv_.notify_all();
if (eventThread_.joinable()) {
eventThread_.join();
}
ECHOION_LOG_INFO("EventBus stopped");
}
void EventBus::unsubscribe(const SubscriptionToken& token) {
if (!token.isValid()) {
return;
}
std::unique_lock<std::shared_mutex> lock(handlersMutex_);
auto it = handlers_.find(token.getType());
if (it != handlers_.end()) {
it->second.erase(token.getId());
if (it->second.empty()) {
handlers_.erase(it);
}
}
}
void EventBus::processOne() {
std::shared_ptr<IEvent> evt;
{
std::lock_guard<std::mutex> lock(queueMutex_);
if (eventQueue_.empty()) {
return;
}
evt = std::move(eventQueue_.front());
eventQueue_.pop();
}
// Get handlers for this event type
std::shared_lock<std::shared_mutex> lock(handlersMutex_);
auto it = handlers_.find(evt->getType());
if (it != handlers_.end()) {
// Copy handlers to avoid holding lock during callback
auto handlers = it->second;
lock.unlock();
for (const auto& [id, handler] : handlers) {
try {
if (handler) {
(*handler)(evt);
}
} catch (const std::exception& e) {
ECHOION_LOG_ERROR(std::format("Event handler exception: {}", e.what()));
}
}
}
}
bool EventBus::waitAndProcessOne(std::chrono::milliseconds timeout) {
std::shared_ptr<IEvent> evt;
{
std::unique_lock<std::mutex> lock(queueMutex_);
if (!queueCv_.wait_for(lock, timeout, [this] {
return !eventQueue_.empty() || stopRequested_.load();
})) {
return false; // Timeout
}
if (stopRequested_ || eventQueue_.empty()) {
return false;
}
evt = std::move(eventQueue_.front());
eventQueue_.pop();
}
// Get handlers for this event type
std::shared_lock<std::shared_mutex> lock(handlersMutex_);
auto it = handlers_.find(evt->getType());
if (it != handlers_.end()) {
auto handlers = it->second;
lock.unlock();
for (const auto& [id, handler] : handlers) {
try {
if (handler) {
(*handler)(evt);
}
} catch (const std::exception& e) {
ECHOION_LOG_ERROR(std::format("Event handler exception: {}", e.what()));
}
}
}
return true;
}
void EventBus::clearQueue() {
std::lock_guard<std::mutex> lock(queueMutex_);
std::queue<std::shared_ptr<IEvent>> empty;
std::swap(eventQueue_, empty);
}
size_t EventBus::getPendingEventCount() const {
std::lock_guard<std::mutex> lock(queueMutex_);
return eventQueue_.size();
}
void EventBus::processEvents() {
while (!stopRequested_) {
std::shared_ptr<IEvent> evt;
{
std::unique_lock<std::mutex> lock(queueMutex_);
queueCv_.wait(lock, [this] {
return !eventQueue_.empty() || stopRequested_.load();
});
if (stopRequested_) {
break;
}
if (eventQueue_.empty()) {
continue;
}
evt = std::move(eventQueue_.front());
eventQueue_.pop();
}
// Get handlers for this event type
std::shared_lock<std::shared_mutex> lock(handlersMutex_);
auto it = handlers_.find(evt->getType());
if (it != handlers_.end()) {
// Copy handlers to avoid holding lock during callback
auto handlers = it->second;
lock.unlock();
for (const auto& [id, handler] : handlers) {
try {
if (handler) {
(*handler)(evt);
}
} catch (const std::exception& e) {
ECHOION_LOG_ERROR(std::format("Event handler exception: {}", e.what()));
}
}
}
}
}
} // namespace Echoion::Api