#pragma once #include "CommandManager.hpp" #include "ConfigLoader.hpp" #include "MusicManager.hpp" #include "NetworkClient.hpp" #include "RpcServer.hpp" #include #include #include #include #include #include #include #include #include #include #include #include namespace Echoion::Core { // Main Core implementation of ICoreInterface class Core : public Api::ICoreInterface { public: Core(); ~Core() override; // ICoreInterface - Lifecycle Api::Result initialize(const std::filesystem::path& configPath) override; Api::Result shutdown() override; [[nodiscard]] bool isInitialized() const noexcept override; // ICoreInterface - Playback control (decoupled) Api::Result play(const std::string& trackId) override; Api::Result pause() override; Api::Result resume() override; Api::Result stop() override; Api::Result skip(uint32_t count) override; Api::Result seek(std::chrono::milliseconds position) override; Api::Result setVolume(float volume) override; // ICoreInterface - Queue management (decoupled) Api::Result queueTrack(const std::string& trackId, uint64_t clientId) override; Api::Result removeFromQueue(uint64_t queueId) override; Api::Result clearQueue() override; [[nodiscard]] Api::Result> getQueueSummaries() const override; Api::Result moveInQueue(uint64_t queueId, uint32_t newPosition) override; // ICoreInterface - Track info (decoupled) [[nodiscard]] Api::Result> getCurrentTrackSummary() const override; [[nodiscard]] Api::Result getPlaybackSummary() const override; // ICoreInterface - Playlist (decoupled) Api::Result loadPlaylist(const std::string& playlistId, uint64_t clientId) override; [[nodiscard]] Api::Result> getPlaylistIds() const override; // ICoreInterface - Configuration (decoupled) Api::Result updateConfigJson(const std::string& configJson) override; [[nodiscard]] Api::Result getConfigJson() const override; // ICoreInterface - Hot reload (decoupled) [[nodiscard]] Api::Result prepareForHotReload() override; Api::Result restoreAfterHotReload(const std::string& state) override; // ICoreInterface - Decoupled state management [[nodiscard]] Api::StateBlob saveState() override; Api::Result 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 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> getCurrentTrackHandle() const override; [[nodiscard]] Api::Result< std::vector>> getQueueHandles() const override; [[nodiscard]] Api::Result getConfigHandle() const override; Api::Result updateConfigFromHandle(Api::AppConfigHandle config) override; // Additional methods for internal use (not in interface) void setAudioDataCallback( const MusicManager::AudioDataCallback& callback); Api::Result reloadConfig(); // Internal implementations using full types Api::Result playTrack(const Api::TrackInfo& track); Api::Result queueTrackFull(const Api::TrackInfo& track, uint64_t clientId); [[nodiscard]] Api::Result> getQueue() const; [[nodiscard]] Api::Result> getCurrentTrack() const; [[nodiscard]] Api::Result getPlaybackState() const; [[nodiscard]] Api::Result> getPlaylists() const; Api::Result updateConfig(const Api::AppConfig& config); [[nodiscard]] Api::Result getConfig() const; [[nodiscard]] Api::Result prepareForHotReloadFull(); Api::Result 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_; std::shared_ptr musicManager_; std::unique_ptr commandManager_; Api::PluginInterfacePtr pluginInterface_; // State Api::AppConfig config_; std::filesystem::path configPath_; std::chrono::system_clock::time_point startTime_; std::atomic initialized_{false}; // Event callbacks std::vector eventCallbacks_; mutable std::mutex eventMutex_; // Admin tracking std::vector adminClients_; mutable std::mutex adminMutex_; // Database for persistence std::shared_ptr database_; // RPC server for Desktop/API clients std::shared_ptr 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() 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