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<T>() helper (14 former GlobalEventBus publish sites). Delete EventBus.cpp (the singleton definition). Build green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
205 lines
7.0 KiB
C++
205 lines
7.0 KiB
C++
#pragma once
|
|
|
|
#include "CommandManager.hpp"
|
|
#include "ConfigLoader.hpp"
|
|
#include "MusicManager.hpp"
|
|
#include "NetworkClient.hpp"
|
|
#include "RpcServer.hpp"
|
|
#include <ConfigTypes.hpp>
|
|
#include <CoreInterface.hpp>
|
|
#include <Void/Async/EventBus.hpp>
|
|
#include <Database.hpp>
|
|
#include <RpcTypes.hpp>
|
|
#include <StateBlob.hpp>
|
|
#include <TrackTypes.hpp>
|
|
#include <Types.hpp>
|
|
#include <Void/Service/Registry.hpp>
|
|
#include <atomic>
|
|
#include <mutex>
|
|
#include <thread>
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
|
|
// Main Core implementation of ICoreInterface
|
|
class Core : public Api::ICoreInterface
|
|
{
|
|
public:
|
|
|
|
Core();
|
|
~Core() override;
|
|
|
|
// ICoreInterface - Lifecycle
|
|
Api::Result<void>
|
|
initialize(const std::filesystem::path& configPath) override;
|
|
Api::Result<void> shutdown() override;
|
|
[[nodiscard]] bool isInitialized() const noexcept override;
|
|
|
|
// ICoreInterface - Playback control (decoupled)
|
|
Api::Result<void> play(const std::string& trackId) override;
|
|
Api::Result<void> pause() override;
|
|
Api::Result<void> resume() override;
|
|
Api::Result<void> stop() override;
|
|
Api::Result<void> skip(uint32_t count) override;
|
|
Api::Result<void> seek(std::chrono::milliseconds position) override;
|
|
Api::Result<void> setVolume(float volume) override;
|
|
|
|
// ICoreInterface - Queue management (decoupled)
|
|
Api::Result<uint64_t> queueTrack(const std::string& trackId,
|
|
uint64_t clientId) override;
|
|
Api::Result<void> removeFromQueue(uint64_t queueId) override;
|
|
Api::Result<void> clearQueue() override;
|
|
[[nodiscard]] Api::Result<std::vector<Api::TrackSummary>>
|
|
getQueueSummaries() const override;
|
|
Api::Result<void> moveInQueue(uint64_t queueId,
|
|
uint32_t newPosition) override;
|
|
|
|
// ICoreInterface - Track info (decoupled)
|
|
[[nodiscard]] Api::Result<std::optional<Api::TrackSummary>>
|
|
getCurrentTrackSummary() const override;
|
|
[[nodiscard]] Api::Result<Api::PlaybackSummary>
|
|
getPlaybackSummary() const override;
|
|
|
|
// ICoreInterface - Playlist (decoupled)
|
|
Api::Result<void> loadPlaylist(const std::string& playlistId,
|
|
uint64_t clientId) override;
|
|
[[nodiscard]] Api::Result<std::vector<std::string>>
|
|
getPlaylistIds() const override;
|
|
|
|
// ICoreInterface - Configuration (decoupled)
|
|
Api::Result<void>
|
|
updateConfigJson(const std::string& configJson) override;
|
|
[[nodiscard]] Api::Result<std::string>
|
|
getConfigJson() const override;
|
|
|
|
// ICoreInterface - Hot reload (decoupled)
|
|
[[nodiscard]] Api::Result<std::string>
|
|
prepareForHotReload() override;
|
|
Api::Result<void>
|
|
restoreAfterHotReload(const std::string& state) override;
|
|
|
|
// ICoreInterface - Decoupled state management
|
|
[[nodiscard]] Api::StateBlob saveState() override;
|
|
Api::Result<void>
|
|
restoreState(const Api::StateBlob& state) override;
|
|
[[nodiscard]] uint32_t getStateFormatVersion() const override;
|
|
[[nodiscard]] bool
|
|
canRestoreStateVersion(uint32_t version) const override;
|
|
|
|
// ICoreInterface - Event handling
|
|
void registerEventCallback(
|
|
const Api::EventCallback& callback) override;
|
|
void unregisterEventCallback(
|
|
const Api::EventCallback& callback) override;
|
|
|
|
// ICoreInterface - Plugin event processing
|
|
Api::Result<void>
|
|
onPluginEvent(const Api::PluginEvent& event) override;
|
|
|
|
// ICoreInterface - Status
|
|
[[nodiscard]] std::string getVersion() const override;
|
|
[[nodiscard]] std::chrono::system_clock::time_point
|
|
getUptime() const override;
|
|
|
|
// ICoreInterface - Plugin interface
|
|
void setPluginInterface(
|
|
Api::PluginInterfacePtr pluginInterface) override;
|
|
void triggerAutoConnect() override;
|
|
|
|
// ICoreInterface - Advanced (for internal Core-to-Core use)
|
|
[[nodiscard]] Api::Result<Api::OpaqueHandle<Api::TrackInfo>>
|
|
getCurrentTrackHandle() const override;
|
|
[[nodiscard]] Api::Result<
|
|
std::vector<Api::OpaqueHandle<Api::QueueEntry>>>
|
|
getQueueHandles() const override;
|
|
[[nodiscard]] Api::Result<Api::AppConfigHandle>
|
|
getConfigHandle() const override;
|
|
Api::Result<void>
|
|
updateConfigFromHandle(Api::AppConfigHandle config) override;
|
|
|
|
// Additional methods for internal use (not in interface)
|
|
void setAudioDataCallback(
|
|
const MusicManager::AudioDataCallback& callback);
|
|
Api::Result<void> reloadConfig();
|
|
|
|
// Internal implementations using full types
|
|
Api::Result<void> playTrack(const Api::TrackInfo& track);
|
|
Api::Result<uint64_t> queueTrackFull(const Api::TrackInfo& track,
|
|
uint64_t clientId);
|
|
[[nodiscard]] Api::Result<std::vector<Api::QueueEntry>>
|
|
getQueue() const;
|
|
[[nodiscard]] Api::Result<std::optional<Api::QueueEntry>>
|
|
getCurrentTrack() const;
|
|
[[nodiscard]] Api::Result<Api::PlaybackState>
|
|
getPlaybackState() const;
|
|
[[nodiscard]] Api::Result<std::vector<Api::PlaylistInfo>>
|
|
getPlaylists() const;
|
|
Api::Result<void> updateConfig(const Api::AppConfig& config);
|
|
[[nodiscard]] Api::Result<Api::AppConfig> getConfig() const;
|
|
[[nodiscard]] Api::Result<Api::HotReloadState>
|
|
prepareForHotReloadFull();
|
|
Api::Result<void>
|
|
restoreAfterHotReloadFull(const Api::HotReloadState& state);
|
|
|
|
// Generate RPC tokens for existing subscribers
|
|
void generateRpcTokensForSubscribers();
|
|
|
|
private:
|
|
|
|
void onMusicEvent(const Api::PluginEvent& event);
|
|
void broadcastEvent(const Api::PluginEvent& event);
|
|
[[nodiscard]] bool isAdmin(uint64_t clientId) const;
|
|
void updateWhisperList();
|
|
void setupRpcHandlers();
|
|
void setupEventBusSubscriptions() noexcept;
|
|
|
|
// Components
|
|
std::shared_ptr<NetworkClient> networkClient_;
|
|
std::shared_ptr<MusicManager> musicManager_;
|
|
std::unique_ptr<CommandManager> commandManager_;
|
|
Api::PluginInterfacePtr pluginInterface_;
|
|
|
|
// State
|
|
Api::AppConfig config_;
|
|
std::filesystem::path configPath_;
|
|
std::chrono::system_clock::time_point startTime_;
|
|
std::atomic<bool> initialized_{false};
|
|
|
|
// Event callbacks
|
|
std::vector<Api::EventCallback> eventCallbacks_;
|
|
mutable std::mutex eventMutex_;
|
|
|
|
// Admin tracking
|
|
std::vector<uint64_t> adminClients_;
|
|
mutable std::mutex adminMutex_;
|
|
|
|
// Database for persistence
|
|
std::shared_ptr<Api::Database> database_;
|
|
|
|
// RPC server for Desktop/API clients
|
|
std::shared_ptr<RpcServer> rpcServer_;
|
|
|
|
// Composition root: a type-keyed registry of all Core services
|
|
// (logger, config, event bus, network, database, music, rpc).
|
|
// initialize() provides each instance; resolveServices() and other
|
|
// code can require<I>() them by interface instead of reaching for
|
|
// global singletons. The former singletons stay as thin macro
|
|
// 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).
|
|
[[nodiscard]] Void::Service::ServiceRegistry& services() noexcept
|
|
{
|
|
return registry_;
|
|
}
|
|
};
|
|
|
|
} // namespace Echoion::Core
|