From f7553d10d3ecdaadb594dd39bbd221b415fbca39 Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 23 Jun 2026 13:56:32 +0300 Subject: [PATCH] Own the event bus in Core; inject it into MusicManager Core holds a Void::EventBus::EventBus (Async), provides it as a service, and runs its 18 event subscriptions against it directly (setupEventBusSubscriptions is no longer const since it now mutates the owned bus). MusicManager gets the bus via setEventBus() and publishes through a small emit() helper (14 former GlobalEventBus publish sites). Delete EventBus.cpp (the singleton definition). Build green. Co-Authored-By: Claude Opus 4.8 --- include/echoion/core/Core.hpp | 8 +++- include/echoion/core/MusicManager.hpp | 15 ++++++++ src/Core.cpp | 53 +++++++++++++-------------- src/EventBus.cpp | 14 ------- src/MusicManager.cpp | 30 +++++++-------- 5 files changed, 63 insertions(+), 57 deletions(-) delete mode 100644 src/EventBus.cpp diff --git a/include/echoion/core/Core.hpp b/include/echoion/core/Core.hpp index 808d06f..ac6e6b1 100644 --- a/include/echoion/core/Core.hpp +++ b/include/echoion/core/Core.hpp @@ -7,6 +7,7 @@ #include "RpcServer.hpp" #include #include +#include #include #include #include @@ -150,7 +151,7 @@ namespace Echoion::Core [[nodiscard]] bool isAdmin(uint64_t clientId) const; void updateWhisperList(); void setupRpcHandlers(); - void setupEventBusSubscriptions() const noexcept; + void setupEventBusSubscriptions() noexcept; // Components std::shared_ptr networkClient_; @@ -186,6 +187,11 @@ namespace Echoion::Core // shims over the same instances. Void::Service::ServiceRegistry registry_; + // Application event bus (async): playback/queue/subscription events + // published by MusicManager and consumed here for RPC broadcast + + // DB persistence. Owned by Core, provided as a service. + Void::EventBus::EventBus eventBus_{Void::EventBus::Mode::Async}; + public: // Access the service registry (composition root). diff --git a/include/echoion/core/MusicManager.hpp b/include/echoion/core/MusicManager.hpp index 4d4b17f..1bd207f 100644 --- a/include/echoion/core/MusicManager.hpp +++ b/include/echoion/core/MusicManager.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,10 @@ namespace Echoion::Core std::shared_ptr database = nullptr); void shutdown(); + // Inject the application event bus (owned by Core). Must be set + // before playback so emitted events reach subscribers. + void setEventBus(Void::EventBus::EventBus* bus) { eventBus_ = bus; } + [[nodiscard]] Api::Result play(const std::string& query, uint64_t clientId); [[nodiscard]] Api::Result pause(); @@ -157,6 +162,14 @@ namespace Echoion::Core private: + // Publish a domain event on the injected bus (no-op until set). + template void emit(T event) + { + if (eventBus_) { + eventBus_->publish(std::move(event)); + } + } + void playbackThread(); [[nodiscard]] Api::Result resolveQuery(const std::string& query); @@ -207,6 +220,8 @@ namespace Echoion::Core std::thread playlistAutoStartThread_; std::shared_ptr networkClient_; + // Non-owning application event bus (provided by Core). + Void::EventBus::EventBus* eventBus_ = nullptr; std::unique_ptr youTubeClient_; std::unique_ptr audioDecoder_; std::unique_ptr subscriptionManager_; diff --git a/src/Core.cpp b/src/Core.cpp index 1376890..e18a91c 100644 --- a/src/Core.cpp +++ b/src/Core.cpp @@ -6,7 +6,7 @@ #include "echoion/core/state/StateVersions.hpp" #include #include -#include +#include #include #include #include @@ -80,9 +80,8 @@ namespace Echoion::Core ECHOION_LOG_INFO("Initializing Echoion Core..."); - // Start EventBus processing thread - Api::GlobalEventBus::instance().start(); - ECHOION_LOG_INFO("EventBus started"); + // The async event bus runs from construction; nothing to start. + ECHOION_LOG_INFO("EventBus ready"); // Initialize network client networkClient_ = std::make_shared(); @@ -110,6 +109,7 @@ namespace Echoion::Core // Initialize music manager musicManager_ = std::make_shared(); + musicManager_->setEventBus(&eventBus_); auto musicResult = musicManager_->initialize(networkClient_, database_); if (!musicResult) { return std::unexpected(musicResult.error()); @@ -307,8 +307,7 @@ namespace Echoion::Core } registry_.provide( alias(&Api::ConfigManager::instance())); - registry_.provide( - alias(&Api::GlobalEventBus::instance())); + registry_.provide(alias(&eventBus_)); if (networkClient_) { registry_.provide(networkClient_); } @@ -395,8 +394,8 @@ namespace Echoion::Core database_.reset(); } - // Stop EventBus - Api::GlobalEventBus::instance().stop(); + // Drain any in-flight event dispatch. + eventBus_.flush(); ECHOION_LOG_INFO("EventBus stopped"); // Clear callbacks @@ -1796,13 +1795,13 @@ namespace Echoion::Core ECHOION_LOG_INFO("RPC handlers registered"); } - void Core::setupEventBusSubscriptions() const noexcept + void Core::setupEventBusSubscriptions() noexcept { using namespace Api::Events; // Subscribe to playback events for RPC broadcasting and database // persistence - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const PlaybackStarted& evt) mutable { // Broadcast to RPC/WebSocket clients if (rpcServer_) { @@ -1822,7 +1821,7 @@ namespace Echoion::Core } }); - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const PlaybackPaused& evt) mutable { if (rpcServer_) { std::string data = std::format("{{\"position\":{}}}", @@ -1831,7 +1830,7 @@ namespace Echoion::Core } }); - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const PlaybackResumed& evt) mutable { if (rpcServer_) { std::string data = std::format("{{\"position\":{}}}", @@ -1840,14 +1839,14 @@ namespace Echoion::Core } }); - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const PlaybackStopped& evt) mutable { if (rpcServer_) { rpcServer_->broadcastEvent("playback.stopped", "{}"); } }); - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const PlaybackPositionChanged& evt) mutable { if (rpcServer_) { std::string data = @@ -1859,7 +1858,7 @@ namespace Echoion::Core } }); - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const TrackFinished& evt) mutable { if (rpcServer_) { rpcServer_->broadcastEvent("playback.finished", "{}"); @@ -1873,7 +1872,7 @@ namespace Echoion::Core }); // Volume and playback mode changes - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const VolumeChanged& evt) mutable { if (rpcServer_) { std::string data = @@ -1882,7 +1881,7 @@ namespace Echoion::Core } }); - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const ShuffleChanged& evt) mutable { if (rpcServer_) { std::string data = std::format( @@ -1891,7 +1890,7 @@ namespace Echoion::Core } }); - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const RepeatChanged& evt) mutable { if (rpcServer_) { std::string data = std::format( @@ -1901,7 +1900,7 @@ namespace Echoion::Core }); // Queue events - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const TrackQueued& evt) mutable { if (rpcServer_) { std::string data = std::format( @@ -1913,7 +1912,7 @@ namespace Echoion::Core } }); - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const TrackRemoved& evt) mutable { if (rpcServer_) { std::string data = @@ -1923,14 +1922,14 @@ namespace Echoion::Core } }); - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const QueueCleared& evt) mutable { if (rpcServer_) { rpcServer_->broadcastEvent("queue.cleared", "{}"); } }); - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const QueueReordered& evt) mutable { if (rpcServer_) { std::string data = std::format( @@ -1942,7 +1941,7 @@ namespace Echoion::Core }); // Playlist events - save to database via event bus using external ID - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const PlaylistLoadStarted& evt) mutable { if (rpcServer_) { std::string data = std::string("{\"playlistId\":\"") + @@ -1957,7 +1956,7 @@ namespace Echoion::Core } }); - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const PlaylistTrackLoaded& evt) mutable { if (rpcServer_) { std::string data = @@ -1979,7 +1978,7 @@ namespace Echoion::Core } }); - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const PlaylistLoadCompleted& evt) mutable { if (rpcServer_) { std::string data = @@ -1993,7 +1992,7 @@ namespace Echoion::Core }); // Subscription events - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const UserSubscribed& evt) mutable { if (rpcServer_) { std::string data = @@ -2003,7 +2002,7 @@ namespace Echoion::Core } }); - Api::GlobalEventBus::instance().subscribe( + eventBus_.subscribe( [this](const UserUnsubscribed& evt) mutable { if (rpcServer_) { std::string data = diff --git a/src/EventBus.cpp b/src/EventBus.cpp deleted file mode 100644 index 8d2be9f..0000000 --- a/src/EventBus.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include - -namespace Echoion::Api -{ - - // Global event bus instance. The EventBus type is otherwise header-only - // (it delegates to Void::EventBus); only the singleton lives here. - EventBus& GlobalEventBus::instance() - { - static EventBus instance; - return instance; - } - -} // namespace Echoion::Api diff --git a/src/MusicManager.cpp b/src/MusicManager.cpp index 0633343..afb8008 100644 --- a/src/MusicManager.cpp +++ b/src/MusicManager.cpp @@ -1,7 +1,7 @@ #include "ConfigManager.hpp" #include "LoggerInterface.hpp" #include "echoion/core/MusicManager.hpp" -#include +#include #include #include #include @@ -396,7 +396,7 @@ namespace Echoion::Core ECHOION_LOG_INFO("Playback stopped"); // Publish playback stopped event - Api::GlobalEventBus::instance().publish(Api::Events::PlaybackStopped{ + emit(Api::Events::PlaybackStopped{ .lastTrack = currentTrack_ ? std::optional(currentTrack_->track) : std::nullopt, .finalPosition = playbackPosition_}); @@ -493,7 +493,7 @@ namespace Echoion::Core ECHOION_LOG_INFO(std::format("Volume set to {}", volume)); // Publish volume changed event - Api::GlobalEventBus::instance().publish(Api::Events::VolumeChanged{ + emit(Api::Events::VolumeChanged{ .oldVolume = oldVolume, .newVolume = volume, .changedByClientId = 0 // Will be set by caller if available @@ -663,7 +663,7 @@ namespace Echoion::Core ECHOION_LOG_INFO(std::format("Removed track at position {}", position)); // Publish track removed event - Api::GlobalEventBus::instance().publish(Api::Events::TrackRemoved{ + emit(Api::Events::TrackRemoved{ .track = removedTrack.track, .queueId = removedTrack.queueId, .removedByClientId = 0, @@ -687,7 +687,7 @@ namespace Echoion::Core ECHOION_LOG_INFO("Queue cleared"); // Publish queue cleared event - Api::GlobalEventBus::instance().publish(Api::Events::QueueCleared{ + emit(Api::Events::QueueCleared{ .clearedByClientId = 0, .previousLength = previousLength}); return {}; @@ -738,7 +738,7 @@ namespace Echoion::Core ECHOION_LOG_INFO(std::format("Moved track from {} to {}", from, to)); // Publish queue reordered event - Api::GlobalEventBus::instance().publish( + emit( Api::Events::QueueReordered{.queueId = entry.queueId, .oldPosition = from, .newPosition = to, @@ -768,7 +768,7 @@ namespace Echoion::Core } // Publish shuffle changed event - Api::GlobalEventBus::instance().publish(Api::Events::ShuffleChanged{ + emit(Api::Events::ShuffleChanged{ .enabled = shuffle_, .changedByClientId = 0}); return shuffle_; @@ -815,7 +815,7 @@ namespace Echoion::Core ECHOION_LOG_INFO(repeat_ ? "Repeat enabled" : "Repeat disabled"); // Publish repeat changed event - Api::GlobalEventBus::instance().publish(Api::Events::RepeatChanged{ + emit(Api::Events::RepeatChanged{ .enabled = repeat_, .changedByClientId = 0}); return repeat_; @@ -865,7 +865,7 @@ namespace Echoion::Core std::format("Starting async playlist load: {}", playlistId)); // Publish playlist load started event - Api::GlobalEventBus::instance().publish( + emit( Api::Events::PlaylistLoadStarted{.playlistId = playlistId, .requestedByClientId = clientId}); @@ -899,7 +899,7 @@ namespace Echoion::Core added.fetch_add(1); // Publish playlist track loaded event - Api::GlobalEventBus::instance().publish( + emit( Api::Events::PlaylistTrackLoaded{ .playlistId = playlistId, .track = track, @@ -954,7 +954,7 @@ namespace Echoion::Core playlistLoading_ = false; // Publish playlist load completed event - Api::GlobalEventBus::instance().publish( + emit( Api::Events::PlaylistLoadCompleted{ .playlistId = playlistId, .totalTracksLoaded = added.load(), @@ -1427,7 +1427,7 @@ namespace Echoion::Core 100.0f; } - Api::GlobalEventBus::instance().publish( + emit( Api::Events::PlaybackPositionChanged{ .track = currentTrack_->track, .position = position, @@ -1723,7 +1723,7 @@ namespace Echoion::Core // Publish track finished event if (currentTrack_.has_value()) { - Api::GlobalEventBus::instance().publish(Api::Events::TrackFinished{ + emit(Api::Events::TrackFinished{ .track = currentTrack_->track, .autoSkipped = false, .finalPosition = getPlaybackPosition()}); @@ -1928,7 +1928,7 @@ Subscriptions are persistent across server reconnects.)"; } // Publish subscription event - Api::GlobalEventBus::instance().publish(Api::Events::UserSubscribed{ + emit(Api::Events::UserSubscribed{ .uid = uid, .nickname = nickname, .clientId = clientId}); if (added) { @@ -1976,7 +1976,7 @@ Subscriptions are persistent across server reconnects.)"; // Publish unsubscription event if (removed) { - Api::GlobalEventBus::instance().publish( + emit( Api::Events::UserUnsubscribed{ .uid = uid, .nickname = nickname, .clientId = clientId}); return "You are now unsubscribed from the bot's audio output.";