#include "echoion/api/EventBus.hpp" #include "echoion/core/Logger.hpp" #include 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 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 evt; { std::lock_guard lock(queueMutex_); if (eventQueue_.empty()) { return; } evt = std::move(eventQueue_.front()); eventQueue_.pop(); } // Get handlers for this event type std::shared_lock 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 evt; { std::unique_lock 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 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 lock(queueMutex_); std::queue> empty; std::swap(eventQueue_, empty); } size_t EventBus::getPendingEventCount() const { std::lock_guard lock(queueMutex_); return eventQueue_.size(); } void EventBus::processEvents() { while (!stopRequested_) { std::shared_ptr evt; { std::unique_lock 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 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